键盘打开时 Recyclerview 不滚动结束

Recyclerview not scrolling to end when keyboard opens

我在我的应用程序中使用 recylerview,每当将新元素添加到 recyclerview 时,它都会使用

滚动到最后一个元素
recyclerView.scrollToPosition(adapter.getCount());

但是,每当键盘打开时(因为 editTextView),它会调整视图的大小并且 recyclerview 变小,但无法滚动到最后一个元素。

android:windowSoftInputMode="adjustResize"

我什至尝试使用以下代码滚动到最后一个元素,但没有成功

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                recyclerView.scrollToPosition(chatAdapter.getItemCount());
            }
        }
    });

我可以尝试 adjustPan 将平移向上移动,但它隐藏了我的工具栏。 请提出任何解决问题的方法。

您可以使用 recyclerview.addOnLayoutChangeListener() 捕捉键盘变化。 如果 bottom 小于 oldBottom 则键盘处于弹起状态。

if ( bottom < oldBottom) { 
   recyclerview.postDelayed(new Runnable() { 
       @Override 
       public void run() {
           recyclerview.smoothScrollToPosition(bottomPosition); 
       }
    }, 100);
}
        recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());

添加您的 activity 或片段:

    if (Build.VERSION.SDK_INT >= 11) {
        recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v,
                    int left, int top, int right, int bottom,
                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom < oldBottom) {
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.smoothScrollToPosition(
                                    recyclerView.getAdapter().getItemCount() - 1);
                        }
                    }, 100);
                }
            }
        });
    }

NestedScrollView

里面绑定RecyclerView
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.v4.widget.NestedScrollView>

虽然这是一个老问题,但我今天遇到了这个问题,我发现上述方法中的 none 有效。这是我的解决方案

    recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v,
                                   int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (bottom < oldBottom) {
                final int lastAdapterItem = mAdapter.getItemCount() - 1;
                recyclerView.post(new Runnable() {
                  @Override
                  public void run() {
                    int recyclerViewPositionOffset = -1000000;
                    View bottomView = linearLayoutManager.findViewByPosition(lastAdapterItem);
                    if (bottomView != null) {
                       recyclerViewPositionOffset = 0 - bottomView.getHeight();
                     }
                     linearLayoutManager.scrollToPositionWithOffset(lastAdapterItem, recyclerViewPositionOffset);
                  }
                });
              }
         });
   }

对我有用

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

这有效。

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mManager;

...

mManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mManager);

(initialize and set adapter.)

mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (bottom < oldBottom) scrollToBottom();
    }
});

private void scrollToBottom() {
    mManager.smoothScrollToPosition(mRecyclerView, null, mAdapter.getItemCount());
}

layoutManager.scrollToPositionWithOffset(chatMessageAdapter.getItemCount() - 1, 0);

它在支持库版本 27.0.1 中正常工作

清单中没有要设置的内容。

val currentScrollPosition = 0

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)

            currentScrollPosition = recyclerView.computeVerticalScrollOffset() + recyclerView.computeVerticalScrollExtent()
        }

        override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) { }
    })

    storyList.addOnLayoutChangeListener { view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
        if (bottom < oldBottom) {
            if (currentScrollPosition >= recyclerView.computeVerticalScrollRange()) {
                recyclerVIew.post {
                    recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_NEVER
                    recyclerView.smoothScrollBy(0, recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollOffset() + recyclerView.computeVerticalScrollExtent())
                }
            }
        } else {
            recyclerView.overScrollMode = RecyclerView.OVER_SCROLL_ALWAYS
        }
    }

我发现 postDelayed 是不必要的,并且使用适配器位置不考虑何时将回收器滚动到项目中间的某个位置。我用这个实现了我想要的外观:

recycler.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
    if (bottom < oldBottom) {
        messageRecycler.scrollBy(0, oldBottom - bottom);
    }
})

您可以使用 addOnItemTouchListener 来查看滚动变化:

private Boolean scrollListerActivate = true;

mRecyclerViewTop.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if(scrollListerActivate == true) {
            scrollListTopManager();
        }
    }
});

要滚动到最后(或其他)位置,您应该使用 scrollListTopManager 方法:

public void scrollListTopManager() {
    int position = 0;
    int itemCount = mRecyclerViewTop.getAdapter().getItemCount();
    position = itemCount - 1;
    
    mRecyclerViewTop.scrollToPosition(position);
}

如果您想手动滚动,您还应该使用 addOnItemTouchListener 停止使用 scrollListTopManager 方法:

mRecyclerViewTop.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent 
    motionEvent) {
        scrollListerActivate = false;
        return false;
    }
    @Override
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) 
    {}
            
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean b) {}
});