ExpandableListView实例

ExpandableListView实例_第1张图片ExpandableListView实例_第2张图片

xml文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">


<ExpandableListView android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:cacheColorHint="#00000000"
   android:listSelector="#00000000">
</ExpandableListView>


</LinearLayout>

但值得简单说下的是 

android:cacheColorHint="#00000000",

这个设置可以去除拖动view时背景变成黑色的效果

android:listSelector="#00000000" ,

可以去除选中时的黄色底色


.java文件


package com.example.myexpandablelistview;


import android.os.Bundle;
import android.app.Activity;
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.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
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,BaseExpandableListAdapter used to provide data and Views from some data to an expandable list view.
ExpandableListAdapter expandable=new BaseExpandableListAdapter(){

//组的名称
private String[] brand=new String[]{"大众","奔驰","宝马","奥迪"};
//每个组下边对于的内容
private String[][] cars=new String[][]{
{"迈腾","帕萨特","宝来","辉腾"},
{"S600","E200","G63","S350"},
{"X6","760li","535li","Z4"},
{"A6","A8","R8","S7"}

};
//每个组对应的图片
private int[] brandImage=new int[]{
R.drawable.dazhong,
R.drawable.benchi,
R.drawable.baoma,
R.drawable.aodi
};


@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return cars[groupPosition][childPosition];
}


@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

//我们的子分类只是一个TextView,这里我们只是建立一个TextView就行了
TextView textView=getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}


@Override
public int getChildrenCount(int groupPosition) {
// 获取某个组下边的子分类的数量
return cars[groupPosition].length;
}


@Override
public Object getGroup(int groupPosition) {
// 获取某个组的值
return brand[groupPosition];
}


@Override
public int getGroupCount() {
//获取组的长度
return brand.length;
}


@Override
public long getGroupId(int groupPosition) {
//获取组brand的Id
return groupPosition;
}


//得到类别组件
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

//获取一组内容,包括一个TextView和一个Image,此处我们用一个线性布局来进行展示

LinearLayout ll=new LinearLayout(MainActivity.this);

/**
* 0 表示horizonal
* 1 表示vertical
*/
ll.setOrientation(0);

//创建一个ImageView
ImageView imageView=new ImageView(MainActivity.this);
imageView.setScaleType(ScaleType.CENTER_INSIDE );
imageView.setImageResource(brandImage[groupPosition]);
imageView.setPadding(50, 24, 0, 0);
ll.addView(imageView);

//创建一个ImageView
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);

return ll;
}


//表示孩子是否和组ID是跨基础数据的更改稳定
public boolean hasStableIds() {
return true;
}


//子组件是不是可以选择的
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}




//我们自己创建一个生成TextView的方法
private TextView getTextView(){
//创建一个LayoutParams的实例
//AbsListView扩展了LayoutParams以提供空间来保存视图类型
/**
* public AbsListView.LayoutParams(int w,int h)
* -1 match_parent
* -1 fill_parent
* -2 wrap_parent
*/
//设置TextView的宽度和高度
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(-1,64);
//创建一个新的TextView
TextView textView=new TextView(MainActivity.this);
//设置TextView的宽度和高度
textView.setLayoutParams(lp);
//设置TextView的显示位置,垂直居中,靠左对齐
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
//设置TextView的padding值
textView.setPadding(20, 0, 0, 0);
//设置字体的大小
textView.setTextSize(20);
return textView;
}

};

ExpandableListView expandListView=(ExpandableListView) super.findViewById(R.id.list);
expandListView.setAdapter(expandable);
}
}

你可能感兴趣的:(ExpandableListView实例)