我必須檢查RecyclerView
是否能夠layoutManager.findLastCompletelyVisibleItemPosition()
對我來說總是會返回-1
。這是我的代碼。我是指它形成Check if RecyclerView is scrollable檢查RecyclerView是否滾動無法正常工作
private void setAdapterData() {
mChatAdapter = new ChatAdapter(mMessagesList);
mChatMessagesRecyclerView.setAdapter(mChatAdapter);
boolean ss=isRecyclerScrollable();
Log.e("ss",ss+""+mMessagesList.size()+"ll");
}
public boolean isRecyclerScrollable() {
LinearLayoutManager layoutManager = (LinearLayoutManager) mChatMessagesRecyclerView.getLayoutManager();
if (layoutManager == null || mChatAdapter == null) return false;
Log.e("ss",layoutManager.findLastCompletelyVisibleItemPosition() +"ll");
return layoutManager.findLastCompletelyVisibleItemPosition() < mChatAdapter.getItemCount() - 1;
}
我實際的代碼是
private void setAdapterData() {
mChatAdapter = new ChatAdapter(mMessagesList);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mChatMessagesRecyclerView.setAdapter(mChatAdapter);
if(isRecyclerScrollable())
mLayoutManager.setStackFromEnd(true);
mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);
}
public boolean isRecyclerScrollable() {
RecyclerView.Adapter adapter = mChatMessagesRecyclerView.getAdapter();
if (mLayoutManager == null || adapter == null) return false;
return mLayoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}
我實際上是尋找答案這個問題
set setStackFromEnd parameter to recycler view based on some condition check
我當前的工作代碼
if(mMessagesList.size()>5)
setStackFromEndLayoutManager();
setAdapterData();
private void setStackFromEndLayoutManager() {
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setStackFromEnd(true);
mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);
}
我硬編碼在這裏mMessagesList.size()> 5 ...我必須使它作爲dynamic..so我尋找答案檢查RecyclerView是滾動??/
你在哪裏設置佈局管理器? –
將佈局管理器設置爲您的回收站視圖 – Charu
在調用isScrollable方法之前設置佈局管理器 – Charu