KeyGenQt
KeyGenQt

Reputation: 129

RecyclerView, slow scrollToPosition, setSelected

Prompt, faced with scrollToPosition speed. I RecyclerView 900 positions and need to quickly establish a position as scrollToPosition very slow, c ListView such problems were not setSelection working out perfectly. It is possible to accelerate the scrollToPosition?

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.Holder> {

final static public String TAG = "EventAdapter";

Context context;
List<Event> items;

public EventAdapter(List<Event> items) {
    this.items = items;
}

@Override
public EventAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_event, parent, false);
    context = parent.getContext();
    return new EventAdapter.Holder(v);
}

@Override
public void onBindViewHolder(EventAdapter.Holder holder, int position) {
    holder.setModel(context, items.get(position), position);
}

@Override
public int getItemCount() {
    return items.size();
}

public class Holder extends RecyclerView.ViewHolder {

    public View block;

    Holder(View view) {
        super(view);
            block = (View) view.findViewById(R.id.cv);
        }

        public void setModel(Context context, Event model, int position) {

        }
    }
}

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);

linearLayoutManager.scrollToPosition(index);

Upvotes: 2

Views: 765

Answers (1)

sumandas
sumandas

Reputation: 565

There are couple of ways to do it:

1) Try smoothScrollToPosition as : [will certainly work]

Toast.makeText(this, "Scroll...", Toast.LENGTH_SHORT).show();
myList.smoothScrollToPosition(0);

2) Also read about scrollToPositionWithOffset as this may help too

Upvotes: 1

Related Questions