Reputation: 145
I'm using RecyclerView, I have one SubmitButton, when i click SubmitButton I'm changing the background of child view item. when i scroll the recyclerview that item background colour change to default colour, how can i fix this error.
Here is my adapter
public class ItemArrayAdapter extends RecyclerView.Adapter<ItemArrayAdapter.ViewHolder> {
//All methods in this adapter are required for a bare minimum recyclerview adapter
private int listItemLayout;
private ArrayList<Item> itemList;
// Constructor of the class
public ItemArrayAdapter(int layoutId, ArrayList<Item> itemList) {
listItemLayout = layoutId;
this.itemList = itemList;
}
// get the size of the list
@Override
public int getItemCount() {
return itemList == null ? 0 : itemList.size();
}
// specify the row layout file and click for each row
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(listItemLayout, parent, false);
ViewHolder myViewHolder = new ViewHolder(view);
return myViewHolder;
}
// load data in each row element
@Override
public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
TextView item = holder.item;
item.setText(itemList.get(listPosition).getName());
holder.setIsRecyclable(false);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
// Static inner class to initialize the views of rows
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView item;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
item = (TextView) itemView.findViewById(R.id.row_item);
}
@Override
public void onClick(View view) {
Log.d("onclick", "onClick " + getLayoutPosition() + " " + item.getText());
}
}
}
My Activity
public class FourthActivity extends AppCompatActivity {
RecyclerView recyclerView;
Button button;
int currentVisibleItem = 0;
private LinearLayoutManager linearLayoutManager;
private boolean programaticallyScrolled;
ImageView img_LeftScroll, img_right_scroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
ArrayList<Item> itemList = new ArrayList<>();
itemList.add(new Item("Item 1"));
itemList.add(new Item("Naveen"));
itemList.add(new Item("Raj"));
itemList.add(new Item("Kumar"));
itemList.add(new Item("Mutharasi"));
itemList.add(new Item("Mutharasi"));
itemList.add(new Item("John"));
itemList.add(new Item("Peeter"));
itemList.add(new Item("Son"));
final ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(R.layout.list_item, itemList);
recyclerView = (RecyclerView) findViewById(R.id.rvs);
recyclerView.setHasFixedSize(true);
linearLayoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(itemArrayAdapter);
img_LeftScroll = findViewById(R.id.leftimage);
img_right_scroll = findViewById(R.id.imageView12);
button=findViewById(R.id.submit_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View row=linearLayoutManager.findViewByPosition(2);
row.setBackgroundColor(getResources().getColor(R.color.blue));
Log.d("onclick", "onClick color changer " );
}
});
}
}
Here is my example clip How to fix this error.
Upvotes: 0
Views: 764
Reputation: 509
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View row=linearLayoutManager.findViewByPosition(2);
row.setBackgroundColor(getResources().getColor(R.color.blue));
Log.d("onclick", "onClick color changer " );
}
});
this is wrong way of implement the selected color. You must apply the item change on the adapter -> onBindViewHolder method.
@Override
public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
TextView item = holder.item;
item.setText(itemList.get(listPosition).getName());
holder.setIsRecyclable(false);
if( itemList.get(listPosition).isSelected){
holder.itemview.setBackgroundColor(getResources().getColor(R.color.blue));
}else{
holder.itemview.setBackgroundColor(getResources().getColor(R.color.light));
}
}
and set method to select next item in the adapter and make sure to notifydatasetchang method to called.
public void setNextItem(int currentPosition){
for(int i=0;i<itemList.size() ;i++){
if(i == currentPosition) {
itemList.get(i).setSelected = true;
}else{
itemList.get(i).setSelected = false;
}
}
notifyDataSetChanged();
}
Upvotes: 2
Reputation: 4444
Instead making recyclerview holder holder.setIsRecyclable
to false, set the color of the background of the row every time, inside the onBindViewHolder()
method.
And inside onClick in Activity, just change the listitem that populates the adapter and call notifyDataSetChanaged()
Upvotes: 0