通过折叠expandList实现一个可以实现页面跳转的目录功能
1.设计实体bean,
由于Intent跳转传递的是a.class 类型,故设计了一个属性为Class
2.adapter
扩展BaseExpandableListAdpter实现ExpandableAdapter
注意1:重写isChildSelectable()方法需要返回true,不然不会触发 子Item的点击事件
3.item 中,单层的layout_marginTop实效,需要换成padding_top或者在外边再嵌套一层。
https://blog.csdn.net/achilles12345/article/details/47803233
实体bean结构如下:
public class ExChdBean {
int icon;
String name;
String note;
Class nameClass;
public ExChdBean(int icon, String name, String note, Class nameClass) {
this.icon = icon;
this.name = name;
this.note = note;
this.nameClass = nameClass;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Class getNameClass() {
return nameClass;
}
public void setNameClass(Class nameClass) {
this.nameClass = nameClass;
}
}
public class ExpandListActivityextends AppCompatActivity {
ArrayListparentList=new ArrayList<>();
ArrayList>childList =new ArrayList>();
ExpandableListViewexpandableListView;
MyExpandAdapteradapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expand_list);
initData();
expandableListView=findViewById(R.id.expand_list);
adapter=new MyExpandAdapter(ExpandListActivity.this,parentList,childList);
expandableListView.setAdapter(adapter);
dealManager();
}
void initData(){
//父级菜单初始化
parentList.add(new ExParBean(1,"图形"));
parentList.add(new ExParBean(2,"基本控件"));
parentList.add(new ExParBean(3,"列表控件"));
parentList.add(new ExParBean(4,"自定义控件"));
//子级菜单初始化
ArrayList list=new ArrayList<>();
list.add(new ExChdBean(R.drawable.come_111_se_icon,"图形1","note1",CardViewActivity.class));
list.add(new ExChdBean(R.drawable.come_111_se_icon,"图形1","note1",CardViewActivity.class));
list.add(new ExChdBean(R.drawable.come_111_se_icon,"图形1","note1",CardViewActivity.class));
childList.add(list);
ArrayList list2=new ArrayList<>();
list2.add(new ExChdBean(R.drawable.come_112_se_icon,"CardView","android5.0",CardViewActivity.class));
list2.add(new ExChdBean(R.drawable.come_113_se_icon,"基本控件2","note1",CardViewActivity.class));
list2.add(new ExChdBean(R.drawable.come_114_se_icon,"基本控件3","note1",CardViewActivity.class));
childList.add(list2);
ArrayList list3=new ArrayList<>();
list3.add(new ExChdBean(R.drawable.come_112_se_icon,"ExpandList","",CardViewActivity.class));
list3.add(new ExChdBean(R.drawable.come_113_se_icon,"列表控件","note1",CardViewActivity.class));
list3.add(new ExChdBean(R.drawable.come_114_se_icon,"列表控件","note1",CardViewActivity.class));
childList.add(list3);
ArrayList list4=new ArrayList<>();
list4.add(new ExChdBean(R.drawable.come_112_se_icon,"自定义进度条","",ProgressActivity.class));
list4.add(new ExChdBean(R.drawable.come_113_se_icon,"自定义圆环","",ViewActivity.class));
childList.add(list4);
}
void dealManager(){
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(ExpandListActivity.this,parentList.get(groupPosition).getParname(),Toast.LENGTH_SHORT).show();
Boolean isExpand=expandableListView.isGroupExpanded(groupPosition);
if (!isExpand){
expandableListView.expandGroup(groupPosition);
adapter.notifyDataSetChanged();
}
return true;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Boolean isExpand=expandableListView.isGroupExpanded(groupPosition);
if (!isExpand){
expandableListView.expandGroup(groupPosition);
adapter.notifyDataSetChanged();
}
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent=new Intent(ExpandListActivity.this,childList.get(groupPosition).get(childPosition).getNameClass());
startActivity(intent);
return true;
}
});
}
adapter中的实现:
public class MyExpandAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<ExParBean> parlist;
ArrayList<ArrayList<ExChdBean>> chdlist;
public MyExpandAdapter() {
}
public MyExpandAdapter(Context context, ArrayList<ExParBean> parlist, ArrayList<ArrayList<ExChdBean>> chdlist) {
this.context=context;
this.parlist=parlist;
this.chdlist=chdlist;
}
@Override
public int getGroupCount() {
return (parlist!=null)&&parlist.size()>0?parlist.size():0;
}
@Override
public int getChildrenCount(int groupPosition) {
return (chdlist!=null)&&chdlist.get(groupPosition).size()>0?chdlist.get(groupPosition).size():0;
}
@Override
public Object getGroup(int groupPosition) {
return parlist.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return chdlist.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ParViewHolder parViewHolder;
if (convertView==null){
parViewHolder=new ParViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.expend_parant_item,parent,false);
parViewHolder.tv_index=convertView.findViewById(R.id.tv_par_index);
parViewHolder.tv_parname=convertView.findViewById(R.id.tv_par_name);
convertView.setTag(parViewHolder);
}
else {
parViewHolder=(ParViewHolder)convertView.getTag();
}
parViewHolder.tv_index.setText(String.valueOf(parlist.get(groupPosition).getParindex()));
parViewHolder.tv_parname.setText(parlist.get(groupPosition).getParname());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChdViewHoider chdViewHoider;
if (convertView==null){
chdViewHoider=new ChdViewHoider();
convertView=LayoutInflater.from(context).inflate(R.layout.expand_child_item,parent,false);
chdViewHoider.img_icon=convertView.findViewById(R.id.img_icon);
chdViewHoider.tv_chdname=convertView.findViewById(R.id.tv_child_name);
chdViewHoider.tv_chdnote=convertView.findViewById(R.id.tv_child_note);
convertView.setTag(chdViewHoider);
}
else {
chdViewHoider=(ChdViewHoider)convertView.getTag();
}
chdViewHoider.img_icon.setImageResource(chdlist.get(groupPosition).get(childPosition).getIcon());
chdViewHoider.tv_chdname.setText(chdlist.get(groupPosition).get(childPosition).getName());
if (!TextUtils.isEmpty(chdlist.get(groupPosition).get(childPosition).getNote())){
chdViewHoider.tv_chdnote.setText(chdlist.get(groupPosition).get(childPosition).getNote());
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class ParViewHolder{
TextView tv_index;
TextView tv_parname;
}
class ChdViewHoider{
ImageView img_icon;
TextView tv_chdname;
TextView tv_chdnote;//注释
}
}
网友评论