ExpandableListView的简单使用

ExpandableListView

是可以有二级的listview,先看效果图:

首先是布局文件,就一个控件就行了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="myapp.czw.com.ceshi.Main4Activity">
   <ExpandableListView
       android:id="@+id/main4_EXListview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
      >
   </ExpandableListView>


</LinearLayout>

然后是activity代码
package myapp.czw.com.ceshi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

public class Main4Activity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    Toast mtoast=GongYongToast.mtoast;
    private int[] logos = new int[]{
            R.drawable.default1,
            R.drawable.default2,
            R.drawable.default3,
            R.drawable.default1
    };
    private String[] armTypes = new String[]{"WORD", "EXCEL", "EMAIL", "PPT"};
    private String[][] arms = new String[][]{
            {"文档编辑", "文档排版", "文档处理", "文档打印"},
            {"表格编辑", "表格排版", "表格处理", "表格打印"},
            {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},
            {"演示编辑", "演示排版", "演示处理", "演示打印"},};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        expandableListView= (ExpandableListView) findViewById(R.id.main4_EXListview);
        MyExpandableListAdapter myExpandableListAdapter=new MyExpandableListAdapter(this,logos,armTypes,arms);
        expandableListView.setAdapter(myExpandableListAdapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if(mtoast!=null)
                {
                    mtoast.cancel();
                    mtoast= Toast.makeText(Main4Activity.this,"你点击的\ngroupPosition="+groupPosition+",childPosition="
                            +childPosition+"\n"+arms[groupPosition][childPosition], Toast.LENGTH_SHORT);
                }
                else
                {
                    mtoast=Toast.makeText(Main4Activity.this,"你点击的\ngroupPosition="+groupPosition+",childPosition="
                            +childPosition+"\n"+arms[groupPosition][childPosition], Toast.LENGTH_SHORT);
                }
                mtoast.show();
                return false;
            }
        });
    }
}

最后是适配器代码,适配器要重写的方法比较多;
package myapp.czw.com.ceshi;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Adminczw on 2016/11/26.
 */
public class MyExpandableListAdapter implements ExpandableListAdapter{
    Context context;
    int[] logos ;
    private String[] armTypes ;
    private String[][] arms ;

    public MyExpandableListAdapter(Context context, int[] logos, String[] armTypes, String[][] arms) {
        this.context = context;
        this.logos = logos;
        this.armTypes = armTypes;
        this.arms = arms;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @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;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(context);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ImageView logo = new ImageView(context);
        logo.setImageResource(logos[groupPosition]);
        logo.setPadding(80, 0, 0, 0);//让图片离父控件左边80,为了不让图片挡住自带的那个指示箭头
        // setPadding(左,上,右,下)离父元素的距离
        logo.setAdjustViewBounds(true);//android:adjustViewBounds是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。
        logo.setMaxWidth(300);
        logo.setMaxHeight(300);
        ll.addView(logo);
        TextView textView = getTextView();//调用的是最下面的那个方法
        textView.setText(getGroup(groupPosition).toString());
        textView.setPadding(10, 0, 0, 0);
        ll.addView(textView);
        return ll;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;//这个为true,子item才可以点击
    }

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

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

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    private TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 100);
        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36, 0, 0, 0);//setPadding(左,上,右,下)离父元素的距离
        textView.setTextSize(20);
        return textView;
    }
}

以上都是自己比较简单的介绍,详情可以参看以下文章链接

https://my.oschina.net/yaowen424/blog/533092
http://www.2cto.com/kf/201207/142950.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值