Anit Kumar
Anit Kumar

Reputation: 11

How to stop change data of items in recycler view during scrolling

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

Answers (2)

Omid Hosseini
Omid Hosseini

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

Yachint
Yachint

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

Related Questions