ExpandableLists

2016/2/1 10:16:27

1. Custom Adapter

实现

    public class Expandable1 extends ExpandableListActivity {private MyExpandableListAdapter mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mAdapter = new MyExpandableListAdapter();setListAdapter(mAdapter);//   registerForContextMenu(getExpandableListView());}/*** 监听条目选择事件* @param parent* @param v* @param groupPosition* @param childPosition* @param id* @return*/@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast.makeText(this,groupPosition+";"+childPosition,Toast.LENGTH_SHORT).show();return super.onChildClick(parent, v, groupPosition, childPosition, id);}public class MyExpandableListAdapter extends BaseExpandableListAdapter {// Sample data set.  children[i] contains the children (String[]) for groups[i].private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };private String[][] children = {{ "Arnold", "Barry", "Chuck", "David" },{ "Ace", "Bandit", "Cha-Cha", "Deuce" },{ "Fluffy", "Snuggles" },{ "Goldy", "Bubbles" }};/*** Group数量* @return*/@Overridepublic int getGroupCount() {return groups.length;}/*** 子条目数量* @param groupPosition* @return*/@Overridepublic int getChildrenCount(int groupPosition) {return children[groupPosition].length;}/*** Group条目* @param groupPosition* @return*/@Overridepublic Object getGroup(int groupPosition) {return groups[groupPosition];}/*** 获取子条目* @param groupPosition* @param childPosition* @return*/@Overridepublic Object getChild(int groupPosition, int childPosition) {return children[groupPosition][childPosition];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}/*** Group条目视图* @param groupPosition* @param isExpanded* @param convertView* @param parent* @return*/@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {TextView textView = getGenericView();textView.setText(getGroup(groupPosition).toString());return textView;}/*** 子条目视图* @param groupPosition* @param childPosition* @param isLastChild* @param convertView* @param parent* @return*/@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {TextView textView = getGenericView();textView.setText(getChild(groupPosition, childPosition).toString());return textView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}public TextView getGenericView() {// Layout parameters for the ExpandableListViewAbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);TextView textView = new TextView(Expandable1.this);textView.setLayoutParams(lp);// Center the text verticallytextView.setGravity(Gravity.CENTER_VERTICAL);// Set the text starting positiontextView.setPaddingRelative(36, 0, 0, 0);// Set the text alignmenttextView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);return textView;}}}

效果

2. Cursor

实现

    /*** Demonstrates expandable lists backed by Cursors*/public class ExpandableList2 extends ExpandableListActivity {private static final String[] CONTACTS_PROJECTION = new String[] {Contacts._ID,Contacts.DISPLAY_NAME};private static final int GROUP_ID_COLUMN_INDEX = 0;private static final String[] PHONE_NUMBER_PROJECTION = new String[] {Phone._ID,Phone.NUMBER};private static final int TOKEN_GROUP = 0;private static final int TOKEN_CHILD = 1;private static final class QueryHandler extends AsyncQueryHandler {private CursorTreeAdapter mAdapter;public QueryHandler(Context context, CursorTreeAdapter adapter) {super(context.getContentResolver());this.mAdapter = adapter;}@Overrideprotected void onQueryComplete(int token, Object cookie, Cursor cursor) {switch (token) {case TOKEN_GROUP:mAdapter.setGroupCursor(cursor);break;case TOKEN_CHILD:int groupPosition = (Integer) cookie;mAdapter.setChildrenCursor(groupPosition, cursor);break;}}}public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {// Note that the constructor does not take a Cursor. This is done to avoid querying the // database on the main thread.public MyExpandableListAdapter(Context context, int groupLayout,int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,int[] childrenTo) {super(context, null, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,childrenTo);}@Overrideprotected Cursor getChildrenCursor(Cursor groupCursor) {// Given the group, we return a cursor for all the children within that group // Return a cursor that points to this contact's phone numbersUri.Builder builder = Contacts.CONTENT_URI.buildUpon();ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX));builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY);Uri phoneNumbersUri = builder.build();mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri, PHONE_NUMBER_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE }, null);return null;}}private QueryHandler mQueryHandler;private CursorTreeAdapter mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Set up our adaptermAdapter = new MyExpandableListAdapter(this,android.R.layout.simple_expandable_list_item_1,android.R.layout.simple_expandable_list_item_1,new String[] { Contacts.DISPLAY_NAME }, // Name for group layoutsnew int[] { android.R.id.text1 },new String[] { Phone.NUMBER }, // Number for child layoutsnew int[] { android.R.id.text1 });setListAdapter(mAdapter);mQueryHandler = new QueryHandler(this, mAdapter);// Query for peoplemQueryHandler.startQuery(TOKEN_GROUP, null, Contacts.CONTENT_URI, CONTACTS_PROJECTION, Contacts.HAS_PHONE_NUMBER + "=1", null, null);}@Overrideprotected void onDestroy() {super.onDestroy();// Null out the group cursor. This will cause the group cursor and all of the child cursors// to be closed.mAdapter.changeCursor(null);mAdapter = null;}}

效果

3. Simple Adapter

实现

/*** Demonstrates expandable lists backed by a Simple Map-based adapter*/public class ExpandableList3 extends ExpandableListActivity {private static final String NAME = "NAME";private static final String IS_EVEN = "IS_EVEN";private ExpandableListAdapter mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();for (int i = 0; i < 20; i++) {Map<String, String> curGroupMap = new HashMap<String, String>();groupData.add(curGroupMap);curGroupMap.put(NAME, "Group " + i);curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");List<Map<String, String>> children = new ArrayList<Map<String, String>>();for (int j = 0; j < 15; j++) {Map<String, String> curChildMap = new HashMap<String, String>();children.add(curChildMap);curChildMap.put(NAME, "Child " + j);curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");}childData.add(children);}// Set up our adaptermAdapter = new SimpleExpandableListAdapter(this,groupData,android.R.layout.simple_expandable_list_item_1,new String[] { NAME, IS_EVEN },new int[] { android.R.id.text1, android.R.id.text2 },childData,android.R.layout.simple_expandable_list_item_2,new String[] { NAME, IS_EVEN },new int[] { android.R.id.text1, android.R.id.text2 });setListAdapter(mAdapter);}}

效果

View之ExpandableLists相关推荐

  1. view(*args)改变张量的大小和形状_pytorch reshape numpy

    20201227 这个方法是在不改变数据内容的情况下,改变一个数组的格式,参数及返回值,官网介绍: a:数组–需要处理的数据 newshape:新的格式–整数或整数数组,如(2,3)表示2行3列,新的 ...

  2. View的Touch事件分发(二.源码分析)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,先来看简单的View的touch事件分发. 主要分析View的dispatchTouchEvent()方法和onTou ...

  3. View的Touch事件分发(一.初步了解)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,先来看简单的View的touch事件分发. 一次完整的Touch事件序列为: ACTION_DOWN -> AC ...

  4. View绘制流程的入口

    View绘制流程的入口是WindowManager.add(decor, l),从Activity的创建开始分析,具体流程如下: Activity.onCreate() setContentView( ...

  5. Android中View如何刷新

    View的更新方式主要有以下3种: 1.不使用多线程和双缓冲 这种情况最简单,在View发生改变时对UI进行重绘.你只需要Activity中显式调用View对象中的invalidate()方法即可,系 ...

  6. Android自定义View基本步骤

    一.自定义属性 1.在res下的values下面新建attrs.xml 2.在布局中使用,声明命名空间 3.在自定义View构造方法中通过TypedArray获取属性 4.必须回收 array.rec ...

  7. Android自定义View —— TypedArray

    在上一篇中Android 自定义View Canvas -- Bitmap写到了TypedArray 这个属性 下面也简单的说一下TypedArray的使用 TypedArray 的作用: 用于从该结 ...

  8. Android 自定义View Canvas —— Bitmap

    Bitmap 绘制图片 常用的方法有一下几种 (1) drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint ...

  9. Android 自定义 —— View lineTo 与 rLineTo 的区别

    lineTo 的作用: 从最后一点到指定点(x,y)添加一条直线(这里大家要了解view坐标系左上角0.0 开始的) 它的特点是:绘制一条道(x,y)的一条直线 ,如果没有对此轮廓执行moveTo() ...

  10. Android 自定义View —— Canvas

    上一篇在android 自定义view Paint 里面 说了几种常见的Point 属性 绘制图形的时候下面总有一个canvas ,Canvas 是是画布 上面可以绘制点,线,正方形,圆,等等,需要和 ...

最新文章

  1. ORA-4031错误深入解析
  2. 阐述 QUEST CENTRAL FOR DB2 八罪
  3. Mysql基础运用(视图,变量,存储,流程控制)
  4. 17-Flutter移动电商实战-首页_楼层区域的编写
  5. 【转载】优秀文章转载集合
  6. NPER用计算机怎么算,计算机财务管理第三章详解.doc
  7. Gitter - 高颜值GitHub小程序客户端诞生记
  8. 基于visual Studio2013解决C语言竞赛题之1010计算
  9. Spring源码解析 -- SpringWeb请求参数获取解析
  10. swoole 安装测试
  11. 自动化运维工具(光纤交换机接口功率监控)
  12. wps表格宏被禁用如何解禁_wps宏被禁用如何打开?
  13. teechart mysql_TeeChart的X轴为时间,多个Y轴的显示
  14. 浅层砂过滤器 全自动浅层介质过滤系统
  15. 2018全球50大最佳发明名单
  16. 三级等保 服务器设置密码策略 centos
  17. 马斯克的火箭上天了,SpaceX开源项目也登上了热榜!
  18. 手机点餐系统概述_廖师兄 微信点餐系统 springcloud学习笔记
  19. SQL 模拟生成商品订单表
  20. 四叠半神话大系(bfs序+st+在线倍增+二分)(北理16校赛)

热门文章

  1. 虚假唤醒spurious wakeup
  2. 如何生成条形码并打印出来
  3. Qt使用flowlayout,使控件两端间距始终固定,垂直和水平间距相等
  4. 中国新能源汽车产业销售模式与十四五竞争格局展望报告2022版
  5. 游戏建模学习技巧分享
  6. 如何在网页title前面加logo
  7. html背景色不透明度,css怎么设置颜色不透明度?
  8. 易基因|干货:m6A RNA甲基化MeRIP-seq测序分析实验全流程解析
  9. 泰凌微8258入门教程 环境篇②——Telink IDE开发环境搭建
  10. mysql添加开机自启_【实操篇】如何设置MySQL开机自启动