一、首先在AndroidStudio里创建个moudle(如果不想新创建项目,且自己的电脑配置扛得住的情况下)
二、在activity_main.xml里布局ExpandableListView
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="wrap_content">ExpandableListView> RelativeLayout>
三、创建父级目录item_group.xml
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#8BCF28" > <TextView android:id="@+id/tv_group" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:text="父目录" />
LinearLayout>
四、创建子级目录item_child.xml
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/child_img" android:src="@mipmap/ic_launcher" android:layout_width="30dp" android:layout_height="30dp" /> <TextView android:id="@+id/tv_child" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="子目录"/> 五、创建MyExpandableListView的优化类
package com.example.expandablelistview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyExpandableListView extends BaseExpandableListAdapter { Context context; private String [] groups; private String [][] childs; public MyExpandableListView(Context context, String[] groups, String[][] childs) { this.context = context; this.groups = groups; this.childs = childs; } @Override public int getGroupCount() { return groups.length; } @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; }
//填充父级目录 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder groupholder =null; if (convertView==null){ groupholder =new GroupHolder(); convertView=View.inflate(context,R.layout.item_group,null); groupholder.tv_group=convertView.findViewById(R.id.tv_group); convertView.setTag(groupholder); }else{ groupholder= (GroupHolder) convertView.getTag(); } groupholder.tv_group.setText(groups[groupPosition]); return convertView; }
//填充子级目录
@Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildHolder childholder=null; if (convertView==null){ childholder=new ChildHolder(); convertView=View.inflate(context,R.layout.item_child,null); childholder.child_img=convertView.findViewById(R.id.child_img); childholder.tv_child=convertView.findViewById(R.id.tv_child); convertView.setTag(childholder); }else { childholder= (ChildHolder) convertView.getTag(); }; //childholder.child_img.setImageBitmap( childholder.tv_child.setText(childs[groupPosition][childPosition]); return convertView;
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupHolder{ TextView tv_group; } class ChildHolder{ ImageView child_img; TextView tv_child; } }
五、Mainactivity修改
package com.example.expandablelistview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ExpandableListView; public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private String [] groups={"A","B","C"}; private String [][] childs={{"A1","A2","A3"},{"B1","B2","B3"},{"C1","C2","C3"}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView=findViewById(R.id.expandableListView); expandableListView.setAdapter(new MyExpandableListView(MainActivity.this,groups,childs)); } }