第一次接触ExpandableListvie这个控件

时间:2021-10-15 08:55:14

对了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;
}
});

}

}


第一次接触ExpandableListvie这个控件