如何在NestedScrollView内使用RecyclerView ?
设置适配器后,RecyclerView内容不可见。
更新布局代码。
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/keyline_1">
</RelativeLayout>
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e5e5e5" />
<android.support.v7.widget.RecyclerView
android:id="@+id/conversation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我已经使用了这个很棒的扩展(写在kotlin,但也可以在Java中使用)
https://github.com/Widgetlabs/expedition-nestedscrollview
基本上你在任何包中都有NestedRecyclerView比如在项目中的utils,然后创建你的recyclerview
<com.your_package.utils.NestedRecyclerView
android:id="@+id/rv_test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
看看Marc Knaup写的这篇很棒的文章
https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a
如果你在NestedScrollView中使用RecyclerView ScrollListener, addOnScrollListener监听器不能正常工作。
使用这段代码。
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
......
}
});
这段代码工作良好的RecyclerView ScrollListener内的NestedScrollView。
谢谢
我在NestedScrollView内使用RecyclerView,它为我工作。我必须记住的唯一问题是NestedScrollView只接受一个子视图。所以在我的情况下,我使用的LienearLayout视图组,它是住房我的RecyclerView加上其他一些我需要的视图。
我经历了一个问题,把我的RecyclerView内的NestedScrollView。我意识到我的RecyclerView的内容滚动变慢了。
我后来意识到我的RecyclerView正在接收滚动事件,因此与NestedScrollView的滚动行为相冲突。
所以为了解决这个问题,我不得不禁用我的RecyclerView的滚动功能与这个方法movieListNewRecyclerView.setNestedScrollingEnabled(false);
你可以在我的Instagram上查看我实际做了什么。这是我的instagram账号elix03