ExpandableListView的使用

效果图

布局

<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

初始化

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

填充数据

KongqwExpandableListviewAdapter kongqwExpandableListviewAdapter = new KongqwExpandableListviewAdapter(this);
expandableListView.setAdapter(kongqwExpandableListviewAdapter);

Adapter

package com.example.kongqw.myapplication;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView; import java.util.ArrayList; /**
* Created by kongqw on 2015/12/21.
*/
public class KongqwExpandableListviewAdapter extends BaseExpandableListAdapter { private Context mContext;
private ArrayList<String> mGroups;
private ArrayList<String> mChilds; // 构造方法
public KongqwExpandableListviewAdapter(Context context) {
mContext = context;
// 模拟初始化数据
mGroups = new ArrayList<String>();
mGroups.add("Group 1");
mGroups.add("Group 2");
mGroups.add("Group 3");
mGroups.add("Group 4");
mGroups.add("Group 5");
mChilds = new ArrayList<String>();
mChilds.add("Child 1");
mChilds.add("Child 2");
mChilds.add("Child 3");
mChilds.add("Child 4");
mChilds.add("Child 5");
mChilds.add("Child 6");
mChilds.add("Child 7");
mChilds.add("Child 8");
mChilds.add("Child 9");
mChilds.add("Child 10");
} @Override
public int getGroupCount() {
return mGroups.size();
} @Override
public int getChildrenCount(int groupPosition) {
return mChilds.size();
} @Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
} @Override
public Object getChild(int groupPosition, int childPosition) {
return mChilds.get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.expandable_group_item, null);
TextView textView = (TextView) view.findViewById(R.id.group_item);
textView.setText(mGroups.get(groupPosition));
return view;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.expandable_child_item, null);
TextView textView = (TextView) view.findViewById(R.id.child_item);
textView.setText(mChilds.get(childPosition));
return view;
} @Override
public boolean hasStableIds() {
return true;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

去掉箭头

expandableListView.setGroupIndicator(null);

默认展开

// 设置ExpandableListView默认是展开的
for (int i = 0; i < kongqwExpandableListviewAdapter.getGroupCount(); i++) {
expandableListView.expandGroup(i);
}

Group不可点击

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// TODO Auto-generated method stub
return true;
}
});

TODO 复用

最新文章

  1. hduoj 1251 统计难题
  2. leetcode@ [355] Design Twitter (Object Oriented Programming)
  3. neural style论文解读
  4. linux命令find应用
  5. pygame系列_游戏窗口显示策略
  6. Oracle表介绍--簇表
  7. 【python】BIF及查看函数帮助
  8. MYSQL 企业常用架构与调优经验分享
  9. Java程序设计第2次作业
  10. Spring框架之使用JdbcTemplate开发Dao层程序
  11. tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode=&#39;CONSTANT&#39;)
  12. 清理 Xcode 10
  13. 理解 block
  14. Linux文件与目录管理(一)
  15. SpringBoot入门(IDEA篇)(一)
  16. ARP地址解析协议原理
  17. lambda续集——1
  18. mac下同时安装jdk1.7和jdk1.8
  19. 沉淀再出发:OpenStack初探
  20. shell的tr命令

热门文章

  1. ABP公共结构
  2. JAVA循环结构示例
  3. ORA-09925: Unable to create audit trail file带来的sqlplus / as sysdba无法连接
  4. spring boot / cloud (四) 自定义线程池以及异步处理@Async
  5. [LeetCode] Path Sum IV 二叉树的路径和之四
  6. 2018.4.16Spring.Net入门学习内容
  7. win10 jkd配置注意事项
  8. ConcurrentHashMap源码分析(1.8)
  9. [ZJOI 2006]书架
  10. HNOI2018 滚粗记