Android笔记之ExpandableListView

1、布局文件:

(1)主布局:放置一个ExpandableListView组件

(2)每一行的Item布局,child_listitem.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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

2、代码清单:

主代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class fragment_2 extends Fragment {

    MyExAdapter myadapter;// 自定义的BaseExpandableListAdapter
    ExpandableListView exList;// 可扩展的ListView
    List<Map<String, Object>> groupData = null;// 这里用Object泛型,可能会有图片数据
    List<List<Map<String, Object>>> childData = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_2, container, false);
        exList = (ExpandableListView) v.findViewById(R.id.list);
        myadapter = new MyExAdapter(getActivity());// 实例化适配器,传context参数
        childData = new ArrayList<List<Map<String, Object>>>();// 初始化子列表的数据集
        groupData = new ArrayList<Map<String, Object>>();// 初始化父列表的数据集

//这个for循环的3为大组的数量
for (int i = 0; i < 3; i++) { // 每个list是一个组的多个行的内容,这里有3个组 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // 每一个map是一个子列表的一行的内容(包括文本,图标) for (int j = 0; j < 5; j++) { Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("title", "路人" + j); map1.put("icon", this.getResources().getDrawable(R.drawable.ic_launcher)); list.add(map1); } childData.add(list); } String[] strings = { "常用联系人", "我的同学", "我的老师" }; for (String string : strings) { // 开始填充父列表数据 // 每一个map是一个父列表的一行的内容 Map<String, Object> map = new HashMap<String, Object>(); map.put("title", string); groupData.add(map); } // childData.size()=groupData.size() myadapter.setChildData(childData); myadapter.setGroupData(groupData); exList.setAdapter(myadapter); exList.setGroupIndicator(null);// 不设置大组指示器图标,因为我们自定义设置了 exList.setDivider(null);// 设置图片可拉伸的 return v; } }

适配器MyExAdapter

 

用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:

  settingLists.setGroupIndicator(null);  ~~~~~~~~~~~~~~~~~此处就是设置自定义的箭头图标的。置空则没有了。

也可以自定义(但是位置还是在那个地方不推荐)如下:

首先,自定义一个expandablelistviewselector.xml文件,具体内容如下:
Java代码

<?xml version="1.0" encoding="utf-8"?>   

<selector xmlns:android="http://schemas.android.com/apk/res/android">   

    <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />   

    <item android:drawable="@drawable/expandablelistviewindicator" />   

</selector>  

加一句代码如下:

settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector)); 

大功告成

 

To be continued!

转载于:https://www.cnblogs.com/xingyyy/p/3389611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值