Issue
couldn't highlight the selected item in the adapter recyclerview.
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
ViewHolder holder = (ViewHolder) viewHolder;
ChannelMainBean bean = mList.get(position);
holder.tvContent.setText(bean.getChannelName());
if (holder.selectedItem == position){
holder.tvContent.setBackground(ResUtils.getDrawable(R.drawable.choose_network_border_blue));
}
else{
holder.tvContent.setBackground(ResUtils.getDrawable(R.drawable.choose_network_background_default));
}
}
Solution
Try This
private int selectedItem = -1;
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
ViewHolder holder = (ViewHolder) viewHolder;
ChannelMainBean bean = mList.get(position);
holder.tvContent.setText(bean.getChannelName());
holder.tvContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedItem = position;
notifyDataSetChanged();
}
});
if (selectedItem == position){
holder.tvContent.setBackground(ResUtils.getDrawable(R.drawable.choose_network_border_blue));
}
else{
holder.tvContent.setBackground(ResUtils.getDrawable(R.drawable.choose_network_background_default));
}
}
}
selectedItem
is integer variable set global and than set click listener on TextView
and inside click method set click position in to selectedItem
and than notifyDataSetChanged()
for update background for text view
Answered By - Mayur
Answer Checked By - Robin (JavaFixing Admin)