1. 程式人生 > >NestedScrollView嵌套ListView時只顯示一行的解決方法

NestedScrollView嵌套ListView時只顯示一行的解決方法

實現 shu clas dsc http 數據 width ref nat

在使用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
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並註明出處。

NestedScrollView嵌套ListView時只顯示一行的解決方法