最近想实现一个带有标题的gridview分组,好多要用GridView添加HeaderView来实现,本人才开始学习这个,感觉有部分理解困难,所以想用的简单点,网上又找了好多例子,发现可以用ExpandableListView+GridView实现分组数据展示,但是要注意克服一个问题即:因为ExpandableListView带滑动,Gridview也带滑动,二个嵌套在一起如果不做任何处理的话就GridView只会显示一行,楼主可以试下,将Gridview高度写成最大值,可以让Gridview不滑动,但是会影响GridView适配器getView方法的调用,当图片太多时就会出现问题.终于让我给找到办法了!想要的效果图
解决思想:继承GridView设置失去滚动效果,使用ExpandableListView去掉展示图标,设置其一直展示
示例代码:
自定义的Gridview,我尝试重写了dispatchTouchEvent但是没有效果,所以也不知道原因,毕竟刚学大神见谅。
public class ExpandableGridview extends GridView {
public ExpandableGridview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO 自动生成的构造函数存根
}
public ExpandableGridview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO 自动生成的构造函数存根
}
public ExpandableGridview(Context context) {
super(context);
// TODO 自动生成的构造函数存根
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
继承BaseExpandableListAdapter的类,因为我用的Gridview都是自定义的,你们也可以自己定义,所以getView方法就不在一一说明,大家都用过BaseAdapter。还有这个getChildrenCount方法,因为我在Gridview的适配器中用了其他指定数据,这里的返回值必须是1,不然,定义的返回数字是几,那么这个gridView中的内容会重复适配器的内容几遍。估计这个方法是方便定义Textview那一类控件而使用的。
</pre><p></p><p><span style="font-size:14px"><span style="font-size:14px"></span></span></p><p><span style="font-size:14px"><span style="font-size:14px"></span></span></p><pre name="code" class="java">public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private int position;
private Context context;
//这里是一个数组在这个adapter中作用不明显,主要是为了我接下来的分组方便
private String[] names = ResourceTools.getTiammuString();
//分组信息的展示内容,这里我用的空数据,
private String[][] namess = new String[names.length][6];
public ExpandableListViewAdapter(int position, Context context) {
this.position = position;
this.context = context;
}
//分的组数目
@Override
public int getGroupCount() {
// TODO 自动生成的方法存根
return names.length;
}
//每组下的子view数目
@Override
public int getChildrenCount(int groupPosition) {
// TODO 自动生成的方法存根
// return namess[groupPosition].length;
return 1;
}
//组命名
@Override
public Object getGroup(int groupPosition) {
// TODO 自动生成的方法存根
return names[groupPosition];
}
//值view内容,因我用的空数据,在这里并没有意义
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO 自动生成的方法存根
return namess[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO 自动生成的方法存根
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO 自动生成的方法存根
return childPosition;
}
//允许刷新数据的,其实我也不太清楚
@Override
public boolean hasStableIds() {
// TODO 自动生成的方法存根
return true;
}
public ExpandableGridview getGridView() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ExpandableGridview contentgrid = new ExpandableGridview(context);
contentgrid.setLayoutParams(params);
contentgrid.setHorizontalSpacing(1);
contentgrid.setVerticalSpacing(1);
contentgrid.setNumColumns(3);
contentgrid.setGravity(Gravity.CENTER);
return contentgrid;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout contentView = new LinearLayout(context);
contentView.setLayoutParams(params);
contentView.setOrientation(LinearLayout.VERTICAL);
TextView contentname = new TextView(context);
contentname.setLayoutParams(params);
contentname.setPadding(0, 20, 0, 0);
contentname.setTextColor(context.getResources().getColor(
R.color.lighttdark));
contentname.setTextSize(14);
contentname.setText(names[position]);
contentView.addView(contentname);
return contentname;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
GridviewAdapter adapter = new GridviewAdapter(context, names[position]);
ExpandableGridview gridView = getGridView();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String namess = (String) parent.getItemAtPosition(position);
Toast.makeText(context, "你选择了" + namess, Toast.LENGTH_SHORT)
.show();
}
});
return gridView;
}
//子view的点击事件,因为我调用的是Gridview,他有自己的点击事件所以,这里返回true就可以了
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO 自动生成的方法存根
return true;
}
}接下来的gridView适配器类就不在说明了,大家都知道的。
再有就是在在activity中设置分组图标,组设置展开。
layout.setGroupIndicator(null);
<pre name="code" class="java">int groupCount = layout.getCount();
for (int i=0; i<groupCount; i++) {
layout.expandGroup(i);
};
layout.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO 自动生成的方法存根
return true;
}
});
本文介绍了如何使用ExpandableListView结合GridView来实现带有标题的分组数据展示。在实现过程中,遇到了GridView只显示一行的问题,通过设置GridView的最大高度避免滑动,但导致了GridView适配器问题。最终解决方案是继承GridView并去除滚动效果,同时调整ExpandableListView的设置,实现了所需的效果。
1273

被折叠的 条评论
为什么被折叠?



