how to keep RecyclerView always scroll bottom

后端 未结 5 1223
野的像风
野的像风 2020-12-15 15:26

I Use Recyclerview Replace with list view I want to keep Recyclerview always scroll bottom.

ListView can use this method setTranscriptMode(AbsListView.TRANSCRI

相关标签:
5条回答
  • 2020-12-15 16:02

    I have faced the same problem and I solved it using the approach mentioned here. It is used to detect whether soft keyboard is open or not and if it is open, just call the smoothScrollToPosition() method.

    A much simpler solution is to give your activity's root view a known ID, say '@+id/activityRoot', hook a GlobalLayoutListener into the ViewTreeObserver, and from there calculate the size diff between your activity's view root and the window size:

    final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > 100) { 
            recyclerView.smoothScrollToPosition(myAdapter.getItemCount() - 1);
        }
     }
    });
    

    Easy!

    0 讨论(0)
  • 2020-12-15 16:03

    This is because RV thinks its reference point is TOP and when keyboard comes up, RV's size is updated by the parent and RV keeps its reference point stable. (thus keeps the top position at the same location)

    You can set LayoutManager#ReverseLayout to true in which case RV will layout items from the end of the adapter.

    e.g. adapter position 0 is at the bottom, 1 is above it etc...

    This will of course require you to reverse the order of your adapter.

    I'm not sure but setting stack from end may also give you the same result w/o reordering your adapter.

    0 讨论(0)
  • 2020-12-15 16:03
    recyclerView.scrollToPosition(getAdapter().getItemCount()-1);
    
    0 讨论(0)
  • 2020-12-15 16:05

    I have also faced same problem. But following code help me. I hope this is useful.

    In this staus is arraylist.

     recyclerView.scrollToPosition(staus.size()-1);
    

    next one is:- In This you can use adapter class

       recyclerView.scrollToPosition(showAdapter.getItemCount()-1);
    
    0 讨论(0)
  • 2020-12-15 16:13

    If you want to keep the scroll position anchored to the bottom of the RecyclerView, it's useful in chat apps. just call setStackFromEnd(true) to on the LinearLayoutManager to make the keyboard keep the list items anchored on the bottom (the keyboard) and not the top.

    0 讨论(0)
提交回复
热议问题