Android之ExpandableListView控件
16lz
2021-01-26
code:
public class ExpandableListViewActivity extends ExpandableListActivity {private static final String TAG = "ExpandableListViewActivity";/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();Map<String, String> groupMap1 = new HashMap<String, String>();Map<String, String> groupMap2 = new HashMap<String, String>();groupMap1.put("parent", "家具");groupMap2.put("parent", "喜好");groupMap1.put("parent2", "家具2");groupData.add(groupMap1);groupData.add(groupMap2);List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();Map<String, String> childMap1 = new HashMap<String, String>();Map<String, String> childMap2 = new HashMap<String, String>();childMap1.put("child", "家具1111");childMap2.put("child", "喜好1111");child1.add(childMap1);child1.add(childMap2);List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();Map<String, String> childMap3 = new HashMap<String, String>();Map<String, String> childMap4 = new HashMap<String, String>();childMap3.put("child", "家具2222");childMap4.put("child", "喜好2222");child2.add(childMap3);child2.add(childMap4);childData.add(child1);childData.add(child2);SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groupData, R.layout.group,new String[] { "parent","parent2" }, new int[] { R.id.group , R.id.group2}, childData, R.layout.child,new String[] { "child" }, new int[] { R.id.child });setListAdapter(adapter);}/** * 默认选中的子目录 */@Overridepublic boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) {Log.e(TAG, "--------setSelectedChild");return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);}/** * 默认选中的父类目录 */@Overridepublic void setSelectedGroup(int groupPosition) {Log.e(TAG, "--------setSelectedGroup");super.setSelectedGroup(groupPosition);}/** * 子目录被点击的时候 */@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Log.e(TAG, "--------onChildClick");return super.onChildClick(parent, v, groupPosition, childPosition, id);}}
layout:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><ExpandableListView android:id="@id/android:list"android:layout_height="wrap_content" android:layout_width="match_parent"></ExpandableListView></LinearLayout>
child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:text="TextView" android:id="@+id/child"android:textSize="25sp" android:paddingLeft="35px" android:paddingTop="10px"android:paddingRight="5px" android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView></LinearLayout>
group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:text="TextView" android:id="@+id/group"android:textSize="25sp" android:paddingLeft="35px" android:paddingTop="10px"android:paddingRight="5px" android:paddingBottom="10px"android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView><TextView android:text="TextView" android:id="@+id/group2"android:textSize="25sp" android:paddingLeft="35px" android:paddingTop="10px"android:paddingRight="5px" android:paddingBottom="10px"android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView></LinearLayout>
效果图:
更多相关文章
- Android中使用OpenCV详细入门教程
- win32到android,missingfile导致的闪退
- Android+OTA+升级之一:编译升级包---make+otapackage
- 【Android(安卓)Studio】分类整理res/Layouts中的布局文件(创建
- 在Eclipse中加入Android源码
- How To Set An Android(安卓)Lock Screen PIN, Password or Unlo
- Android(安卓)SD卡中压缩包解压(ZIP文件)
- 【Android(安卓)系统开发】使用 Source InSight 阅读 Android(安
- Windows下 ndk 编译程序