Android编程笔记 GridView、ListView和ScrollView的嵌套冲突

写项目遇到GridView和上边的一部分作为整体,能够满足上下滑动,我就自然地想到了ScrollView嵌套GridView,但测试的时候发现GridView显示不全,很令人郁闷,上网查了查资料,发现这是ScrollView嵌套Gridview和Listview的通病,他们都带有滚动条,高度计算就会出现错误,下面一起来讨论GridView、ListView和ScrollView的嵌套问题

解决的核心就是重写GridView和ListView的onMeasure()方法
下面我们详细地讨论 
GridView和ScrollView的嵌套冲突
简单的就是自定义MyGridView 重写onMeasure方法
public class MyGridView extends GridView{

	public MyGridView(Context context) {
		super(context);
	}
	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public MyGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int myMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, myMeasureSpec);
	}

}
XML代码如下
  <pre name="code" class="html"><ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true" >

        <RelativeLayout
            android:id="@+id/contentlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:id="@+id/lay_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bj"
                android:gravity="center_vertical" >

                <ImageView
                    android:id="@+id/btn_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_margin="5dp"
                    android:background="@drawable/btn_search_seletor" />

                <EditText
                    android:id="@+id/et_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_toLeftOf="@id/btn_search"
                    android:background="@drawable/edit_text_bj" >

                    <requestFocus />
                </EditText>
            </RelativeLayout>

            <com.example.custom.MyGridView
                android:id="@+id/gridview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/lay_search"
                android:numColumns="4"
                android:stretchMode="columnWidth"
                android:verticalSpacing="20dp" >
            </com.example.custom.MyGridView>
        </RelativeLayout>
    </ScrollView>
 
 

MainAty.java代码如下

mGridView = (MyGridView) view.findViewById(R.id.gridview);
searchLayout = view.findViewById(R.id.lay_search);
imgs = new int[]{R.drawable.tu1,R.drawable.tu2,R.drawable.tu3,R.drawable.tu4,R.drawable.tu5,R.drawable.tu6,R.drawable.tu7,R.drawable.tu8,Rdrawable.tu11,R.drawable.tu9};
		ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String,Object>>();
		for (int i = 0; i < 10; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("imgs", imgs[i]);
			item.add(map);
		}
		 
		SimpleAdapter adapter = new SimpleAdapter(getActivity(), item, R.layout.gridview_item, new String[]{"imgs"}, new int[]{R.id.img});
		mGridView.setAdapter(adapter);
	scrollView.requestChildFocus(searchLayout, null);//GridView的上边搜索部分

这样冲突就解决了

效果图




ListView和ScrollView的冲突

原理和GridView相同 这里就不再赘述

ps:上述中的

scrollView.requestChildFocus(searchLayout, null) 是把焦点给了搜索框,防止GridView上来就获得焦点,把搜索框顶上去


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值