android – 在NestedScrollView onBindViewHolder中的RecyclerView调用所有getItemCount大小

前端之家收集整理的这篇文章主要介绍了android – 在NestedScrollView onBindViewHolder中的RecyclerView调用所有getItemCount大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我将RecyclerView放入NestedScrollView时,然后onBindViewHolder调用所有行,比如说我有一个大小为30的列表,那么即使没有滚动,也会同时为所有30行调用onBindViewHolder
  1. RecyclerView list;
  2. LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
  3. list.setLayoutManager(layoutManager);
  4. layoutManager.setAutoMeasureEnabled(true);
  5. list.setNestedScrollingEnabled(false);
  6. list.addItemDecoration(new VerticalSpaceItemDecoration(5));
  7. list.setAdapter(adapter);

我的xml是

  1. <android.support.v4.widget.NestedScrollView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:fillViewport="true"
  5. android:scrollbars="none"
  6. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  7. <RelativeLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:background="@color/grey">
  11. <android.support.v7.widget.RecyclerView
  12. android:id="@+id/recycler_views"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_below="@+id/info"
  16. android:layout_marginBottom="5dp"
  17. android:layout_marginLeft="5dp"
  18. android:layout_marginRight="5dp"
  19. android:textAlignment="center"
  20.  
  21. android:visibility="visible"
  22. />

但如果我删除NestedScrollView它正常工作.

解决方法

高度问题引起的问题.

1)编辑NestedScrollView& RecyclerView如下:

  1. <android.support.v4.widget.NestedScrollView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:fillViewport="true"
  5. ......
  6. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  7. .......
  8. <android.support.v7.widget.RecyclerView
  9. android:id="@+id/recycler_views"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. .....
  13. />

2)确保你编译了’com.android.support:recyclerview-v7:23.2.1′

猜你在找的Android相关文章