Janrone's Blog

Janrone's Blog 我的自由地 https://janrone.allone.top

android学习——ExpandableListView控件

ExpandableListView  可扩展的ListView

xml

main.xml

<?xml version="1.0" encoding="utf-8"?>
<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_width="fill_parent"
   android:layout_height="wrap_content" 
   android:layout_weight="1"
   android:drawSelectorOnTop="false"
   />
   <TextView 
   		android:id="@id/android:empty"
   		android:layout_width="fill_parent"
   		android:layout_height="wrap_content"
   		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="fill_parent" android:layout_height="fill_parent">
	
	<TextView 
		android:id="@+id/group_text"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent"
		android:textSize="26sp"
		android:paddingLeft="40dip"
		android:paddingTop="3dip"
		android:paddingBottom="3dip"
		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="fill_parent" android:layout_height="fill_parent">
	
	<TextView 
		android:id="@+id/chile_text"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent"
		android:textSize="20sp"
		android:paddingLeft="60dip"
		android:paddingTop="3dip"
		android:paddingBottom="3dip"
		android:text="No data"/>
</LinearLayout>

java

public class HelloExpandableListView extends ExpandableListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        
        //创建父组组数据
        List<Map<String,String>> groups=new ArrayList<Map<String,String>>();
        
        Map<String,String> groupOne=new HashMap<String, String> ();
        groupOne.put("group", "groupData1");
        
        Map<String,String> groupTwo=new HashMap<String, String> ();
        groupTwo.put("group", "groupData2");
        
        groups.add(groupOne);
        groups.add(groupTwo);
        
        //第一组列表的数据
        List<Map<String, String>> groupOneData=new ArrayList<Map<String,String>>();
        Map<String, String> groupOneChildOne=new HashMap<String, String>();
        groupOneChildOne.put("child", "childOneData1");
        
        Map<String, String> groupOneChildTwo=new HashMap<String, String>();
        groupOneChildTwo.put("child", "childOneData2");
        
        groupOneData.add(groupOneChildOne);
        groupOneData.add(groupOneChildTwo);
        
        //第二组的列表数据
        List<Map<String, String>> groupTwoData=new ArrayList<Map<String,String>>();
        Map<String,String> groupTwoChildOne=new HashMap<String, String>();
        groupTwoChildOne.put("child", "childTwoData1");
        
        Map<String,String> groupTwoChildTwo=new HashMap<String, String>();
        groupTwoChildTwo.put("child", "childTwoData2");
        
        groupTwoData.add(groupTwoChildOne);
        groupTwoData.add(groupTwoChildTwo);
        
        //子数据的list集合
        List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
        childs.add(groupOneData);
        childs.add(groupTwoData);
        
        //new String[]{} 数组对应 Map中的key
		SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
				this, groups, R.layout.group, new String[] { "group" },
				new int[] { R.id.group_text }, childs,R.layout.child,
				new String[] { "child" }, new int[] { R.id.chile_text });
		//给ExpandableListView设置Adapter
		setListAdapter(adapter);
    }
}

阅读更多

扫码向博主提问

去开通我的Chat快问

janronehoo

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Android
版权声明:欢迎转载,附带链接。 https://blog.csdn.net/janronehoo/article/details/6889092
个人分类: Android学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭