Select items in RecyclerView

后端 未结 2 1208
有刺的猬
有刺的猬 2020-12-31 07:34

Similar question have been asked, but i can\'t get any of them work.

What i want is to select item in RecyclerView, change the background of that item view, and stor

2条回答
  •  盖世英雄少女心
    2020-12-31 08:09

    Make global variable to store position and handle click listener in ViewHolder. Onclick of item, change the global position value like

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            globalPosition=getAdapterPosition();
            notifyDataSetChanged();
        }
    });
    

    then in onBindViewHolder

    if(postion==globalPosition)
    {
        //change color like
        textview.setTextColor(Color.RED);
    }
    else
    {
        //revert back to regular color 
        textview.setTextColor(Color.WHITE);
    }
    

    with this code, the item you clicked get red colored and all other wiil be in white.

提交回复
热议问题