二级列表的简单效果实现

二级列表的布局 

<!--二级列表
groupIndicator 1:@null 去掉默认的图标 2: @drawable/ic_launcher 指定一个默认的图标
-->
<ExpandableListView
    android:id="@+id/expandable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ></ExpandableListView>
<!--android:groupIndicator="@mipmap/ic_launcher_round"-->


简单色实现思路:定义两个数组一个一维数组,一个二维数组
//一级标题的数据
public String[] groupArray=new String[]{"武侠类","惊悚/恐怖类","影视小说"};
//二级标题的数据
public String[][] childArray=new String[][]{
        {"天龙八部","倚天屠龙记","神雕侠侣","鹿鼎记"},
        {"盗墓笔记","摸金校尉","鬼吹灯"},
        {"花千骨","步步惊心","琅琊榜","追风筝的人"}};
二级列表的点击事件和适配器的写法:
//一维数组的点击事件
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
                                int groupPosition, long id) {
        Toast.makeText(MainActivity.this, "你点击了:"+groupArray[groupPosition], Toast.LENGTH_SHORT).show();
        return false;
    }
});
//二维维数组的点击事件
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {

        Toast.makeText(MainActivity.this, "你点击了:"+childArray[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
        return false;
    }
});

//二级列表的适配器 需要继承
extends BaseExpandableListAdapter
//适配器里的代码
class MyAdapter2 extends BaseExpandableListAdapter{

    //得到分组的个数
    @Override
    public int getGroupCount() {
        return groupArray.length;
    }

    //得到每个分组内数据的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childArray[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    //得到分组内的布局视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        View v=View.inflate(MainActivity.this, R.layout.group_item, null);
        TextView tv=(TextView) v.findViewById(R.id.textView1);

        tv.setText(groupArray[groupPosition]);

        return v;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        View v=View.inflate(MainActivity.this, R.layout.child_item, null);
        TextView tv=(TextView) v.findViewById(R.id.textView1);

        //根据分组下标以及组成成员的下标值 获取数据
        tv.setText(childArray[groupPosition][childPosition]);

        return v;
    }

    //二级标题内容是否可以被选中  true:表示可选择即可点击  反之
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //
        return true;
    }

}
全部代码:
package com.example.myexpandablelistview;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView elv;

    //一级标题的数据
    public String[] groupArray=new String[]{"武侠类","惊悚/恐怖类","影视小说"};
    //二级标题的数据
    public String[][] childArray=new String[][]{
            {"天龙八部","倚天屠龙记","神雕侠侣","鹿鼎记"},
            {"盗墓笔记","摸金校尉","鬼吹灯"},
            {"花千骨","步步惊心","琅琊榜","追风筝的人"}};

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


        elv = (ExpandableListView) findViewById(R.id.expandable);
        //设置适配器
        elv.setAdapter(new MyAdapter2());

        elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                Toast.makeText(MainActivity.this, "你点击了:"+groupArray[groupPosition], Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                Toast.makeText(MainActivity.this, "你点击了:"+childArray[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                return false;
            }
        });


    }
    class MyAdapter2 extends BaseExpandableListAdapter{

        //得到分组的个数
        @Override
        public int getGroupCount() {
            return groupArray.length;
        }

        //得到每个分组内数据的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return childArray[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        //得到分组内的布局视图
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {

            View v=View.inflate(MainActivity.this, R.layout.group_item, null);
            TextView tv=(TextView) v.findViewById(R.id.textView1);

            tv.setText(groupArray[groupPosition]);

            return v;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            View v=View.inflate(MainActivity.this, R.layout.child_item, null);
            TextView tv=(TextView) v.findViewById(R.id.textView1);

            //根据分组下标以及组成成员的下标值 获取数据
            tv.setText(childArray[groupPosition][childPosition]);

            return v;
        }

        //二级标题内容是否可以被选中  true:表示可选择即可点击  反之
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            //
            return true;
        }

    }
}

布局文件:
《activity_main》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myexpandablelistview.MainActivity">
    <!--二级列表
    groupIndicator 1:@null 去掉默认的图标 2: @drawable/ic_launcher 指定一个默认的图标
    -->
    <ExpandableListView
        android:id="@+id/expandable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ExpandableListView>
    <!--android:groupIndicator="@mipmap/ic_launcher_round"-->

</LinearLayout>

group_item
<?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="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#f00"
        android:layout_marginLeft="30dp" />

</LinearLayout>









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值