ExpandableListView 里面嵌套GridView实现高度自适应
2016-08-02 10:36
447 查看
很早之前做过一个商城的app 也是第一次做安卓。
实现的效果如下:
因为一开始做安卓,很多写的代码都不规范,在下面上代码的地方,还请高手指点(勿喷,楼主是自尊心很强的屌丝)
这个效果要解决2个大问题,
第一个是ExpandableListView 如何放置gridview ,这个比较好做 思路就是adapter里实现
第二个是在ExpandableListView里面展开后,GirdView如何能充满ExpandableListView的item, 就是楼上的效果
好,先来解决第一个。如何放置gridview,
在这里使用的adapter 是BaseExpandableListAdapter 并且实现点击girdview里面item的事件
Java代码
//适配器
class madapter extends BaseExpandableListAdapter implements OnItemClickListener {
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return alllist.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return alllist.get(groupPosition).parentnode;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView text = createView(getGroup(groupPosition).toString(),getContext());
return text;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup item = (ViewGroup)layoutInflater.inflate(R.layout.shops_allshops_type_grid, null); //载入gridview布局
grid = (ShowAllShopsType_list_grid) item.findViewById(R.id.shopstypegridview);// 获取girdview的节点
grid.setNumColumns(4);// 设置每行列数
grid.setGravity(Gravity.CENTER);// 位置居中
grid.setVerticalSpacing(10);
grid.setAdapter(gridAdapter(parent.getContext(),(int)getGroupId(groupPosition)));
grid.setOnItemClickListener(this);
grid.setVisibility(View.VISIBLE);
return item;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent =new Intent(getContext(),ShowAllShops.class);
intent.putExtra("text", ((TextView)view).getText());
//getContext().startActivity(intent);
shopactivty.setResult(1, intent);
shopactivty.finish();
}
}
代码只是提供思路的,详细的哪里不懂可以评论发
第二个问题是gridview的自适应
Java代码
public class ShowAllShopsType_list_grid extends GridView{
public ShowAllShopsType_list_grid(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置不滚动
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
这里只是重写了onMeasure 方法,这样的重写 在ScrollView里放置ListView的冲突上面一样可以解决。
实现的效果如下:
因为一开始做安卓,很多写的代码都不规范,在下面上代码的地方,还请高手指点(勿喷,楼主是自尊心很强的屌丝)
这个效果要解决2个大问题,
第一个是ExpandableListView 如何放置gridview ,这个比较好做 思路就是adapter里实现
第二个是在ExpandableListView里面展开后,GirdView如何能充满ExpandableListView的item, 就是楼上的效果
好,先来解决第一个。如何放置gridview,
在这里使用的adapter 是BaseExpandableListAdapter 并且实现点击girdview里面item的事件
Java代码
//适配器
class madapter extends BaseExpandableListAdapter implements OnItemClickListener {
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return alllist.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return alllist.get(groupPosition).parentnode;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView text = createView(getGroup(groupPosition).toString(),getContext());
return text;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup item = (ViewGroup)layoutInflater.inflate(R.layout.shops_allshops_type_grid, null); //载入gridview布局
grid = (ShowAllShopsType_list_grid) item.findViewById(R.id.shopstypegridview);// 获取girdview的节点
grid.setNumColumns(4);// 设置每行列数
grid.setGravity(Gravity.CENTER);// 位置居中
grid.setVerticalSpacing(10);
grid.setAdapter(gridAdapter(parent.getContext(),(int)getGroupId(groupPosition)));
grid.setOnItemClickListener(this);
grid.setVisibility(View.VISIBLE);
return item;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent =new Intent(getContext(),ShowAllShops.class);
intent.putExtra("text", ((TextView)view).getText());
//getContext().startActivity(intent);
shopactivty.setResult(1, intent);
shopactivty.finish();
}
}
代码只是提供思路的,详细的哪里不懂可以评论发
第二个问题是gridview的自适应
Java代码
public class ShowAllShopsType_list_grid extends GridView{
public ShowAllShopsType_list_grid(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置不滚动
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
这里只是重写了onMeasure 方法,这样的重写 在ScrollView里放置ListView的冲突上面一样可以解决。
相关文章推荐
- ScrollView嵌套ListView或GridView等,使得其高度自适应解决方案
- expandablelistview学习--在listView里面嵌套GridView
- expandablelistview学习--在listView里面嵌套GridView
- expandablelistview学习--在listView里面嵌套GridView
- ExpandableListView里面嵌套GridView的问题
- expandablelistview学习--在listView里面嵌套GridView
- expandablelistview学习--在listView里面嵌套GridView
- expandablelistview学习--在listView里面嵌套GridView
- ExpandableListView嵌套GridView实现购物标签栏
- 解决android expandablelistview 里面嵌入gridview行数据重复问题
- scrollview与listview、gridview嵌套高度冲突解决思路
- expandlistview嵌套gridview来实现功能
- ScrollView嵌套ListView,gridView只显示一行解决方案 (以及计算显示高度错误问题)
- 关于scrollview--listview(内部嵌套gridview等)的高度测量异常
- android关于scrollview嵌套ExpandableListView的实现1
- Android动态设定GridView的高度,固定column,实现高度自适应
- 避免在SrollView里面嵌套的ListView或者ExpandableListView无法全部展示数据
- Android动态设定GridView的高度,固定column,实现高度自适应
- android 在一个scrollView里面嵌套一个需要滑动的控件(listView、gridView)
- 动态设定GridView的高度,固定column,实现高度自适应