Does Recyclerview make the last added item selected?

I have a horizontal Recyclerview that displays bitmaps. The way it is implemented is that I have an Imageview and a recyclerview underneath. The currently selected item is displayed in the image. A blue background is displayed for the selected image to indicate that it is selected. I can select images from the gallery, and every time a new image is selected, I want to go to the last position and make the item selected.

The list of images is stored in the list of arrays, and every time a new image is added, I add the image to the list and notifyDataChanged ().

Currently, when I snap the view, I switch the visibility of the blue background to

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

But the problem is that if the child is turned off, the snap view is not called, and I am not moving to a new position. I read the recycler view documentation and couldn't figure out how to scroll this kind of child view. I don't have a SmoothScrollTo method, but my question is where can I call this?

+4
source share
4 answers

There is one solution:

  • In your adapter, RecyclerViewadd the variable selectedItemand methods setSelectedItem():

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

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) 
    {
       ... ...
    
       if(selectedItem == position)
          holder.itemView.setSelected(true);
    }   
    
  • Now you can go to a specific element and install it programmatically:

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

, , :

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


[ ]

, addItem(...) , . , / :

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

!

+16

, .

recyclerView.scrollToPosition(position)

 recyclerView.getAdapter().size()

, .

+1

RecyclerView LayoutManager

recyclerView.getLayoutManager().scrollToPosition(position)

, , .

0

 recyclerView.smoothScrollToPosition(View.FOCUS_DOWN);
0
source

All Articles