how to keep RecyclerView always scroll bottom

后端 未结 5 1227
野的像风
野的像风 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!

提交回复
热议问题