2017-08-01 14 views
-3

以下代碼是查找第一個位置的視圖。我如何找到最後位置的觀點?查找關於RecyclerView最後位置的視圖?

TextView textView = 
    (TextView)myRecyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.myTV); //this is first position 

,這裏是我的xml

<android.support.v7.widget.RecyclerView 
     android:id="@+id/messageRecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:nestedScrollingEnabled="false" /> 
+0

使用recyclerview大小或長度 –

+0

@jigarsavaliya你能不能給示例代碼請?我會立即接受工作答案。 –

+0

提供您的適配器 –

回答

2

試試這個

TextView textView =(TextView)myRecyclerView.findViewHolderForAdapterPosition(mFirebaseAdapter.getItemCount()-1).itemView.findViewById(R.id.myTV); 

試試這個

TextView textView = (TextView) myRecyclerView.findViewHolderForAdapterPosition(arraylist.size()-1).itemView.findViewById(R.id.myTV); 

或者試試這個

簡單,你可以這樣做:

if(position == YouList.size()-1) 
{ 
    /*lastItem*/ 
} 
+0

'arraylist'是什麼?它在我的項目中是紅色的... –

+0

它通過您的回收站適配器類 –

+0

@NileshRathod請通過完整的代碼更新您的答案,所以我可以接受它:) –

0
TextView textView =(TextView)myRecyclerView.findViewHolderForAdapterPosition(Arraylist.size()-1).itemView.findViewById(R.id.myTV); 
+0

'Arraylist'在我的項目中是紅色的。 –

+0

把你recyclerview的適配器 –

+0

看到我的編輯請 –

0

你可以試試這個

TextView textView = (TextView) myRecyclerView.findViewHolderForAdapterPosition(myRecyclerView.getAdapter().getItemCount() - 1).itemView.findViewById(R.id.myTV); 
+0

我得到的錯誤:'java.lang.NullPointerException:嘗試從字段'android.support.View android.support.v7.widget.RecyclerView $ ViewHolder.itemView'讀取空對象引用' –

+0

'0'它的工作原理:'TextView textView = (TextView)myRecyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.myTV);'但是你的代碼崩潰了。 –

相關問題