java 代碼
package com.example.bao;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
//創(chuàng)建一個(gè)BaseEXpandableListAdpter對(duì)象
? ? ? ? BaseExpandableListAdapter ba=new BaseExpandableListAdapter() {
int[] bao=new int[]{
R.drawable.love,
R.drawable.mia,
R.drawable.mia1
};
private String[] love=new String[]
{"love mia","mia","miss mia"};
private String[][] loves=new String[][]{
{"love mia","mia","miss mia","010802"},
{"love mia","mia","miss mia","010802"},
{"love mia","mia","miss mia","010802"}
};
//獲取指定組位置,指定列表項(xiàng)處的子列表項(xiàng)數(shù)據(jù)
@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;
}
//決定每個(gè)組選項(xiàng)的外觀
@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(bao[arg0]);
ll.addView(logo);
TextView tv=getTextView();
tv.setText(getGroup(arg0).toString());
ll.addView(tv);
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 love.length;
}
//指定組位置處的組數(shù)據(jù)
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return love[arg0];
}
@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return loves[arg0].length;
}
private TextView getTextView(){
//ViewGroup.LayoutParams.MATCH_PARENT 徒溪,意思為寬度和父view相同 高度為64
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
//這個(gè)"this"是指MainActivity本身, 告訴系統(tǒng)這個(gè)v只跟MainActivity有關(guān)
TextView tv=new TextView(MainActivity.this);
tv.setLayoutParams(lp);
tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
tv.setPadding(36, 0, 0, 0);
tv.setTextSize(20);
return tv;
};
//該方法覺定每個(gè)子選項(xiàng)外觀
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
// TODO Auto-generated method stub
TextView tv=getTextView();
tv.setText(getChild(arg0, arg1).toString());
return tv;
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
//獲取指定組位置,指定列表項(xiàng)處的子列表項(xiàng)數(shù)據(jù)
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return loves[arg0][arg1];
}
};
ExpandableListView elv=(ExpandableListView) findViewById(R.id.list);
elv.setAdapter(ba);
? ? }
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical" >
? ? <ExpandableListView
? ? ? ? android:id="@+id/list"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"></ExpandableListView>
? ? </LinearLayout>
效果界面
![在這里插入圖片描述](https://img-blog.csdnimg.cn/45534ecf7fd740cc992e8ff78578b1b3.png)
:![在這里插入圖片描述](https://img-blog.csdnimg.cn/b0ceef17d4ec4c0a9f3ebbe2400d1c4f.png)