zoukankan      html  css  js  c++  java
  • ScrollView listView gridView 之间的冲突问题

    在ScrollView中的listView gridView添加适配器之后添加
    //设置gridView整体的高度
    public void setListViewHeightBasedOnChildren(GridView gridView) {
    // 获取ListView对应的Adapter

    if(adapter== null) {
    return;
    }
    int totalHeight = 0;
    for(int i = 0, len = adapter.getCount(); i < len; i++) { //
    listAdapter.getCount()返回数据项的数目
    View listItem = adapter.getView(i, null, gridView);
    listItem.measure(0, 0); // 计算子项View 的宽高
    totalHeight += listItem.getMeasuredHeight(); // 统计所有子项
    的总高度
    }
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = totalHeight
    + (gridView.getHeight() * (adapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    gridView.setLayoutParams(params);
    }

    相当于把他们展开用ScrollView滚动查看
    在listView gridView的父元素中添加 android:focusableInTouchMode="true"
    android:focusable="true"可以避免他们展开时获取焦点让父元素获取焦点


    在ScrollView中的 避免父控件拦截触摸事件
    gridView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    gridView.getParent().requestDisallowInterceptTouchEvent(true);
    return false;//没消费当前事件
    }
    });

    另一种方式 定义高度为wrap content  重写一个listView 重新算高度

    <com.xqx.ximafm.app.widgets.FullListView

                        android:id="@+id/fullListView"

                        android:layout_width="match_parent"

                        android:layout_height="wrap_content">

    public class FullListView extends ListView {

        public FullListView(Context context) {

            super(context);

        }

        public FullListView(Context context, AttributeSet attrs) {

            super(context, attrs);

        }

        @Override

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

            int expandSpec = MeasureSpec.makeMeasureSpec(

                    Integer.MAX_VALUE >> 2,     // size 控件可以显示的最大高度

                    MeasureSpec.AT_MOST);      //

            super.onMeasure(widthMeasureSpec, expandSpec);

        }

    }

     对LinearLayout而言比较简单,由于 android:layout_width="match_parent",因此其width对应地widthSpec

      mode值为MeasureSpec.EXACTLY

    由于android:layout_width="wrap_content" , 因此其height对应地widthSpec mode值为

    MeasureSpec.AT_MOST

    推荐第二种方法   第一种方法的话如果布局不是linearlayout的话就会发生异常 

  • 相关阅读:
    PHP WAMP关闭notice等提示
    PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM
    Servlet 各种path路径比较
    数据库一对一的两种关联 主键关联和外键关联
    Java Timer定时器
    VS2010彻底卸载
    VS2010每次编译都重新编译整个工程的解决方案
    DbgPrint/KdPrint输出格式控制
    error TRK0002: Microsoft Visual Studio 10.0VCinlink.exe Access is denied.
    WinDbg F9时“code not found breakpoint not set”
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4830719.html
Copyright © 2011-2022 走看看