如何在滑动上更改RecyclerView项目的颜色?

时间:2018-06-21 06:50:08

标签: android android-recyclerview

我正在尝试使recyclerView的项目在滑动时改变颜色,因此它会被加亮,因为在滑动时我打开了AlertDialog,以便该人可以识别出他正在改变哪个项目。

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

viewHolder.itemView.setBackgroundColor(Color.parseColor("#cc0000"));

没什么大不了的,这是我从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中进行选择

IMAGE

1 个答案:

答案 0 :(得分:1)

尝试这样使用:

首先,在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的背景就可以正常运行。

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