生活是一面镜子。你对它笑,它就对你笑;你对它哭,它也对你哭。
本讲内容:ExpandableListView 多级列表
下面我们通过一个例子感受一下
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/default_bg" tools:context="com.example.expandablelistview.MainActivity$PlaceholderFragment" > <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true"> </ExpandableListView> </RelativeLayout>
下面是res/layout/member_listview.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="40dp" android:layout_gravity="center_horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingTop="10dp" android:src="@drawable/user_group" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/tv01" android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="25sp" android:textColor="#fff" android:gravity="center_vertical" android:layout_gravity="center_vertical" android:paddingLeft="10dp"/> <ImageView android:id="@+id/tubiao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> </LinearLayout> </LinearLayout>
下面是res/layout/member_childitem.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:id="@+id/childlayout" android:orientation="horizontal" > <ImageView android:id="@+id/child_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/child_image" android:paddingTop="7dp" android:layout_marginLeft="40dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/child_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="17sp" android:layout_gravity="center_vertical" android:gravity="center_vertical"/> <TextView android:id="@+id/child_text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:layout_gravity="center_vertical" android:gravity="center_vertical"/> </LinearLayout> </LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity { private static final String G_TEXT = "g_text"; private static final String C_TEXT1 = "c_text1"; private static final String C_TEXT2 = "c_text2"; List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); ExAdapter adapter; ExpandableListView exList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("溪坎中學"); for (int i = 0; i < 3; i++) { Map<String, String> groupMap = new HashMap<String, String>(); groupData.add(groupMap); // groupMap.put(G_TEXT, "Group"+i); if (i == 0) { groupMap.put(G_TEXT, "一八班"); } else if (i == 1) { groupMap.put(G_TEXT, "二四班"); } else if (i == 2) { groupMap.put(G_TEXT, "三三班"); } List<Map<String, String>> children = new ArrayList<Map<String, String>>(); for (int j = 0; j < 5; j++) { Map<String, String> childMap = new HashMap<String, String>(); children.add(childMap); if (j == 0) { childMap.put(C_TEXT1, "丹妹"); childMap.put(C_TEXT2, "舍与得,留下的,珍惜。"); } else if (j == 1) { childMap.put(C_TEXT1, "四眼妹"); childMap.put(C_TEXT2, "太平洋保险,人寿保险,财产保险"); } else if (j == 2) { childMap.put(C_TEXT1, "冯开妥"); childMap.put(C_TEXT2, "哪一刻,心~停止跳动。"); } else if (j == 3) { childMap.put(C_TEXT1, "郑健美 "); childMap.put(C_TEXT2, "爱情就是赌局,谁先爱上谁,谁就输了,我只愿意输一次"); } else if (j == 4) { childMap.put(C_TEXT1, "黄小君"); childMap.put(C_TEXT2, "挤不进的世界,就别挤了,何苦为难别人,苦了自己。"); } } childData.add(children); } adapter = new ExAdapter(MainActivity.this); exList = (ExpandableListView) findViewById(R.id.list); exList.setAdapter(adapter); exList.setGroupIndicator(null); exList.setDivider(null); } class ExAdapter extends BaseExpandableListAdapter { Context context; public ExAdapter(Context context) { context = this.context; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.member_listview, null); } TextView title = (TextView) view.findViewById(R.id.tv01);// 记得加view否则报错 title.setText(getGroup(groupPosition).toString()); ImageView image = (ImageView) view.findViewById(R.id.tubiao); if (isExpanded) { image.setBackgroundResource(R.drawable.btn_browser2); } else { image.setBackgroundResource(R.drawable.btn_browser); } return view; } public long getGroupId(int groupPosition) { return groupPosition; } public Object getGroup(int groupPosition) { return groupData.get(groupPosition).get(G_TEXT).toString(); } public int getGroupCount() { return groupData.size(); } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.member_childitem, null); } final TextView title = (TextView) view.findViewById(R.id.child_text); title.setText(childData.get(groupPosition).get(childPosition).get(C_TEXT1).toString()); final TextView title2 = (TextView) view.findViewById(R.id.child_text2); title2.setText(childData.get(groupPosition).get(childPosition).get(C_TEXT2).toString()); return view; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public Object getChild(int groupPosition, int childPosition) { return childData.get(groupPosition).get(childPosition).get(C_TEXT1).toString(); } public int getChildrenCount(int groupPosition) { return childData.get(groupPosition).size(); } // ************************************** public boolean hasStableIds() { return true; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
本讲就到这里,Take your time and enjoy it
时间: 2024-11-10 14:12:06