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



【需求】:
如上面图示
当点开某个一级菜单的时候,其他菜单收起;
子级菜单默认最多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实现展开更多和收起更多的更多相关文章
- javascript 特效实现(1)——展开选项和收起效果
知识点:javascript事件:判断当前展开收起状态:延迟执行setTimeout方法. 1.简单的展开和收起效果: 1.1 静态结构HTML代码分析 body包含最外层的div id=" ...
- android之ExpandableListView 无法展开
1.Button 对,没错,就是这个button组件,不知道出现都少次过问题,很多都是它造成的! 最常见的问题:ExpandableListView无法展开,OnItemClickListener不响 ...
- WPF中TreeView单击展开其子元素以及点击一个元素展开其他元素收起
TreeView单击展开其子元素: 在WPF的TreeView控件中,要想展开它的子元素,我们必须要鼠标左键点两下或者右键点一下,那么我们怎样实现左键点一下就使它展开呢? Xaml: <Grid ...
- avalon.js 文字显示更多与收起
isShowMore: function (content) { if(content && content.length >= 124){ var shortContent = ...
- 点击更多button显示更多数据的功能实现思路代码
此功能是根据自己思路去慢慢做出来的,做的不够专业,希望有懂这个的前辈给自己指点指点. //分界线———————————————————————————————————————————————————— ...
- vue实现点击展开,点击收起
安装vue的步骤在这里就不进行赘述了,下面直接进入正题 首先定义一下data里面的数据: data () { return { toLearnList:[ 'html','css','javascri ...
- expandablelistView 可展开的列表
这个东西用法基本固定,不知道能不能做三级的展开. 界面代码 <?xml version="1.0" encoding="utf-8"?> <L ...
- 微信小程序——收起和查看更多功能
项目中做一些列表的时候,可能会需要做到 查看更多 及 收起功能,如下图所示: 大概的需求就是默认只显示2条,点击[查看更多]显示全部,点击[收起]还原. 实现的方法千万种.我来讲一下我的实现思路: 1 ...
- Android开发之仿微信显示更多文字的View
最近开发需求中要模仿微信朋友圈文章的展开收起功能,网上找了找,发现都有问题,于是乎自己在前辈的基础上进行了一定量的修改,下边将源码贴出来供大家参考:1.主Activity布局文件就不粘贴了,很简单,就 ...
随机推荐
- css双飞翼布局
双飞翼布局是一种比较灵活的布局,始于淘宝UED,玉伯提出的,他着重介绍的是双飞翼栅格布局. 三列布局为"双飞燕"布局,可以把三栏比作一只鸟,main部分相当是于鸟的身体,而lef ...
- 开发该选择Blocks还是Delegates
前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...
- KnockoutJS 3.X API 第七章 其他技术(5) 使用其他事件处理程序
在大多数情况下,数据绑定属性提供了一种干净和简洁的方式来绑定到视图模型. 然而,事件处理是一个常常会导致详细数据绑定属性的领域,因为匿名函数通常是传递参数的推荐技术. 例如: <a href=& ...
- 今天不谈技术,说说一些常用的软件~By 逆天
前端工具:http://www.cnblogs.com/dunitian/p/5640147.html 在线办公: http://word.baidu.com/welcome.html http ...
- Sql Server系列:Microsoft SQL Server Management Studio模板资源管理器
模板资源管理器是Microsoft SQL Server Management Studio的一个组件,可以用来SQL代码模板,使用模板提供的代码,省去每次都要输入基本代码的工作. 使用模板资源管理器 ...
- jQuery源码分析系列(38) : 队列操作
Queue队列,如同data数据缓存与Deferred异步模型一样,都是jQuery库的内部实现的基础设施 Queue队列是animate动画依赖的基础设施,整个jQuery中队列仅供给动画使用 Qu ...
- 【求助】WPF 在XP下 有的Textbox光标会消失
最近做个项目,一直有一个问题没有解决,就是在XP下,有的Textbox里在文本框里没有东西的时候,会没有光标.不同的XP机器,失去光标的Textbox也不一样. 各位大师看下面的三张图,当Textbo ...
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
需要下载Genymotion-ARM-Translation_v1.1.zip ARM插件包 用于安装ARM架构的程序,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你 ...
- 追根溯源:EntityFramework 实体的状态变化
阅读目录: 1. 应用场景 2. 场景测试 3. 问题分析 4. 追根溯源 5. 简要总结 1. 应用场景 首先,应用程序使用 EntityFramework,应用场景中有两个实体 S_Class(班 ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...