how to select and de-select an item in RecyclerView? How to Highlight selected item only in recycler view?

前端 未结 1 795
傲寒
傲寒 2021-01-18 21:31
@Override
    public void onBindViewHolder(final mainscreenspecializationadap.MyViewHolder holder, final int positionz) {
        this.position = holder.getAdapterPo         


        
1条回答
  •  别那么骄傲
    2021-01-18 22:12

    Here is the solution for your requirement

        public class AdapterClass extends RecyclerView.Adapter {
            private int selected_position = -1;
    
            @Override
            public void onBindViewHolder(AdapterClass.ViewHolder holder, final int position) {
                if (selected_position == position) {
                    // do your stuff here like
                    //Change selected item background color and Show sub item views
    
                } else {
                      // do your stuff here like
                      //Change  unselected item background color and Hide sub item views
                }
      // rest of the code here
    
        holder.linelayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(selected_position==position){
                            selected_position=-1;
                            notifyDataSetChanged();
                            return;
                        }
                        selected_position = position;
                        notifyDataSetChanged();
    
                }
            });
    
        //rest of the code here
    
         }
    
    
    }
    

    0 讨论(0)
提交回复
热议问题