android ExpandableListActivity简介
来源:互联网 发布:windows dpi 修改工具 编辑:程序博客网 时间:2024/06/05 20:10
主函数package com.example.expandlistactivity;import java.util.ArrayList;import java.util.List;import android.app.ExpandableListActivity;import android.os.Bundle;import android.widget.ExpandableListView;public class MainActivity extends ExpandableListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<String> gl = new ArrayList<String>(); List<List<String>> cl = new ArrayList<List<String>>(); gl.add("gr1"); gl.add("gr2"); gl.add("gr3"); List<String> chil1 = new ArrayList<String>(); chil1.add("c11"); chil1.add("c12"); chil1.add("c13"); chil1.add("c14"); chil1.add("c15"); chil1.add("c16"); chil1.add("c17"); chil1.add("c18"); List<String> chil2 = new ArrayList<String>(); chil2.add("c21"); chil2.add("c22"); chil2.add("c23"); chil2.add("c24"); chil2.add("c25"); chil2.add("c26"); chil2.add("c27"); chil2.add("c28"); List<String> chil3 = new ArrayList<String>(); chil3.add("c31"); chil3.add("c32"); chil3.add("c33"); chil3.add("c34"); chil3.add("c35"); chil3.add("c36"); chil3.add("c37"); chil3.add("c38"); cl.add(chil1); cl.add(chil2); cl.add(chil3); adapter e = new adapter(this, gl, cl); ExpandableListView expandableListView = getExpandableListView(); // 设置适配器 expandableListView.setAdapter(e); // 设置组图标 可以使用selector expandableListView.setGroupIndicator(getResources().getDrawable( R.drawable.away)); // 设置组图标位置 expandableListView.setIndicatorBounds(0, 180); // 设置子项图标 expandableListView.setChildIndicator(getResources().getDrawable( R.drawable.busy)); // 设置子项图标位置 expandableListView.setChildIndicatorBounds(0, 180); } }
显示expandableListView的adapter
package com.example.expandlistactivity;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class adapter extends BaseExpandableListAdapter { List<String> GroupList = new ArrayList<String>(); List<List<String>> ChildrenList = new ArrayList<List<String>>(); Context context = null; public adapter(Context c, List<String> gl, List<List<String>> cl) { context = c; GroupList.addAll(gl); ChildrenList.addAll(cl); } // 返回组的总数 @Override public int getGroupCount() { return GroupList.size(); } // 返回子项总数 @Override public int getChildrenCount(int groupPosition) { return ChildrenList.get(groupPosition).size(); } // 返回组对象 @Override public Object getGroup(int groupPosition) { return GroupList.get(groupPosition); } // 返回子项对象 @Override public Object getChild(int groupPosition, int childPosition) { return ChildrenList.get(groupPosition).get(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 false; } // 返回组VIEW @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView tv = new TextView(context); tv.setText(GroupList.get(groupPosition)); tv.setHeight(40); return tv; } // 返回子项VIEW @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView tv = new TextView(context); // for(int i=0;i<childPosition + 1;i++){//这两种方式都可以在页面显示child// tv.setText(ChildrenList.get(groupPosition).get(i)); // tv.setHeight(40); // } tv.setText(ChildrenList.get(groupPosition).get(childPosition)); tv.setHeight(40); return tv; } // 子项是否可以被选中 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ExpandableListView></LinearLayout>
0 0
- android ExpandableListActivity简介
- Android-ExpandableListActivity
- android ExpandableListActivity
- Android UI ExpandableListActivity与ExpandableListActivity
- Android上 ExpandableListActivity及CursorTreeAdapter的使用简介
- Android上 ExpandableListActivity及CursorTreeAdapter的使用简介
- Android ExpandableListActivity 学习笔记
- Android ExpandableListActivity 学习笔记
- Android基础之ExpandableListActivity
- [Android] ExpandableListActivity的使用
- Android开发--浅谈ExpandableListActivity
- (android 基础知识) ExpandableListActivity
- Android基础之ExpandableListActivity
- android ExpandableListActivity 练习
- android之ExpandableListActivity
- Android开发之ExpandableListActivity
- Android LauncherActivity,ExpandableListActivity,PreferenceActivity
- ExpandableListActivity
- 重载运算符的应用1
- Linux串口接收程序会将0x0D转换成0x0A的问题记录
- get和post 分析
- Java REST框架一览
- EDM电子邮件营销购物车挽回技巧
- android ExpandableListActivity简介
- 游标溢出问题
- HTML5拖拽
- Struts2的Action访问spring的ApplicationContext
- 内存管理
- 使用 Emmet 提高编写 CSS 的效率
- SQL之通用查询分页存储过程
- js 订单代码
- dl_iterate_phdr