android_ExpandableListView

简单介绍:

ExpandableListView是ListView的子类,
这个类,主要的使用场景就是,当条目下面还有子条目。这个时候,就是用ExpandableListView来操作。

第一步:页面布局


    <ExpandableListView
        android:groupIndicator="@null"
        android:id="@+id/commnumber_elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ExpandableListView>  

/**
groupIndicator属性,是用来控制,主条目,是否会显示下拉的箭头。
不让其显示就赋值为@null
*/

Activity中的代码

public class MainActivity extends Activity implements OnChildClickListener, OnGroupClickListener {
    private ExpandableListView mElv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //准备数据
        //组的数据
        String[] group=new String[]{"组1","组2","组3","组4","组5"};
        //一个组下面有多个条目
        String[] item1=new String[]{"子1","子1","子1","子1","子1","子1","子1","子1"};
        String[] item2=new String[]{"子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2","子2"};
        String[] item3=new String[]{"子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3","子3"};
        String[] item4=new String[]{"子4","子4","子4","子4","子4","子4","子4","子4"};
        String[] item5=new String[]{"子5","子5","子5","子5","子5","子5","子5","子5"};

        //把子的数据,往集合里面添加
        List<String[]> list=new ArrayList<String[]>();
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mElv.setAdapter(new ExableAdapter(this,group,list));


        mElv.setOnChildClickListener(this);//监听子条目被点击
        mElv.setOnGroupClickListener(this);//监听组被点击了
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Log.d("tag", "子条目被点击了");
        return true;
    }

    int mCurrenPosition=-1;//当前没有任何的组展开

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

        if(mCurrenPosition==-1){//表示当前没有任何的组展开
            mElv.expandGroup(groupPosition);
            mCurrenPosition=groupPosition;
        }else{

            if(mCurrenPosition==groupPosition){//2次点击的是相同的组
                mElv.collapseGroup(groupPosition);
                mCurrenPosition=-1;
            }else{//如果2次点击的条目不一致,就关闭以前的组,展开现在点击的组,并且置顶
                //关闭组
                mElv.collapseGroup(mCurrenPosition);
                //展开组
                mElv.expandGroup(groupPosition);
                //选择置顶的组
                mElv.setSelectedGroup(groupPosition);

                mCurrenPosition=groupPosition;
            }
        }
        /**
         *  performClick view
         *  expandableListView 的条目点击实际上就是父亲里面有一个方法处理  performItemClick
         * 
         */
        //返回值 true:  关于组的点击效果,只会执行我们这里的逻辑,系统默认的逻辑,再也不会执行了。
        //false:  代表的是,这个组的点击,除了处理我们自己写的逻辑之外,还会处理父亲中的组展开和关闭的逻辑,所以
        //有可能在界面上看到重复的效果,或者是某一个组并没有移动到最顶上位置
        return true;
    }
    }

ExableAdapter

public class ExableAdapter extends BaseExpandableListAdapter {

    Context context;
    String[] group;
    List<String[]> allItem;

    public ExableAdapter(Context context, String[] group, List<String[]> item) {
        this.context = context;
        this.group = group;
        this.allItem = item;
    }

    // 组有多少条目
    @Override
    public int getGroupCount() {
        return group.length;
    }

    // 子条目有多少条
    @Override
    public int getChildrenCount(int groupPosition) {
        return allItem.get(groupPosition).length;
    }

    // 显示组的View
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView tv = new TextView(context);
        String value = group[groupPosition];
        tv.setTextSize(18);
        tv.setBackgroundColor(Color.parseColor("#33000000"));
        tv.setPadding(8, 8, 8, 8);

        tv.setText(value);
        return tv;
    }

    // 显示子条目的View
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView tv = new TextView(context);
        // 获取到每一个子的
        String itemValue = allItem.get(groupPosition)[childPosition];
        tv.setTextSize(18);
        tv.setPadding(8, 8, 8, 8);
        tv.setText(itemValue);
        return tv;
    }

    // 子条目是否可以被点击,当设置为true表示可以被点击
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    // ---------分割线----------

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

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

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值