可扩展的ListView1

MainActivity.java:

package com.example.test09_expandable;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;


public class MainActivity extends Activity {
private ExpandableListView expand_list;
public String[] groupStrings = {"西游记", "水浒传", "三国演义", "红楼梦"};
    public String[][] childStrings = {
            {"唐三藏", "孙悟空", "猪八戒", "沙和尚"},
            {"宋江", "林冲", "李逵", "鲁智深"},
            {"曹操", "刘备", "孙权", "诸葛亮", "周瑜"},
            {"贾宝玉", "林黛玉", "薛宝钗", "王熙凤"}
    };




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expand_list=(ExpandableListView) findViewById(R.id.expand_list);
MyAdapter adapter = new MyAdapter();
expand_list.setAdapter(adapter);
}
class MyAdapter extends BaseExpandableListAdapter{


@Override
public int getGroupCount() {

return groupStrings.length;
}

@Override
public int getChildrenCount(int groupPosition) {

return childStrings[groupPosition].length;
}


//获取指定的分组数据
@Override
public Object getGroup(int groupPosition) {
return groupStrings[groupPosition];
}


//获取指定分组中指定的子选项的数据
//[groupPosition][childPosition]第一个取得是二维数组里的行,第二个是行里面的数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return childStrings[groupPosition][childPosition];
}


//获取指定分组的ID
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}



//获取指定分组下的子选项的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}



//分组和子选项是否持有稳定的ID,也就是底层数据的改变会不会影响到它们俩
@Override
public boolean hasStableIds() {
return true;
}


//获取指定分组的视图
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder = null;
if (convertView==null) {
convertView=View.inflate(MainActivity.this, 
R.layout.item_expand_group, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.tvTitle=(TextView) convertView.findViewById(R.id.label_expand_group);
convertView.setTag(groupViewHolder);
}else{
groupViewHolder=(GroupViewHolder) convertView.getTag();
}
groupViewHolder.tvTitle.setText(groupStrings[groupPosition]);
return convertView;
}



//获取指定分组当中的指定子选项的视图
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder= null;
if (convertView==null) {
convertView=View.inflate(MainActivity.this,
R.layout.item_expand_child, null);
childViewHolder = new ChildViewHolder();
childViewHolder.tvTitle=(TextView)convertView.findViewById(R.id.label_expand_child);
convertView.setTag(childViewHolder);
}else{
childViewHolder=(ChildViewHolder) convertView.getTag();
}
childViewHolder.tvTitle.setText(childStrings[groupPosition][childPosition]);
return convertView;
}


//指定位置的子元素是否可选中
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {

return true;
}
class GroupViewHolder {
        TextView tvTitle;
    }
class ChildViewHolder {
        TextView tvTitle;
}

}

}


activity_main.xml:

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/expand_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


</ExpandableListView>

item_expand_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"


    android:id="@+id/label_expand_child"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_blue_light"
    android:paddingLeft="20dp"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

item_expand_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"


    android:id="@+id/label_expand_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_blue_light"
   
    android:textColor="@android:color/white"
    android:textSize="20sp" />







阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页