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);
    }



}

  然后在含有listview的布局文件和處理listview邏輯的Activity類文件中,用NestedListView代替ListView。結果達到需求的需要

原因:參考:https://www.jianshu.com/p/7a56cd29e090



作者:jxtx
鏈接:https://www.jianshu.com/p/f8a89a00e030
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM