LostPuppy
LostPuppy

Reputation: 2501

Recyclerview make the last added item be the selected item?

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 ?

Upvotes: 6

Views: 18020

Answers (4)

Marwen Doukh
Marwen Doukh

Reputation: 2050

You could use

 recyclerView.smoothScrollToPosition(View.FOCUS_DOWN);

Upvotes: 0

Xcihnegn
Xcihnegn

Reputation: 11607

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!

Upvotes: 17

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

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.

Upvotes: 2

rahul.taicho
rahul.taicho

Reputation: 1379

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.

Upvotes: 0

Related Questions