android 高级之旅 (六)ExpandableListView 使用详解

ExpandableListView和ListView有很多相似之处,E可以实现qq那样的下拉列表。如效果图
效果图

布局代码:

    <ExpandableListView
        android:layout_width="match_parent"
        android:id="@+id/expandable_lv"
        android:listSelector="#ffbaaa"
        android:background="#ffffff"
        android:cacheColorHint="#c92c2c"
        android:layout_height="match_parent">
    </ExpandableListView>

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:layout_width="match_parent"
        android:id="@+id/group_tv"
        android:gravity="center"
        android:text="1122"
        android:layout_height="48dp"/>
</LinearLayout>

child.xml

<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/child_iv"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:padding="6dp"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="111111111"/>

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="111111111"/>

        <TextView
            android:id="@+id/tv3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="111111111"/>


    </LinearLayout>

</LinearLayout>

MianActivity

public class MainActivity extends AppCompatActivity {

    private LayoutInflater inflater;
    private ExpandableListView exLv;
    private List<StrBean> groupList;
    private List<ChildBean> childList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exLv = (ExpandableListView) findViewById(R.id.expandable_lv);
        inflater = LayoutInflater.from(MainActivity.this);
        groupList = new ArrayList<>();
        childList = new ArrayList<>();
        initData();
//        exLv.setBottom(10);

        exLv.setAdapter(new MyAdapter(groupList,MainActivity.this));

    }

    private void initData() {
        for (int i = 0; i < 5; i++) {
            StrBean strBean = new StrBean();
            strBean.setChildSize(8);
            strBean.setStrName("深航"+i);
            for (int j = 0; j < 8; j++) {
                ChildBean childBean = new ChildBean();
                childBean.setPrice(1+j+"万");
                childBean.setSecondPrice(1+"万");
                childBean.setTitle("商务卡");
                childList.add(childBean);
            }
            strBean.setList(childList);
            groupList.add(strBean);
        }

    }
}

最重要的 adpter

public class MyAdapter extends BaseExpandableListAdapter {
    private List<StrBean> groupList;
    Context context;
    private Object childHolder;

    public MyAdapter(List<StrBean> groupList, Context context) {
        this.groupList = groupList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList.get(groupPosition).getChildSize();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        View view = null;
        GroupHolder groupHolder = null;
        if (convertView != null) {
            view = convertView;
            groupHolder = (GroupHolder) view.getTag();
        } else {
            view = View.inflate(context, R.layout.group, null);
            groupHolder = new GroupHolder();
            groupHolder.exTv = (TextView) view.findViewById(R.id.group_tv);
            view.setTag(groupHolder);
        }
        groupHolder.exTv.setText(groupList.get(groupPosition).getStrName());
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = null;
        ChildHolder childHolder = null;
        if (convertView != null) {
            view = convertView;
            childHolder = (ChildHolder) view.getTag();
        } else {
            view = View.inflate(context, R.layout.child, null);
            childHolder = new ChildHolder();
            childHolder.iv = (ImageView) view.findViewById(R.id.child_iv);
            childHolder.priceTv = (TextView) view.findViewById(R.id.tv1);
            childHolder.scPriceTv = (TextView) view.findViewById(R.id.tv2);
            childHolder.titleTv = (TextView) view.findViewById(R.id.tv3);
            view.setTag(childHolder);

        }
        childHolder.scPriceTv.setText(groupList.get(groupPosition).getList().get(childPosition).getSecondPrice());
        childHolder.titleTv.setText(groupList.get(groupPosition).getList().get(childPosition).getTitle());
        childHolder.priceTv.setText(groupList.get(groupPosition).getList().get(childPosition).getPrice());
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder{
        TextView exTv;
    }
    class ChildHolder{
        ImageView iv;
        TextView priceTv;
        TextView scPriceTv;
        TextView titleTv;
    }
}

前面介绍过ExpandableListView与ListView类似,所以ListView Adapter中存在的方法,ExpandableListView Adapter必定存在,只是Group和Child分别重写了ListView Adapter中的方法,同时新增加了两个方法,分别是hasStableIds() 和isChildSelectable(int groupPosition, int childPosition),所以,ExpandableListView Adapter中总共重写了10个方法。
其中要注意hasStableIds() 和isChildSelectable(int groupPosition, int childPosition)这两个方法。hasStableIds() 主要是用来判断ExpandableListView内容id是否有效的(返回true or false),系统会跟据id来确定当前显示哪条内容,也就是firstVisibleChild的位置。而isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。

关于expandablelistview的点击事件,直接添加点击事件即可。需要注意的是:

  1. 当设置setOnGroupClickListener监听并让其返回true时,所有Group消费点击事件,事件均不能分发传递给child(换言之,设置setOnChildClickListener不起任何作用)。

2.默认设置完Group以及child,Group左边会默认有以上下切换的图标,假如你有强迫症可以通过mListView.setGroupIndicator(null)去除。

3.前面已经简单说明了isChildSelectable(int groupPosition, int childPosition)方法的作用,所以当我们需要child可点击时,必须将setOnGroupClickListener和isChildSelectable对应设置为false和true。

4.说到这里也许读者会问,到底还是没有说出图一的做法。现在对图一进行详细的介绍。图一主要是将Group设置为不能收缩并且使其默认展开(即设置setOnGroupClickListener返回true,并且添加源码中setAdapter后三行代码)。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值