ExpandableListView练习代码

来源:互联网 发布:手机视频加速软件 编辑:程序博客网 时间:2024/06/01 13:59

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"><ExpandableListViewandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Create a "BaseExpandableListAdapter" objectExpandableListAdapter 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","幽灵"}};@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}//该方法决定每个组选项的外观@Overridepublic 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;}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn armTypes.length;}//获取指定组位置处的组数据@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn armTypes[groupPosition];}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn arms[groupPosition].length;}//The method determines the appearance of each sub-option@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView textView = getTextView();textView.setText(getChild(groupPosition,childPosition).toString());return textView;}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}//Obtain the specified group location,specify the child at the child list item list item data@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn 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);}@Overridepublic 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;}}


原创粉丝点击