UI组件之ExpandableListView

来源:互联网 发布:火力银电淘宝哪家店有 编辑:程序博客网 时间:2024/05/18 01:23

UI组件之ExpandableListView

ExpandableListView是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用列表分为几组,每个组里有可以包含多个列表项。

ExpandableListView的整体思路

1.要给ExpandableListView设置适配器,必须先设置数据源。

2.数据源,就需要写一个适配器类去继承BaseExpandableListAdapter类

3.设置完数据源之后,直接给用ExpandableListView的setAdapter方法即可

demo

主界面布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <ExpandableListView        android:id="@+id/expand_listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></RelativeLayout>

父列表界面布局

<?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/image_group"        android:layout_width="50dp"        android:layout_height="50dp"        android:src="@mipmap/ic_launcher"        />    <TextView        android:id="@+id/tv_group"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

子列表界面布局

<?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:paddingLeft="30dp"    android:orientation="horizontal"    >    <ImageView        android:id="@+id/image_child"        android:layout_width="50dp"        android:layout_height="50dp"        />    <TextView        android:id="@+id/tv_child"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

适配器代码

package com.shake.day10_android_3.adapter;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;import com.shake.day10_android_3.R;/** * Created by Administrator on 2016/1/26. */public class MyAdapter extends BaseExpandableListAdapter {    private String[] groups;    private String[][] childs;    private int[][] images;    public MyAdapter(String[] groups, String[][] childs, int images[][]) {        this.groups = groups;        this.childs = childs;        this.images = images;    }    @Override    public int getGroupCount() {        return groups.length;    }    @Override    public int getChildrenCount(int groupPosition) {        return childs[groupPosition].length;    }    @Override    public Object getGroup(int groupPosition) {        return groups[groupPosition];    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return childs[groupPosition][childPosition];    }    @Override    public long getGroupId(int groupPosition) {        return groupPosition;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return childPosition;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group, parent, false);        }        TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);        ImageView image_group = (ImageView) convertView.findViewById(R.id.image_group);        tv_group.setText(groups[groupPosition]);        return convertView;    }    @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child, parent, false);        }        ImageView image_child = (ImageView) convertView.findViewById(R.id.image_child);        TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);        tv_child.setText(childs[groupPosition][childPosition]);        image_child.setImageResource(images[groupPosition][childPosition]);        return convertView;    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }}

Activity代码

package com.shake.day10_android_3;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.Toast;import com.shake.day10_android_3.adapter.MyAdapter;public class MainActivity extends AppCompatActivity implements ExpandableListView.OnChildClickListener {    private ExpandableListView listView;    private String[] groups = {"美女", "也是美女"};    private String[][] childs = {{"baby", "陈乔恩", "高圆圆", "龚璎菲"}, {"李小璐", "唐嫣", "杨幂", "姚笛"}};    private int[][] images = {{R.drawable.baby, R.drawable.chenqiaoen, R.drawable.gaoyuanyuan, R.drawable.gongyinfei},            {R.drawable.lixiaolu, R.drawable.tangyan, R.drawable.yangmi, R.drawable.yaodi}};    private MyAdapter myAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ExpandableListView) findViewById(R.id.expand_listview);        myAdapter = new MyAdapter(groups, childs, images);        listView.setAdapter(myAdapter);        listView.setOnChildClickListener(this);    }    /**     * listview的点击监听事件     *     * @param parent     * @param v     * @param groupPosition     * @param childPosition     * @param id     * @return     */    @Override    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {        Toast.makeText(this,childs[groupPosition][childPosition],Toast.LENGTH_LONG).show();        return true;    }}

效果

这里写图片描述

0 0