嵌套滚动视图在Android中与RecyclerView一起使用时无法平稳滚动。

11

我正在使用嵌套式滚动视图(Nested Scrollview)来包装RecyclerView和其他按钮。它工作得非常完美,但我注意到当我滚动时不太流畅。请指导如何使滚动更加顺畅。

 <android.support.v4.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/scrollView"
                        android:fillViewport="true">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">
                                <Button
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_action_filter"
                                    android:text="Filter"
                                    android:id="@+id/btn_filter"
                                    android:layout_margin="0dp"
                                    android:layout_weight="1"
                                    />
                                <Button
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_action_sortbyt"
                                    android:layout_margin="0dp"
                                    android:text="Sort By"
                                    android:id="@+id/btn_sortby"
                                    android:layout_weight="1"/>
                            </LinearLayout>
                            <android.support.v7.widget.RecyclerView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/lastest_product_list"
                                android:nestedScrollingEnabled="false"
                                android:isScrollContainer="false">
                            </android.support.v7.widget.RecyclerView>
                        </LinearLayout>
                    </android.support.v4.widget.NestedScrollView>
3个回答

23

根据Android文档,android:nestedScrollingEnabled="false"Android API level >= 21上有效。

如果您还想支持API 21以下的设备,请使用以下内容:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

21

尝试以下代码:

 RecyclerView recycleView = (RecyclerView) findViewById(R.id.lastest_product_list);
    recycleView.setNestedScrollingEnabled(false);
您可以修改您的布局。
<ScrollView>
     <LinearLayout> 
         <android.support.v7.widget.RecyclerView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/lastest_product_list"
              android:nestedScrollingEnabled="false"
              android:isScrollContainer="false">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

在此处查看链接:Recyclerview内部ScrollView无法平稳滚动


1
recycleView.setNestedScrollingEnabled(false); 对我有效,XML 的那些没用。 - M.Baraka

4

若要实现平滑滚动,您可以在recycler view的编码中更改所设置的布局管理器。希望这能有所帮助。

  RecyclerView.LayoutManager layoutManager = new  LinearLayoutManager(getActivity()) {
        @Override
        public boolean canScrollVertically() {
            return false;
        }
    };
    recyclerView.setLayoutManager(layoutManager);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接