NestedScrollView嵌套ListView时只显示一行的解决方法

在使用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);
    }



}

  

结果达到需求的需要
原因:参考:https://www.jianshu.com/p/7a56cd29e090



作者:jxtx
链接:https://www.jianshu.com/p/f8a89a00e030
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自www.cnblogs.com/geili/p/10264360.html