2016-12-30 75 views
0

我必須檢查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是滾動??/

+0

你在哪裏設置佈局管理器? –

+0

將佈局管理器設置爲您的回收站視圖 – Charu

+1

在調用isScrollable方法之前設置佈局管理器 – Charu

回答

0

試試這個setAdapterData()。 並在所有其他事情之前調用它。

private void setAdapterData() { 
    mChatAdapter = new ChatAdapter(mMessagesList); 

    mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    mChatMessagesRecyclerView.setLayoutManager(mLayoutManager); 

    mChatMessagesRecyclerView.setAdapter(mChatAdapter); 

    if(isRecyclerScrollable()) 
     mLayoutManager.setStackFromEnd(true); 

} 
public boolean isRecyclerScrollable() { 
     RecyclerView.Adapter adapter = mChatMessagesRecyclerView.getAdapter(); 
     if (mLayoutManager == null || adapter == null) return false; 
      return mLayoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1; 
} 

而且你不需要setStackFromEndLayoutManager()方法

+0

isRecyclerScrollable總是返回true ....這不工作 –

+0

isRecyclerScrollable()總是返回true ....這不工作。 .even使用單個項目返回true –

+0

int visibleItemCount = mLinearLayoutManager.getChildCount(); if(visibleItemCount Charu

0

HII請補充以下代碼CalladapterMethod

private void setAdapterData() { 
    mChatAdapter = new ChatAdapter(mMessagesList); 
    mChatMessagesRecyclerView.setAdapter(mChatAdapter); 
    boolean ss=isRecyclerScrollable(); 
    Log.e("ss",ss+""+mMessagesList.size()+"ll"); 
mChartAdapter.notifyDataSetChanged(); 
} 
0

從您的代碼, 你是你的病情後,設置

mChatMessagesRecyclerView.setLayoutManager(mLayoutManager); 

檢查你在哪裏尋找

mLayoutManager.findLastCompletelyVisibleItemPosition() 

,如果你的條件if(mMessagesList.size()>5)是假的,佈局管理器未在recyclerview設置,它不會具備的條件之前任何可見items.Set它。

+0

其實我的期望是取代如果(mMessagesList.size()> 5)這個條件,因爲它是靜態的,而不是我需要這樣funactional調用 –

+0

公共布爾isRecyclerScrollable(){ LinearLayoutManager layoutManager =(LinearLayoutManager)mChatMessagesRecyclerView.getLayoutManager (); if(layoutManager == null || mChatAdapter == null)return false; Log.e(「ss」,layoutManager.findLastCompletelyVisibleItemPosition()+「ll」); return layoutManager.findLastCompletelyVisibleItemPosition()

+0

但它效果不好.... layoutManager.findLastCompletelyVisibleItemPosition()總是返回-1 –

相關問題