1.為了更好的适應NestedScrollView的特性,應該把布局檔案的根布局設為CoordinatorLayout;
2.給NestedScrollView設定behavior,即 app:layout_behavior="@string/appbar_scrolling_view_behavior"
3.RecyclerView的高度設為match parent或者wrap content
這樣就可以保證RecyclerView的内容可以顯示出來,但是為了解決滑動不流暢的問題,還需要最後一步:
4. recyclerView.setNestedScrollingEnabled(false);
如果 是在布局檔案中使用可以參考如下:
<android.support.v4.widget.NestedScrollView
android:id="@+id/f_scroll_sub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/mlaycourse"
android:fadingEdge="none"
android:scrollbars="none">
<android.support.v7.widget.RecyclerView android :id= "@+id/mRecycleorCourse" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :nestedScrollingEnabled= "false" app :layout_behavior= "@string/appbar_scrolling_view_behavior" /> </android.support.v4.widget.NestedScrollView>
這樣就可以實作了