标签:
ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候,可以将他的子item显示出来,像qq中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键是设置他的adapter,这个adapter必须继承BaseExpandableListAdapter类,所以实现运用ExpandableListView的核心就是学会继承BaseExpandableListAdapte类。
activity_main.xml
<RelativeLayout .....
xmlns:tools="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/expandable"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
item_parent.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" > <TextView android:id="@+id/parent_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"/ > </LinearLayout>
item_child.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/child_tv"
9 android:layout_width="match_parent"
10 android:layout_height="40dp"
11 android:layout_gravity="center"
12 android:gravity="center" />
13
14 </LinearLayout>
MainActivity.java:
public class MainActivity extends Activity { ExpandableListView mainlistview = null; List<String> parent = null; Map<String, List<String>> map = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainlistview = (ExpandableListView) this .findViewById(R.id.main_expandablelistview); initData(); mainlistview.setAdapter(new MyAdapter()); } // 初始化数据 public void initData() { parent = new ArrayList<String>(); parent.add("parent1"); parent.add("parent2"); parent.add("parent3"); map = new HashMap<String, List<String>>(); List<String> list1 = new ArrayList<String>(); list1.add("child1-1"); list1.add("child1-2"); list1.add("child1-3"); map.put("parent1", list1); List<String> list2 = new ArrayList<String>(); list2.add("child2-1"); list2.add("child2-2"); list2.add("child2-3"); map.put("parent2", list2); List<String> list3 = new ArrayList<String>(); list3.add("child3-1"); list3.add("child3-2"); list3.add("child3-3"); map.put("parent3", list3); } class MyAdapter extends BaseExpandableListAdapter { //得到子item需要关联的数据 @Override public Object getChild(int groupPosition, int childPosition) { String key = parent.get(groupPosition); return (map.get(key).get(childPosition)); } //得到子item的ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //设置子item的组件 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String key = MainActivity.this.parent.get(groupPosition); String info = map.get(key).get(childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.layout_children, null); } TextView tv = (TextView) convertView .findViewById(R.id.child_textview); tv.setText(info); return tv; } //获取当前父item下的子item的个数 @Override public int getChildrenCount(int groupPosition) { String key = parent.get(groupPosition); int size=map.get(key).size(); return size; } //获取当前父item的数据 @Override public Object getGroup(int groupPosition) { return parent.get(groupPosition); } @Override public int getGroupCount() { return parent.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } //设置父item组件 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = (LayoutInflater) MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.layout_parent, null); } TextView tv = (TextView) convertView .findViewById(R.id.parent_tv); tv.setText(MainActivity.this.parent.get(groupPosition)); return tv; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
标签:
原文地址:http://www.cnblogs.com/chhom/p/4898250.html