码迷,mamicode.com
首页 > 其他好文 > 详细

ExpandableListView实现展开更多和收起更多

时间:2016-11-04 23:53:23      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:rem   app   tor   添加   adapter   处理   textview   pcl   apt   

技术分享

技术分享

技术分享

【需求】:

如上面图示

当点开某个一级菜单的时候,其他菜单收起;

子级菜单默认最多5个;

多于5个的显示“展开更多”

点击“展开更多”,展开该级所有子级菜单,同时显示“收起更多”

【代码】:

@Bind(R.id.exp_listview)
ExpandableListView expListview;
adapter = new MyAdapter1(dataBeans);
expListview.setDividerHeight(0);
expListview.setChildDivider(null);
expListview.setGroupIndicator(null);//去掉ExpandableListView 默认的箭头
expListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long
id) {
return false;//默认为false,设为true时,点击事件不会展开Group
}
});
expListview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
adapter.setMaxIs5(true);
for (int i = 0; i < expListview.getCount(); i++) {
//expListview.expandGroup(i);
if (i != groupPosition) {
expListview.collapseGroup(i);//折叠其他
}
}
adapter.notifyDataSetChanged();
}
});
expListview.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
adapter.setMaxIs5(true);
}
});
expListview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int
childPosition, long id) {
return false;
}
});
expListview.setAdapter(adapter);

class MyAdapter1 extends BaseExpandableListAdapter {

private List<Category.DataBean> dataBeans;
private boolean maxIs5=true;//子View最多5个
private View expand_more_view;//展开更多
private View collapse_more_view;//收起更多
// private View listviewItem;//
private boolean isExpandMore=false;
private boolean isCollapseMore=false;

public void setMaxIs5(boolean maxIs5) {
this.maxIs5 = maxIs5;
}

public MyAdapter1(List<Category.DataBean> dataBeans1) {
this.dataBeans = dataBeans1;
expand_more_view=UIUtils.getXmlView(R.layout.category_expand_more);
collapse_more_view=UIUtils.getXmlView(R.layout.category_collapse_more);
// listviewItem=UIUtils.getXmlView(R.layout.item_category_listview_item);
}

@Override
public int getGroupCount() {
return dataBeans == null ? 0 : dataBeans.size();
}

@Override
public int getChildrenCount(int groupPosition) {
int size = dataBeans.get(groupPosition).getSubs().size();
int size1=Math.min(size,5);
int size2=Math.max(size,size1);
if(maxIs5){
System.out.println("【getChildrenCount】:"+groupPosition+":"+size1);
return size1;
}else{
System.out.println("【getChildrenCount】:"+groupPosition+":"+size2);
return size2;
}
}

@Override
public Object getGroup(int groupPosition) {
return dataBeans.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
System.out.println("【getChild】:groupPosition="+groupPosition+",childPosition="+childPosition);
return dataBeans.get(groupPosition).getSubs().get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
System.out.println("【getGroupId】:"+groupPosition);
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
System.out.println("【getChildId】:groupPosition="+groupPosition+",childPosition="+childPosition);
return childPosition;
}

@Override
public boolean hasStableIds() {
//组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = UIUtils.getXmlView(R.layout.expand_header);
}
ImageView iv_category_item = (ImageView) convertView.findViewById(R.id
.iv_category_item);
x.image().bind(iv_category_item, dataBeans.get(groupPosition).getImg());
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View
convertView, ViewGroup parent) {
System.out.println("【getChildView】:"+childPosition);
// if (convertView == null) {
// convertView = UIUtils.getXmlView(R.layout.expand_content);
// }
// ListView lv_category_item = (ListView) convertView.findViewById(R.id.lv_category_item);
// MyAdapter2 adapter = new MyAdapter2(dataBeans.get(groupPosition).getSubs());
// lv_category_item.setAdapter(adapter);
// if (convertView == null) {
// convertView = listviewItem;
convertView = UIUtils.getXmlView(R.layout.item_category_listview_item);
// convertView= LayoutInflater.from(getActivity()).inflate(R.layout.item_category_listview_item,parent,false);
// }
TextView tv_catename = (TextView) convertView.findViewById(R.id.tv_catename);
RelativeLayout rl_category= (RelativeLayout) convertView.findViewById(R.id.rl_category);
List<Category.DataBean.SubsBean> list=dataBeans.get(groupPosition).getSubs();
final Category.DataBean.SubsBean subsBean =list.get(childPosition);
tv_catename.setText(subsBean.getCate_name());
rl_category.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UIUtils.toast("跳转到类别:" + subsBean.getCate_name() + ",【图片url】:" + subsBean
.getImg(), false);
// if (childPosition == 4) {
// maxIs5 = false;
// YouFanApplication.mHandler.post(new Runnable() {
// @Override
// public void run() {
// notifyDataSetChanged();
// }
// });
// }
}
});
//处理“展开更多”和“收起更多”
if(list.size()>5 && isLastChild){
final LinearLayout convertView1= (LinearLayout) convertView;
if(maxIs5){//添加展开更多布局
if(isExpandMore){
//convertView1.removeView(expand_more_view);
}
ViewGroup parent1 = (ViewGroup) expand_more_view.getParent();
if(parent1!=null){
parent1.removeAllViews();
}
convertView1.addView(expand_more_view);
//The specified child already has a parent. You must call removeView() on the child‘s parent first.
isExpandMore=true;
}else{//添加收起更多布局
if(isCollapseMore){
//convertView1.removeView(collapse_more_view);
}
ViewGroup parent2= (ViewGroup) collapse_more_view.getParent();
if(parent2!=null){
parent2.removeAllViews();
}
convertView1.addView(collapse_more_view);
isCollapseMore=true;
}

/*-------------------------------------------------------------*/
expand_more_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertView1.removeView(v);
//isCollapseMore=true;
//isExpandMore=false;
maxIs5 = false;
YouFanApplication.mHandler.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
});
/*-------------------------------------------------------------*/
collapse_more_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertView1.removeView(v);
//isExpandMore=true;
maxIs5 = true;
YouFanApplication.mHandler.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
});
/*-------------------------------------------------------------*/
return convertView1;
}

return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
//是否选中指定位置上的子元素
return true;
}
}

ExpandableListView实现展开更多和收起更多

标签:rem   app   tor   添加   adapter   处理   textview   pcl   apt   

原文地址:http://www.cnblogs.com/libuyi/p/6031702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!