Recyclerview not scrolling to end when keyboard opens

前端 未结 10 1385
有刺的猬
有刺的猬 2020-12-04 16:19

I am using recylerview in my application and whenever new element is added to recyclerview, it scrolls to last element by using

recyclerView.scrollToPosition         


        
10条回答
  •  眼角桃花
    2020-12-04 17:09

    This works.

    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());
    }
    

提交回复
热议问题