ExpandableListView是可展开的列表组件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:childIndicator="@drawable/ic_launcher"
/>
</LinearLayout>
MainActivity.java
package com.example.expandablelistview;
import android.os.Bundle;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
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);
//Create a "BaseExpandableListAdapter" object
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] logos = new int[]{
R.drawable.p,
R.drawable.t,
R.drawable.z
};
private String[] armTypes = new String[]{
"神族兵种","虫族兵种","人族兵种"
};
private String[][] arms = new String[][]{{"狂战士","龙骑士","黑暗圣堂","电兵"},{"小狗","刺蛇","飞龙","自爆飞机"},
{"机枪兵","护士MM","幽灵"}};
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return armTypes[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return arms[groupPosition].length;
}
//The method determines the appearance of each sub-option
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
//Obtain the specified group location,specify the child at the child list item list item data
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
};
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.list);
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;
}
}
本文详细介绍了如何在Android应用中使用ExpandableListView组件创建可展开的列表,包括XML布局文件的配置、适配器的实现以及数据展示逻辑。通过实例展示了神族兵种、虫族兵种、人族兵种等不同类别的兵种列表,并提供了自定义适配器的方法,以实现不同类型数据的展示。
362

被折叠的 条评论
为什么被折叠?



