Reputation: 11
In my case initial loading of data is perfect but when I scrolling recyclerView at some position textView is visible even if the status does not equal to 1.
adapter = new FirebaseRecyclerAdapter<Model, MyViewHolder>(Model.class,
R.layout.layout_order_list,
MyViewHolder.class,
reference) {
@Override
protected void populateViewHolder(MyViewHolder viewHolder, Model model, int position) {
viewHolder.textView.setText(model.getName());
String status = model.getStatus();
if(status.equals("1")){
viewHolder.btnStatus.setVisibility(View.GONE);
viewHolder.textView.setVisibility(View.VISIBLE);
}
}
};
Recycler View
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
Upvotes: 1
Views: 412
Reputation: 1
Please add this overriding method to your custom adapter:
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
Upvotes: -1
Reputation: 135
You need to add an else block just after the if block to take into account for positions where status is not equal to 1.
The populateViewHolder function is run for each view in the recycler view and view holders are reused.
What might be happening here is that initially the status = 1 for one such view and your 'if' block executes, but for others where status != 1, there is no 'else' block to handle the case and when the view holder gets reused, the textView remains visible.
if(status.equals("1")){
viewHolder.btnStatus.setVisibility(View.GONE);
viewHolder.textView.setVisibility(View.VISIBLE);
} else {
viewHolder.btnStatus.setVisibility(View.VISIBLE);
viewHolder.textView.setVisibility(View.GONE);
}
Upvotes: 3