這可能工作。它應該返回ViewHolder的所有可見的子視圖。您還可以通過與recyclerView.getAdapter().getItemCount()
更換firstItem
與0
和lastItem
嘗試:
private static int getRecyclerViewChildCount(RecyclerView recyclerView) {
//This below section is a bit clumsy, but the findFirstVisibleItem() etc. methods
//Only apply to LinearLayoutManagers and their subclasses.
try {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
} catch (ClassCastException e) {
throw new IllegalArgumentException("LayoutManager must be of type " + LinearLayoutManager.class.getSimpleName());
}
final int firstItem = layoutManager.findFirstVisibleItemPositiob();
final int lastItem = layoutManager.recyclverView.getLayoutManager().findLastVisibleItemPosition();
int viewCount = 0;
for (int i = firstItem; i < lastItem; i++) {
View view = recyclerView.findViewHolderForAdapterPosition(i).itemView;
viewCount += view.getChildCount();
}
return viewCount;
}
http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemCount() –
你是什麼意思?已經有['getItemCount()'](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemCount())方法,在構建時您必須重寫適配器。你有什麼別的嗎? – PPartisan
我想到一個猜測..說你有一個viewholder對象; inOncreateViewHolder函數你得到父母的權利..從你可以嘗試獲得所有的孩子;不知道 – DJphy