ExpandableListView的简单例子
2013-03-21 17:47
295 查看
最近一段时间参考网上的例子,做了一下简单的 ExpandableListView,现在和大家共享一下:
1:main.xml的内容
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
显示一个ExpandableListView
2:ExpandableListViewDemo 类的内容
[java] view
plaincopy
package com.chama.expandablelistviewdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ExpandableListViewDemo extends Activity {
//定义两个List,用来存放控件中Group/Child中的String
private List<String> groupArray;
private List<List<String>> childArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//对这两个List进行初始化,并插入一些数据
groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
groupArray.add("第一行");
groupArray.add("第二行");
List<String> tempArray = new ArrayList<String>();
tempArray.add("第一条");
tempArray.add("第二条");
tempArray.add("第三条");
for(int index = 0; index <groupArray.size(); ++index)
{
childArray.add(tempArray);
}
//给定义好的ExpandableListView添加上Adapter
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
ExpandableAdapter adapter = new ExpandableAdapter(this);
expandableListView.setAdapter(adapter);
}
//定义ExpandableListView的Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
}
需要注意的内容:
1:groupArray 和childArray的内容,分别定义父对象和子对象显示的内容
2:ExpandableAdapter继承了BaseExpandableListAdapter,并重载了其中的一些方法, 注意public TextView getGenericView(String string)的作用,主要用形成显示父对象和子对象视图的类
3:程序效果
1:main.xml的内容
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
显示一个ExpandableListView
2:ExpandableListViewDemo 类的内容
[java] view
plaincopy
package com.chama.expandablelistviewdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ExpandableListViewDemo extends Activity {
//定义两个List,用来存放控件中Group/Child中的String
private List<String> groupArray;
private List<List<String>> childArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//对这两个List进行初始化,并插入一些数据
groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
groupArray.add("第一行");
groupArray.add("第二行");
List<String> tempArray = new ArrayList<String>();
tempArray.add("第一条");
tempArray.add("第二条");
tempArray.add("第三条");
for(int index = 0; index <groupArray.size(); ++index)
{
childArray.add(tempArray);
}
//给定义好的ExpandableListView添加上Adapter
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
ExpandableAdapter adapter = new ExpandableAdapter(this);
expandableListView.setAdapter(adapter);
}
//定义ExpandableListView的Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
}
需要注意的内容:
1:groupArray 和childArray的内容,分别定义父对象和子对象显示的内容
2:ExpandableAdapter继承了BaseExpandableListAdapter,并重载了其中的一些方法, 注意public TextView getGenericView(String string)的作用,主要用形成显示父对象和子对象视图的类
3:程序效果
相关文章推荐
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- Android ExpandableListView简单例子(附加时间模拟器)-FenGKun
- Android关于ExpandableListView用法的一个简单小例子。仿QQ列表
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- ExpandableListView的简单例子
- ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子
- Android中关于ExpandableListView用法的一个简单小例子
- 关于ExpandableListView用法的一个简单小例子(转)
- 关于ExpandableListView用法的一个简单小例子