ExpandableListView用法
来源:互联网 发布:兼职淘宝美工工资 编辑:程序博客网 时间:2024/04/20 18:09
ExpandableListView实现子列表
1.布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<ExpandableListView
android:id="@android:id/list" //必须如此定义ID
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
2.ExpandableListView适配器
public class ProvinceAdapter extends BaseExpandableListAdapter{
private Activity activity;
private String[] armTypes = new String[]{"南京","常州","南通"};
private String[][] arms = new String[][]{
{"江宁","雨花","下关","白下"},
{"溧阳","金坛"},
{"海安","如皋"}
};
public ProvinceAdapter(Activity activity){
this.activity = activity;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(activity);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView show_or_hideImg = new ImageView(activity);
if (isExpanded) {//如果父菜单是展开的 则展示向上箭头的图片
show_or_hideImg.setBackgroundResource(R.drawable.arrow_up);
}else {
show_or_hideImg.setBackgroundResource(R.drawable.arrow_down);
}
TextView textView = new TextView(activity);
textView.setText(getGroup(groupPosition).toString());
textView.setTextColor(Color.BLACK);
textView.setTextSize(16);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setPadding(15, 10, 10, 10);
textView.setGravity(Gravity.CENTER_VERTICAL);
ll.addView(textView,new LinearLayout.LayoutParams(300,55));
LinearLayout.LayoutParams imgLp = new LinearLayout.LayoutParams(44,LayoutParams.WRAP_CONTENT);
imgLp.gravity = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;
show_or_hideImg.setLayoutParams(imgLp);
ll.addView(show_or_hideImg);//图片放在右边
return ll;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return armTypes.length;
}
@Override
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].length;
}
private TextView getTextView(){
// AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,47);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams((int) (SyncSerUtil.defaultWidth * 0.8),50);
TextView textView = new TextView(activity);
textView.setLayoutParams(lp);
textView.setPadding(38, 10, 0, 10);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setTextColor(Color.BLACK);
textView.setTextSize(15);
return textView;
}
@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) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
}}
3.给ExpandableListView设定适配器
provinceListview.setGroupIndicator(null);//去掉自带的图片
provinceListview.setAdapter(new ProvinceAdapter(XXXActivity.this))
4.常用事件
//点击子菜单事件
provinceListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
Toast.makeText(ChartActivity.this, provinceAdapter.getChild(groupPosition, childPosition).toString(), Toast.LENGTH_LONG).show();
return false;
}
});//展开收缩子菜单事件(保证当前只展开一个父菜单)
provinceListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int i = 0; i < provinceAdapter.getGroupCount(); i++) {
if (i != groupPosition) {
provinceListView.collapseGroup(i);
}}});
- ExpandableListView用法
- ExpandableListView用法
- ExpandableListView 的用法
- ExpandableListView简单用法
- ExpandableListView简单用法
- ExpandableListView的详细用法
- ExpandableListView 的用法
- ExpandableListView简单用法
- ExpandableListView用法例子
- ExpandableListView用法和实例
- ExpandableListView的用法
- 关于ExpandableListView用法
- ExpandableListView用法实例
- ExpandableListView的基本用法
- ExpandableListView 的简单用法
- android ExpandableListView 用法
- ExpandableListView用法介绍
- ExpandableListView的用法
- 2013第五周上机任务【项目3 长方体类】
- 中兴WCDMA模块 Linux拨号流程
- 观察者模式 observer
- uva11198
- 网络流与线性规划24题09方格取数问题
- ExpandableListView用法
- HDU 4267 A Simple Problem with Integers
- Json_接收与解析(C++)
- 学JAVA必须知道:抽象类与接口的区别
- 判断自己的电脑是否被入侵
- 带参数宏定义和函数的区别
- 解决popupwindow中弹出输入法被挤上去 顶起的bug
- vi/vim缩进,注释,替换,查找,修改, 跳转
- 第五周项目三:长方柱类