這個問題困擾了我半天,國內百度上的資料很是的爛。根本沒法解決問題。java
在android 4 / 5 版本中 scrollview 包含了一個recyclerview 滾動一切正常。android
在6.0中不能所有顯示。原來是一個BUGgit
最終 stackoverflow 找到了解決辦法:github
http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-workingapp
主要代碼ide
https://github.com/amardeshbd/android-recycler-view-wrap-contentcode
This is a sample android app which demonstrates `RecyclerView` wrap_content inside `ScrollView` issue on Marshmallow and Nougat (API 23 & 24) and how to work around it.xml
在API23 24中不能正常工做。ci
解決辦法:element
在 recyclerview 外面再包一層 RelativeLayout
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants"> <!-- DEV NOTE: Outer wrapper relative layout is added intentionally to address issue that only happens on Marshmallow & Nougat devices (API 23 & 24). On marshmallow API 23, the "RecyclerView" `layout_height="wrap_content"` does NOT occupy the height of all the elements added to it via adapter. The result is cut out items that is outside of device viewport when it loads initially. Wrapping "RecyclerView" with "RelativeLayout" fixes the issue on Marshmallow devices. --> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listitem="@layout/row_list_item"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(getContext()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); // Disabled nested scrolling since Parent scrollview will scroll the content. mRecyclerView.setNestedScrollingEnabled(false); // specify an adapter (see also next example) mAdapter = new SimpleListAdapter(DataSetProvider.generateDataset()); mRecyclerView.setAdapter(mAdapter);
問題終於解決。仍是stackoverflow靠譜