zoukankan      html  css  js  c++  java
  • 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
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    rpm
    java 锁4
    java 锁3
    小技巧:在线生成按钮Shape的网站
    Python入门:内置函数
    山东理工大学SDUT
    Android项目实战(四十):Andoird 7.0+ 安装APK适配
    Android打赏功能:支付宝转账
    浅谈Android数据库DBFlow
    acffo的开源项目汇总
  • 原文地址:https://www.cnblogs.com/geili/p/10264360.html
Copyright © 2011-2022 走看看