Android关于自定义ExpandableListView样式

创建项目:ExpandableListView

运行项目效果:

布局文件

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" ><ExpandableListView     android:id="@id/android:list"    android:layout_width="match_parent"    android:layout_height="match_parent"    style="@style/myExpandableListView"    android:drawSelectorOnTop="false"    /><TextView    android:id="@id/android:empty"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="no data"    /></LinearLayout>

第一级条目布局
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" >    <TextView         android:id="@+id/groupTo"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingLeft="40px"        android:paddingTop="3px"        android:paddingBottom="3px"        android:textSize="26sp"         android:text="No data"        /></LinearLayout>


第二级条目布局

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="vertical" >    <TextView         android:id="@+id/childTo"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingLeft="50px"        android:paddingTop="5px"        android:paddingBottom="5px"        android:textSize="20sp"        android:text="No data"/></LinearLayout>


在drawable-mdpi中定义my_expander_group.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/ic_picture" ></item></selector>


MainActivity.java

package org.wwj.view;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ExpandableListActivity;import android.os.Bundle;import android.widget.SimpleExpandableListAdapter;public class MainActivity extends ExpandableListActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //定义一个List,该List对象为一级条目提供数据        List<Map<String, String>> groups = new ArrayList<Map<String,String>>();        Map<String, String> group1 = new HashMap<String, String>();        group1.put("group", "第一组");        Map<String, String> group2 = new HashMap<String, String>();        group2.put("group", "第二组");        groups.add(group1);        groups.add(group2);              //定义一个List,该List对象为第一个一级条目提供二级条目的数据        List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();        Map<String, String> child1data1 = new HashMap<String, String>();        child1data1.put("child", "第一条");        child1.add(child1data1);        Map<String, String> child1data2 = new HashMap<String, String>();        child1data2.put("child", "第二条");        child1.add(child1data2);                //定义一个List,该List对象为第二个一级条目提供二级条目的数据        List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();        Map<String, String> child2data1 = new HashMap<String, String>();        child2data1.put("child", "第三条");        child2.add(child2data1);        Map<String, String> child2data2 = new HashMap<String, String>();        child2data2.put("child", "第四条");        child2.add(child2data2);                        //定义一个List,该List对象存储所有的二级条目的数据        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();        childs.add(child1);        childs.add(child2);                //生成一个SimpleExpandableListAdapter对象//1.context//2.一级条目的数据//3.用来设置一级条目样式的布局文件//4.指定一级条目数据的key//5.指定一级条目数据显示控件的id//6.指定二级条目的数据//7.用来设置二级条目样式的布局文件//8.指定二级条目数据的key//9.指定二级条目数据显示控件的id        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this        , groups        , R.layout.group        , new String[]{"group"}        , new int[] {R.id.groupTo}        , childs        , R.layout.child        , new String[]{"child"}        , new int[] {R.id.childTo}        );      //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity        setListAdapter(adapter);            }}


更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. Android(安卓)通过WebView 调用Js sqlite数据库
  4. android imageswitcher gallery 根据数据库内图片名字进行查看/
  5. 数据短信接收
  6. android sqlcipher 速度太慢,正确使用sqlcipher for Android
  7. Android串口开发,基于官方的android-serialport-api增加支持设置
  8. content provider
  9. android SharedPreferences(供选项使用)

随机推荐

  1. Android(安卓)ToggleButton控件的用法
  2. 重装Ubuntu13.04后需要做的事情
  3. 日拱一卒(十三)
  4. android 保存配置文档
  5. 《android上传图片》
  6. andorid 自定义seekbar
  7. Android代码实现APK文件的安装与卸载
  8. unity 关闭弹出android 提示框
  9. Android(安卓)使用netty框架实现socket通
  10. Flutter 打包 Could not get resource