android– 如何在Swipe上更改RecyclerView项目颜色?

android– 如何在Swipe上更改RecyclerView项目颜色?,第1张

概述我试图在轻扫时使recyclerView项目更改颜色,因此它会被高亮显示,因为在刷卡时我打开一个AlertDialog,这样该人就会识别出他正在改变哪一项.但我的主要问题是当我尝试使用时更改背景颜色viewHolder.itemView.setBackgroundColor(Color.parseColor("#cc0000"));没有什么是heppen

我试图在轻扫时使recyclerVIEw项目更改颜色,因此它会被高亮显示,因为在刷卡时我打开一个AlertDialog,这样该人就会识别出他正在改变哪一项.

但我的主要问题是当我尝试使用时更改背景颜色

vIEwHolder.itemVIEw.setBackgroundcolor(color.parsecolor("#cc0000"));

没有什么是heppening,这是我的recyclerVIEw构建器的完整代码

public voID buildtopRecyclerVIEw(){    mRecyclerVIEwtop = findVIEwByID(R.ID.ListVIEw);    mRecyclerVIEwtop.setHasFixedSize(true);    RecyclerVIEw.LayoutManager mLayoutManager = new linearlayoutmanager(this);    exampleAdapter = new ExampleAdapter(itemCassas);    mRecyclerVIEwtop.setLayoutManager(mLayoutManager);    mRecyclerVIEwtop.setAdapter(exampleAdapter);  //  onSwipe();    new itemtouchhelper(new itemtouchhelper.SimpleCallback(0,itemtouchhelper.left | itemtouchhelper.RIGHT) {        @OverrIDe        public boolean onMove(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, RecyclerVIEw.VIEwHolder target) {            return false;        }        @OverrIDe        public voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder, int direction) {            int position = vIEwHolder.getAdapterposition();            if (direction == itemtouchhelper.RIGHT) {                customAllertDelete(position);                vIEwHolder.itemVIEw.setBackgroundcolor(color.parsecolor("#cc0000"));                exampleAdapter.notifyDataSetChanged();            }            if (direction == itemtouchhelper.left) {                customAllertQuantity(position);            }        }    }).attachToRecyclerVIEw(mRecyclerVIEwtop);}

如果它可能有用,这里也是适配器代码

public class ExampleAdapter extends RecyclerVIEw.Adapter<ExampleAdapter.ExampleVIEwHolder> {    private ArrayList<ItemCassa> mExampleList;    @NonNull    @OverrIDe    public ExampleVIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) {        VIEw v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerList_item,parent,false);        return new ExampleVIEwHolder(v);    }    ExampleAdapter(ArrayList<ItemCassa> exampleList){        mExampleList = exampleList;    }    @OverrIDe    public voID onBindVIEwHolder(@NonNull ExampleVIEwHolder holder, int position) {        ItemCassa item = mExampleList.get(position);        holder.desc.setText(item.getBtnname());        holder.qta.setText(String.valueOf(item.getQuant()));        holder.imp.setText(String.valueOf((new DecimalFormat("#0.00").format(item.getPrice()))));        if(position % 2 == 0 ){            holder.itemVIEw.setBackgroundcolor(color.parsecolor("#C0C0C0"));            holder.desc.setBackgroundcolor(color.parsecolor("#C0C0C0"));            holder.qta.setBackgroundcolor(color.parsecolor("#C0C0C0"));            holder.imp.setBackgroundcolor(color.parsecolor("#C0C0C0"));        }else if(position % 2 == 1){            holder.itemVIEw.setBackgroundcolor(color.parsecolor("#D3D3D3"));            holder.desc.setBackgroundcolor(color.parsecolor("#D3D3D3"));            holder.qta.setBackgroundcolor(color.parsecolor("#D3D3D3"));            holder.imp.setBackgroundcolor(color.parsecolor("#D3D3D3"));        }    }    @OverrIDe    public int getItemCount() {       return mExampleList.size();    }    public static class ExampleVIEwHolder extends RecyclerVIEw.VIEwHolder {        public TextVIEw desc;        public TextVIEw qta;        public TextVIEw imp;        ExampleVIEwHolder(VIEw itemVIEw) {            super(itemVIEw);            desc = itemVIEw.findVIEwByID(R.ID.Desc);            qta = itemVIEw.findVIEwByID(R.ID.Qta);            imp = itemVIEw.findVIEwByID(R.ID.Imp);        }    }    public voID removeItem(int position) {        mExampleList.remove(position);        notifyItemRemoved(position);    }}

这是gif“解释”我正在尝试做的事情,正如你可以看到它滑动它打开一个AlertDialog并转回所选项目,但现在我将为所选项目背景着色,直到用户做出选择在AlertDialog中

解决方法:

尝试使用这样:

首先在onSwiped方法中调用适配器的方法并传递所需的参数.

  @OverrIDe        public voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder, int swipeDir) {            int swipedposition = vIEwHolder.getAdapterposition();            YourAdapter adapter = (YourAdapter) rlCartList.getAdapter();                adapter.remove(swipedposition); // I was removing items so you                                                  can change the name of method as you like        }

现在在您的适配器中执行以下 *** 作:

  public voID remove(int position) {        YourModel item = cartItems.get(position);        if (cartItems.contains(item)) {            VIEwHolder.tvItemname.setBackgroundcolor(mContext.getResources().getcolor(R.color.grey));            notifyDataSetChanged();        }    }

还有一件事是你需要制作你的textVIEw或你正在为Static指定背景的视图.我已经测试了这段代码,它在我的项目中正常运行,textVIEw的背景在刷卡时发生了变化.

如果您需要进一步的帮助,请告诉我还有一件事你可以在适配器中使用警报对话框:)

总结

以上是内存溢出为你收集整理的android – 如何在Swipe上更改RecyclerView项目颜色?全部内容,希望文章能够帮你解决android – 如何在Swipe上更改RecyclerView项目颜色?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104701.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存