Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change RecyclerView item color on Swipe?

I'm trying to make recyclerView item change color on swipe so it's will be highlited because on swipe i open an AlertDialog so the person will recognize which one item he is changing.

But my main issue is that when i try to change the background color by using

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

Nothing is heppening, here is my full code from the builder of 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);

}

And here is also the Adapter code if it could be usefull

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);
    }




}

Here is the gif that "explain" a bit what i'm trying to do, as you can see on swipe it's open an AlertDialog and turn back the selected item, but now i would color that selected item background until the user make a choose in the AlertDialog

IMAGE

like image 462
NiceToMytyuk Avatar asked Jun 21 '18 06:06

NiceToMytyuk


1 Answers

Try using like this:

First of all in your onSwiped method call your adapter's method and pass the required parameters.

  @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
        }

Now in your adapter do something like this:

  public void remove(int position) {

        YourModel item = cartItems.get(position);

        if (cartItems.contains(item)) {
            ViewHolder.tvItemName.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
            notifyDataSetChanged();
        }
    }

One more thing is you will need to make your textView or the view you are assigning the background to Static. And I have tested this code it's running fine in my project the background of textView changes on swipe.

Tell me if you need further assistance. And also one more thing you can use your alert dialog in adapter too :)

like image 191
Umair Avatar answered Nov 14 '22 02:11

Umair