您的位置:首页 > 其它

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的冲突上面一样可以解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: