Android ExpandableListView 的简单使用

MAinActivity的代码如下所示:

public class MainActivity extends Activity {

	private ExpandableListView mExpandableListView;
	private ArrayList<String> list;
	private ArrayList<String[]> arrayList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mExpandableListView = (ExpandableListView) findViewById(R.id.activity_main_expandableListView);
	    //外层布局数据
		list = new ArrayList<String>();
		//内层布局数据
		arrayList = new ArrayList<String[]>();
		list.add("我的好友");
		list.add("黑名单");
		list.add("陌生人");
		list.add("同事");
		list.add("老师");
		
		arrayList.add(new String[]{"欣哥","张三","李四","王五"});
		arrayList.add(new String[]{"欣哥","张三","李四","王五"});
		arrayList.add(new String[]{"张三","李四","王五"});
		arrayList.add(new String[]{"欣哥","张三","李四","王五"});
		arrayList.add(new String[]{"张三","李四","王五"});
	
        MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter();
	    mExpandableListView.setAdapter(adapter);
	    mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			
			@Override
			public void onGroupExpand(int groupPosition) {
				
			}
		});
	}
	//ExpandableListViewAdapter组件的父类适配是BaseExpandableListAdapter
	class MyExpandableListViewAdapter extends BaseExpandableListAdapter{

		//外层Item数量
		@Override
		public int getGroupCount() {
			return list.size();
		}

		//当前内层Item数量
		@Override
		public int getChildrenCount(int groupPosition) {
			return arrayList.get(groupPosition).length;
		}

		//当前外层Item内容
		@Override
		public Object getGroup(int groupPosition) {
			return list.get(groupPosition);
		}

		//当前内层Item内容
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return arrayList.get(groupPosition)[childPosition];
		}

		//当前外层Item Id
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		//当前内层Item Id
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
        
		//是否使用同一个id
		@Override
		public boolean hasStableIds() {
			return false;
		}

		//获得当前外层Item的视图
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			//创建一个TextView的对象
			TextView text = new TextView(MainActivity.this);
			//创建一个矩形布局
			LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			//给TextView对象设置宽高
			text.setLayoutParams(params);
			//给TextView对象设置padding属性(左,上,右,下)
			text.setPadding(10, 5, 0, 5);
			text.setText(list.get(groupPosition));
			//给TextView设置重心
			text.setGravity(Gravity.CENTER_HORIZONTAL);
			return text;
		}

		//获得当前内层Item的视图
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView text = new TextView(MainActivity.this);
			LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			text.setLayoutParams(params);
			text.setPadding(10, 5, 0, 5);
			text.setTextColor(Color.RED);
			text.setGravity(Gravity.CENTER_HORIZONTAL);
			text.setText(arrayList.get(groupPosition)[childPosition]);
			return text;
		}

		//设置内层视图是否可点击
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}

}

activity_main.xml

<RelativeLayout 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=".MainActivity" >

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

</RelativeLayout>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值