第一次接触ExpandableListvie这个控件

对了qq这么久都不知道qq的界面怎么做。ExpandableListvie 可以帮到你!

首先在xml 加上控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
           android:background="#ffffff"
        android:cacheColorHint="#00000000"
         android:listSelector="#00000000" >
    </ExpandableListView>

</LinearLayout>

然后在你的MainActivity.java 

实例化你的expandablelistview 和重写BaseExpandableListAdapter

package com.july.expandablelistview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

			int[] logos = new int[] { R.drawable.ic_launcher,
					R.drawable.ic_launcher, R.drawable.ic_launcher };

			private String[] generalsTypes = new String[] { "好友", "好友", "好友" };

			private String[][] generals = new String[][] {

			{ "陈明冠", "陈明冠", "陈明冠" }, { "b", "bb", "bbb" }, { "c", "cc", "ccc" } };

			private int[][] generallogos = new int[][] {

					{ R.drawable.ic_launcher, R.drawable.ic_launcher,
							R.drawable.ic_launcher },
					{ R.drawable.ic_launcher, R.drawable.ic_launcher,
							R.drawable.ic_launcher },
					{ R.drawable.ic_launcher, R.drawable.ic_launcher,
							R.drawable.ic_launcher }

			};

			TextView getTextView() {
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT, 64);
				TextView textView = new TextView(MainActivity.this);
				textView.setPadding(45, 0, 0, 0);
				textView.setTextSize(20);
				textView.setTextColor(Color.BLACK);
				textView.setLayoutParams(lp);
				textView.setGravity(Gravity.CENTER_VERTICAL);

				return textView;
			}

			@Override
			public boolean isChildSelectable(int arg0, int arg1) {
				// TODO Auto-generated method stub
				return true;
			}

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

			@Override
			public View getGroupView(int arg0, boolean arg1, View arg2,
					ViewGroup arg3) {
				// TODO Auto-generated method stub

				LinearLayout ll = new LinearLayout(MainActivity.this);
				ll.setOrientation(0);
				ImageView logo = new ImageView(MainActivity.this);
				logo.setImageResource(logos[arg0]);

				TextView a = getTextView();
				a.setText(getGroup(arg0).toString());

				ll.addView(a);
				return ll;
			}

			@Override
			public long getGroupId(int arg0) {
				// TODO Auto-generated method stub
				return arg0;
			}

			@Override
			public int getGroupCount() {
				// TODO Auto-generated method stub
				return generalsTypes.length;
			}

			@Override
			public Object getGroup(int arg0) {
				// TODO Auto-generated method stub
				return generalsTypes[arg0];
			}

			@Override
			public int getChildrenCount(int arg0) {
				// TODO Auto-generated method stub
				return generals[arg0].length;
			}

			@Override
			public View getChildView(int arg0, int arg1, boolean arg2,
					View arg3, ViewGroup arg4) {
				LinearLayout ll = new LinearLayout(MainActivity.this);
				ll.setOrientation(0);
				ImageView logo = new ImageView(MainActivity.this);
				logo.setImageResource(generallogos[arg0][arg1]);
				ll.addView(logo);
				TextView a = getTextView();
				a.setText(getChild(arg0, arg1).toString());
				ll.addView(a);
				return ll;

			}

			@Override
			public long getChildId(int arg0, int arg1) {
				// TODO Auto-generated method stub
				return arg1;
			}

			@Override
			public Object getChild(int arg0, int arg1) {
				// TODO Auto-generated method stub
				return generals[arg0][arg1];
			}
		};

		ExpandableListView expandbalelistview = (ExpandableListView) findViewById(R.id.expandableListView1);
		expandbalelistview.setAdapter(adapter);
		expandbalelistview.setOnChildClickListener(new OnChildClickListener() {

			public boolean onChildClick(ExpandableListView parent, View v,
					int arg0, int arg1, long id) {
				// TODO Auto-generated method stub

				Toast.makeText(MainActivity.this,
						"你点击了" + adapter.getChild(arg0, arg1),
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});

	}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值