6

I have a horizontal Recyclerview which displays bitmaps. The Way it is implemented is I have a Imageview and a recyclerview underneath it. The currently selected item is displayed on the image view. The selected image view is given a blue background to indicate it is selected. I can choose images from the gallery and each time a new image is selected, I want to scroll to the last position and make the item selected.

The list of images is maintained in a array list and each time a new image is added, I add the image to the list and notifyDataChanged().

Currently when I am binding a view, I toggle the visibility of the blue background in

public void onBindViewHolder(final MyRecyclerViewHolder holder, int position) { }

But the problem is, if the child is off screen, the bind view is not called and I dont scroll to the new position. I read through the documentation of recycler view and could not figure out how to scroll to that particular child view. I do not there is a SmoothScrollTo method but my question is where do I trigger this ?

0

4 Answers 4

17
+50

There is one solution:

  1. In your RecyclerView adapter, add a variable selectedItem and a methods setSelectedItem():

    private static int selectedItem = -1;
    
     ... ...
    
    public void setSelectedItem(int position)
    {
       selectedItem = position;
    }
    
  2. In your onBindViewHolder(...), add:

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) 
    {
       ... ...
    
       if(selectedItem == position)
          holder.itemView.setSelected(true);
    }   
    
  3. Now you can scroll to specific item and set it selected programmatically by:

    myRecyclerViewAdapter.setSelectedItem(position_scrollTo);
    myRecyclerView.scrollToPosition(position_scrollTo);
    

For example, you want to scroll to last position and make it selected, just:

    int last_pos = myRecyclerViewAdapter.getItemCount() - 1;
    myRecyclerViewAdapter.setSelectedItem(last_pos);
    myRecyclerView.scrollToPosition(last_pos);


[UPDATE]

To add item and make it selected, you should have a addItem(...) method in adapter, which will add item to item list. After adding item, refresh list and scroll to new added/latest item:

myRecyclerViewAdapter.addItem(...);
myRecyclerViewAdapter.notifyDataSetChanged();
myRecyclerViewAdapter.setSelectedItem(myRecyclerViewAdapter.getItemCount() - 1); 
myRecyclerView.scrollToPosition(myRecyclerViewAdapter.getItemCount() - 1);

Hope this help!

2
  • The problem is not actually being able to scroll to a certain position. The problem is being able to scroll to it right after the item has been added . When ever a new Item is added, I get the new list of data, I tell the adapter I have new data by calling the function notifyDataChanged() . If I call scrollToPosition and the position it will crash because the view has not been created yet and we are trying to scroll to it.
    – LostPuppy
    Mar 27, 2015 at 18:47
  • @Shrek, look my UPDATE, now after adding new item, it is selected. I have tested the codes and work fine. Any further problem just tell
    – Xcihnegn
    Mar 28, 2015 at 8:51
2

Your view will not be created until you scroll to the item.

You must call

recyclerView.scrollToPosition(position)

Where position is

 recyclerView.getAdapter().size()

So the item became visible.

2
  • When ever a new Item is added, I get the new list of data, I tell the adapter I have new data by calling the function notifyDataChanged() . If I call scrollToPosition and the position it will crash because the view has not been created yet and we are trying to scroll to it.
    – LostPuppy
    Mar 27, 2015 at 18:46
  • 1
    Why dont you add the item to your current list and notify only rows inserted? Mar 30, 2015 at 18:51
0

Use RecyclerView LayoutManager to scroll item at position

recyclerView.getLayoutManager().scrollToPosition(position)

I forgot to address the timing part ideally this should be called when the adapter has been identified of the dataset change.

0

You could use

 recyclerView.smoothScrollToPosition(View.FOCUS_DOWN);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.