ExpandableListView的使用

ExpandableListView的使用
前言:

    之前在一款APP上看到一个“常见问题模块”,进入后只显示所有的问题,点击某个问题就在该问题的下面显示该问题的答案。然后点击其他问题之前点击过的问题就自动关闭,只显示当前问题的答案,于是就考虑用ExpandableListview试着实现了一下,先看一下效果图:
    
具体实现:
    1.首先创建两个布局文件,分别用来填充group和child:
expandle_head_item.xml
<span style="color:#009900;"><?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="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal"
    android:background="#87B8E5" >

    <TextView
        android:id="@+id/group_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#EEF3FA"
        android:textSize="15sp"
        android:padding="8dp"/>

</LinearLayout></span>

expandle_child_item.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="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal"
    android:background="#87B8E5" >

    <TextView
        android:id="@+id/group_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#EEF3FA"
        android:textSize="15sp"
        android:padding="8dp"/>

</LinearLayout>

   2.activity的布局文件主要是一个ExpandableListview,具体如下:
<span style="color:#009900;"><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="com.example.ly.activity.ExpandableListActivity">

    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#2dd4b6"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tetil_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="常见问题"
            android:textStyle="bold"
            android:textSize="18sp"/>

    </LinearLayout>

    <ExpandableListView
        android:id="@+id/expandable_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/title_layout"
        android:divider="@null"
        android:groupIndicator="@null"
        android:dividerHeight="13dp"/>

</RelativeLayout></span>

3.Activity具体实现:重写一个继承自BaseExpandableListAdapter的adapter来填充ExpandableListview
<span style="font-size:24px;color:#009900;">import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private int[] logos = new int[]{
            R.mipmap.src1,
            R.mipmap.src2,
            R.mipmap.src3
    };
    private String[] armTypes = new String[]{"问题1?","问题2?","问题3?"};
    private String[][] arms = new String[][]{
            {"答案1"},
            {"答案2"},
            {"答案3"}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list);

        expandableListView = (ExpandableListView) findViewById(R.id.expandable_list);
        ExpandListViewAdapter adapter = new ExpandListViewAdapter(this);
        expandableListView.setAdapter(adapter);
        expandableListView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for(int f = 0;f < armTypes.length ;f++){
                    if(groupPosition != f){
                        expandableListView.collapseGroup(f);
                    }
                }
            }
        });
    }

    class ExpandListViewAdapter extends BaseExpandableListAdapter{

        private LayoutInflater mInflater;

        public ExpandListViewAdapter(Context context){
            mInflater = LayoutInflater.from(context);
        }

        //获取总共有多少个组项
        @Override
        public int getGroupCount() {
            return armTypes.length;
        }

        //获取指定组项包含的子项数目
        @Override
        public int getChildrenCount(int groupPosition) {
            return arms[groupPosition].length;
        }

        //获取指定组位置处的组数据
        @Override
        public Object getGroup(int groupPosition) {
            return armTypes[groupPosition];
        }

        //获取指定组、指定子列表项处的子列表项数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return arms[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 true;
        }

        //返回组项显示的View
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
            view = mInflater.inflate(R.layout.expandle_head_item, viewGroup,false);
            TextView textView = (TextView) view.findViewById(R.id.group_text);
            textView.setText(getGroup(groupPosition).toString());
            return view;
        }

        //返回子项显示的View
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
            view = mInflater.inflate(R.layout.expandle_child_item, viewGroup,false);
            TextView textView = (TextView) view.findViewById(R.id.child_text);
            textView.setText(getChild(groupPosition, childPosition).toString());
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }
}</span>
又不足之处还望留言指正!









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值