1

I have to scroll to a specific item in a recyclerview.

Firstly I need to fetch the position of the item by matching with a string which I pass from the activity to the adapter. then to focus to that item position.

For example in the adapter there is a TextView which has EventID stored, I will pass a string from activity to adapter to match it with the EventID and then fetch that item position and set focus/scroll to that particular position. I have defined a method in adapter to fetch position of item but I don't know how to call it from the activity since it has viewholder as parameter.

I'm confused about how to implement recyclerView.scrollToPosition(mSkipTo) or something else to achieve this. How to call it in my activity. Kindly give a code example since I'm new to this.

public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.ViewHolder> {

private Context context;
private final List<EventsDataModel> eventsDataModels;
private static int currentPosition = 0;


public EventsAdapter(Context context) {

    super();
    this.context = context;
    this.eventsDataModels = new ArrayList<EventsDataModel>();

}

public void updateModels(List<EventsDataModel> newModels) {
    eventsDataModels.clear();
    eventsDataModels.addAll(newModels);
    notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_card_new, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    final EventsDataModel dataAdapter = eventsDataModels.get(position);
    ......
    ......
    viewHolder.EventID.setText(dataAdapter.getEventID());



    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (context instanceof EventsActivity) {
                ((EventsActivity) context).clickOnEventType(viewHolder.EventID.getText().toString());
            }

        }
    });
}

//How to call this method from activity ? Since ViewHolder cannot be obtained from there

public int getItemPosition(ViewHolder viewHolder,String inEventID){
    int itemPos=0;
    if (viewHolder.EventID.getText().equals(inEventID)) {
        itemPos  =  viewHolder.getAdapterPosition();
    }
    return itemPos;
}

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


}
Kumza Ion
  • 353
  • 2
  • 12

2 Answers2

4

Add public function into your adapter.

public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.ViewHolder> {
    ...
public int getItemPosition(String eventId) {
    for (int i = 0; i < eventsDataModels.size(); i++) {
        if (eventsDataModels.get(i).getEventID().equals(eventId)) {
            return i;
        }
    }
    return -1;
}

Activity:

private void scrollToPosition() {
    String eventId = "someId";
    int position = adapter.getItemPosition(eventId);
    if (position >= 0) {
        recycler.scrollToPosition(position);
    }
}
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
  • Could you please have a look at another question of mine? https://stackoverflow.com/questions/64459556/app-crashes-while-calling-locationupdatesservice-with-permission-with-nullpointe – Kumza Ion Oct 21 '20 at 12:13
1

Expose an interface and implement it in your activity. In that interface declare a method that returns an Integer value (Position).Call that method in your onClick. Set the interface from the activity and override the method and use recyclerView.scrollToPosition(mSkipTo).

Akhilesh Mishra
  • 5,876
  • 3
  • 16
  • 32
VANIK JAIN
  • 11
  • 2
  • I want to pass a string from activity to adapter, then matches it and get its position and then focus to that specific item. It would be more helpful if you could explain in the code since I'm new to this. Thanks – Kumza Ion Oct 14 '20 at 03:36