在使用CoordinatorLayout和AppBarLayout實現嵌套滑動的時候,出現listview沒有嵌套滑動;
如果要實現嵌套滑動,則需要添加NestedScrollView,但是結果發現listview只顯示一行數據
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="500dp" /> </android.support.v4.widget.NestedScrollView>
這需要重寫listview的onMeasure方法
public class NestedListView extends ListView { public NestedListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //測量的大小由一個32位的數字表示,前兩位表示測量模式,后30位表示大小,這里需要右移兩位才能拿到測量的大小 int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightSpec); } }
然后在含有listview的布局文件和處理listview邏輯的Activity類文件中,用NestedListView代替ListView。結果達到需求的需要
作者:jxtx
鏈接:https://www.jianshu.com/p/f8a89a00e030
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。