前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ExpandableListView的使用

ExpandableListView的使用

作者头像
提莫队长
发布2019-03-01 09:42:57
1.4K0
发布2019-03-01 09:42:57
举报
文章被收录于专栏:刘晓杰刘晓杰

ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像手机QQ中的好友列表就是实现的类似效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类。

MainActivity.java

代码语言:javascript
复制
package com.example.expandablelistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private ExpandableListView listView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ExpandableListView)findViewById(R.id.expandableListView1);
        listView.setAdapter(new MyExpandableAdapter());
    }
    
    class MyExpandableAdapter extends BaseExpandableListAdapter{
    	private String[] groups = {"好友", "扣丁学堂"};
    	private String[][] childs = {{"猪哥", "蛋蛋"}, {"威哥", "阿珂"}};

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

		@Override
		public Object getChild(int index1, int index2) {
			return childs[index1][index2];
		}

		@Override
		public long getChildId(int index1, int index2) {
			return index2;
		}

		@Override
		public View getChildView(int index1, int index2, boolean arg2, 
				View view, ViewGroup parent) {
			if(view == null){
				view = getLayoutInflater().inflate(R.layout.child, parent, false);
			}
			TextView tv = (TextView)view.findViewById(R.id.title);
			tv.setText(childs[index1][index2]);
			return view;
		}

		@Override
		public int getChildrenCount(int index) {
			return childs[index].length;
		}

		@Override
		public long getCombinedChildId(long arg0, long arg1) { return 0; }

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

		@Override
		public Object getGroup(int index) {
			return groups[index];
		}

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

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

		@Override
		public View getGroupView(int index, boolean arg1, View view, 
				ViewGroup parent) {
			if(view == null){
				view = getLayoutInflater().inflate(R.layout.group, parent, false);
			}
			TextView tv = (TextView)view.findViewById(R.id.title);
			tv.setText(groups[index]);
			return view;
		}

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

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}
    }
}

main.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    tools:context="com.example.expandablelistview.MainActivity" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>

group.xml

代码语言:javascript
复制
<?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"
    android:orientation="horizontal"
    android:gravity="left"
    tools:context=".MainActivity" >

    <ImageView
        android:contentDescription="@null"
        android:id="@+id/icon"
        android:maxWidth="60sp"
        android:maxHeight="60sp"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:textSize="40sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

child.xml

代码语言:javascript
复制
<?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"
    android:orientation="horizontal"
    android:gravity="left"
    android:paddingLeft="30sp"
    tools:context=".MainActivity" >

    <ImageView
        android:contentDescription="@null"
        android:id="@+id/icon"
        android:maxWidth="60sp"
        android:maxHeight="60sp"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:textSize="40sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年03月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档