ExpandableListView 使用及设置监听函数

直接贴代码(原谅中二的我)

public class MyExpandableListView extends Activity{
    private ExpandableListView listView;
    private TextView childView,groupView;
    private ImageView imageView;
    private  LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandablelistview);
        initview();


        listView = (ExpandableListView)findViewById(R.id.expandablelistview);
        linearLayout = (LinearLayout)findViewById(R.id.layout);


        final int[] img = new int[]{
                R.mipmap.a,
                R.mipmap.b,
                R.mipmap.c
        };

        final String[] title = new String[]{
                "物语系列","FATE ZERO","钢之炼金术"
        };

        final String[][] str = new String[][]{
                {"阿拉拉垃圾君","战场原","八九寺"},
                {"卫宫切嗣","saber","金闪闪"},
                {"艾德","艾尔","大佐"}
        };

        ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter() {
            @Override
            public void registerDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public int getGroupCount() {
                return title.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return str[groupPosition].length;
            }

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

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return str[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) {
                final View view2 = LayoutInflater.from(MyExpandableListView.this).inflate(R.layout.groupview,null);
                groupView = (TextView)view2.findViewById(R.id.groupview);
                imageView = (ImageView)view2.findViewById(R.id.imageview);
                imageView.setImageResource(img[groupPosition]);
                groupView.setText(title[groupPosition]);
                return view2;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                View view1 = LayoutInflater.from(MyExpandableListView.this).inflate(R.layout.childview,null);
                childView = (TextView)view1.findViewById(R.id.childview);
                childView.setText(str[groupPosition][childPosition]);
                return childView;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

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

            @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;
            }
        };
        listView.setAdapter(expandableListAdapter);

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.i(""+MyExpandableListView.this, "group " + groupPosition + " child " + childPosition);
                return false;
            }
        });

        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.i("" + MyExpandableListView.this, "group " + groupPosition );
                return false;
            }
        });
    }
}

注意getChildView和getGroupView中的view声明必须在内部,相当于每次创建表项都new一个view来显示,否则会后只是会显示最后一个group和最后一个child。

expandablelistview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ExpandableListView
        android:id="@+id/expandablelistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ExpandableListView>
</LinearLayout>

childview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/childview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

groupview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <TextView
        android:id="@+id/groupview"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

其中几个重要的方法
1、getGroupCount()
返回包含的组列表项的数量。
2、getGroupView()
该方法将返回的View对象将作为组列表项
3、getChildCount()
该方法返回特定组所包含的子列表项的数量
4、getChildView()
该方法返回的View对象将作为特定组、特定位置的子列表项
5、isChildSelectable()
该方法返回true时,可以对子列表项设置监听

监听函数
1、listView.setOnChildClickListener()
监听子列表项按下事件
2、setOnGroupClickListener()
监听组列表项按下事件
注意:setOnGroupClickListener()返回值必须为false时,才可以正确监听(但是并没搞懂为什么)

这是OnGroupClickListener的api介绍

onChildClick
boolean onChildClick(ExpandableListView parent,
View v,
int groupPosition,
int childPosition,
long id)
Callback method to be invoked when a child in this expandable list hasbeen clicked.
参数:
parent - The ExpandableListView where the click happened
v - The view within the expandable list/ListView that was clicked
groupPosition - The group position that contains the child thatwas clicked
childPosition - The child position within the group
id - The row id of the child that was clicked
返回:
True if the click was handled

但是最后意思是 如果事件被处理返回true,有点搞不懂。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值