在实际的应用中我们可能会需要可以展开的listview 下面就介绍一下这个方法的使用
main.xml
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginRight="24dp"
android:layout_marginTop="59dp" >
</ExpandableListView>
main.java
package com.luoshuai.fengkaungtest2;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter adapter=new ExpandableListAdapter() {
int[] logos={
R.drawable.ccc,
R.drawable.ccc,
R.drawable.ccc
};
private String[] armTypeStrings={
"罗哥1","罗哥2","罗哥3"
};
private String[][] armStrings={
{"1","2","3","4"},{"5","6","7","8"},{"9","10","11","12"}
};
public void unregisterDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
public void registerDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
public void onGroupExpanded(int arg0) {
// TODO Auto-generated method stub
}
public void onGroupCollapsed(int arg0) {
// TODO Auto-generated method stub
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
//该方法决定每个组选项的外观
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 logoImageView=new ImageView(MainActivity.this);
logoImageView.setImageResource(logos[arg0]);
ll.addView(logoImageView);
TextView textView=getTextView();
textView.setText(getGroup(arg0).toString());
ll.addView(textView);
return ll;
}
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypeStrings.length;
}
//获取指定组位置处的组数据
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return armTypeStrings[arg0];
}
public long getCombinedGroupId(long arg0) {
// TODO Auto-generated method stub
return 0;
}
public long getCombinedChildId(long arg0, long arg1) {
// TODO Auto-generated method stub
return 0;
}
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return armStrings[arg0].length;
}
private TextView getTextView(){
// TODO Auto-generated method stub
AbsListView.LayoutParams lpLayoutParams=new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);
TextView textView=new TextView(MainActivity.this);
textView.setLayoutParams(lpLayoutParams);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
TextView textView=getTextView();
textView.setText(getChild(arg0, arg1).toString());
return textView;
}
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return armStrings[arg0][arg1];
}
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
};
ExpandableListView expandableListView=(ExpandableListView) findViewById(R.id.expandableListView1);
expandableListView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
效果图