一个activity里多个gridview布局显示问题

移动开发 码拜 4年前 (2017-04-14) 631次浏览
本人一个activity里放入2个GridView,每个GridView里都有很多数据。本人的目标是想只让ScrollView滚动,GridView的高度可以很高很高超过屏幕高度,但不滚动。但是按下面这样写完,发现原本应该显示很多行的GridView却只显示一行内容。本人的适配数据部分没错,原因是本人把两个GridView的高度假如写死成500dp,而不用wrap_content的话,就能正常显示全部内容,但当中原因是写死了高度,所以多出很多空白。问一下这个怎么解决呢?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="ecust.mlkz.act_MLKZ_HomePage">
    <include
        android:id="@+id/part_actionbar"
        layout="@layout/part_actionbar" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="跳蚤市场" />
            <GridView
                android:id="@+id/gv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="0dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="0dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="励志书院" />
            <GridView
                android:id="@+id/gv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="0dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="0dp" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
 ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<7;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
            map.put("ItemText", "aa"+String.valueOf(i));//按序号做ItemText
            lstImageItem.add(map);
        }
        //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
        SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
                lstImageItem,//数据来源
                R.layout.gv,//night_item的XML实现
                //动态数组与ImageItem对应的子项
                new String[] {"ItemImage","ItemText"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[] {R.id.gv_iv,R.id.gv_tv});
        GridView gv1=(GridView) findViewById(R.id.gv1);
        gv1.setAdapter(saImageItems);
        gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
                //显示所选Item的ItemText
                Toast.makeText(act_MLKZ_HomePage.this, "aa" + position, Toast.LENGTH_SHORT).show();
            }
        });
        ArrayList<HashMap<String, Object>> lstImageItem2 = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<30;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
            map.put("ItemText", "bb"+String.valueOf(i));//按序号做ItemText
            lstImageItem2.add(map);
        }
        //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
        SimpleAdapter saImageItems2 = new SimpleAdapter(this, //没什么解释
                lstImageItem2,//数据来源
                R.layout.gv,//night_item的XML实现
                //动态数组与ImageItem对应的子项
                new String[] {"ItemImage","ItemText"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[] {R.id.gv_iv,R.id.gv_tv});
        GridView gv2=(GridView) findViewById(R.id.gv2);
        gv2.setAdapter(saImageItems2);
        gv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
                //显示所选Item的ItemText
                Toast.makeText(act_MLKZ_HomePage.this, "bb" + position, Toast.LENGTH_SHORT).show();
            }
        });
解决方案

30

设置完adapter后,重新计算gridview的高度,并设置给它就OK。

public static void setGrideViewHeightBasedOnChildren(GridView grid) {
		ListAdapter adapter = grid.getAdapter();
		if (adapter == null) {
			return;
		}
		int totalHeight = 0;
		View listItem = adapter.getView(0, null, grid);
		listItem.measure(0, 0);
		if (adapter.getCount() - 1 < 0) {
			totalHeight = listItem.getMeasuredHeight();
		} else {
			int line = adapter.getCount() / 3;
			if (adapter.getCount() % 3 != 0)
				line = line + 1;
			totalHeight = (listItem.getMeasuredHeight() + 30) * line;
		}
		ViewGroup.LayoutParams params = grid.getLayoutParams();
		params.height = totalHeight + 30;
		((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
		grid.setLayoutParams(params);
	}

这个是本人本人根据需求写的,供参考。

10

重写一下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 expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明一个activity里多个gridview布局显示问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)