树形ExpandableListView控件使用
来源:互联网 发布:阿里云短信服务 编辑:程序博客网 时间:2024/05/03 12:16
原文:http://blog.csdn.net/ly_rose/article/details/50358433
还有一篇比较完整:http://blog.csdn.net/sysukehan/article/details/51960473
创建工程Android_expandableListView中添加控件ExpandableListView
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.android_expandablelistview.MainActivity" >
- <ExpandableListView
- android:id="@+id/expandableListView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true" >
- </ExpandableListView>
- </RelativeLayout>
- package com.example.android_expandablelistview;
- import java.util.ArrayList;
- import java.util.List;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity {
- private ExpandableListView listView;
- private MyAdapter adapter;
- private List<String> group;//组
- private List<List<String>> child;//子
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView=(ExpandableListView) this.findViewById(R.id.expandableListView1);
- adapter=new MyAdapter();
- initData();
- listView.setAdapter(adapter);
- listView.setGroupIndicator(null);//把箭头去掉
- }
- //初始化数据,组和子元素实例化
- public void initData(){
- group=new ArrayList<String>();
- child=new ArrayList<List<String>>();
- addInfo("广东",new String[]{"深圳","珠海","广州"});
- addInfo("湖北",new String[]{"武汉","孝感","黄冈"});
- addInfo("河南",new String[]{"郑州","洛阳","商丘"});
- }
- public void addInfo(String g,String[] c){
- group.add(g);//添加组
- List<String> list = new ArrayList<String>();
- for(int i=0;i<c.length;i++){
- list.add(c[i]);
- }
- child.add(list);
- }
- class MyAdapter extends BaseExpandableListAdapter{
- @Override
- public int getGroupCount() {
- // TODO Auto-generated method stub
- return group.size();
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- // TODO Auto-generated method stub
- return child.size();
- }
- @Override
- public Object getGroup(int groupPosition) {
- // TODO Auto-generated method stub
- return group.get(groupPosition);
- }
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return child.get(groupPosition).get(childPosition);
- }
- @Override
- public long getGroupId(int groupPosition) {
- // TODO Auto-generated method stub
- return groupPosition;
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return childPosition;
- }
- @Override
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- TextView textView = null;
- if(convertView==null){
- textView = new TextView(MainActivity.this);
- }else{
- textView = (TextView)convertView;
- }
- textView.setText(group.get(groupPosition));
- textView.setTextSize(30);
- textView.setPadding(36, 10, 0, 10);
- return textView;
- }
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- TextView textView = null;
- if(convertView==null){
- textView = new TextView(MainActivity.this);
- }else{
- textView = (TextView)convertView;
- }
- textView.setText(child.get(groupPosition).get(childPosition));
- textView.setTextSize(20);
- textView.setPadding(72, 10, 0, 10);
- return textView;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return true;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
运行效果图如下
去掉箭头,加上
- listView.setGroupIndicator(null);//把箭头去掉
0 0
- 树形ExpandableListView控件使用
- ExpandableListView控件使用
- ExpandableListView控件的使用
- ExpandableListView控件基本使用
- 树形组件ExpandableListView的使用介绍
- Android之ExpandableListView控件使用
- MFC 树形控件使用
- 树形控件的使用
- 树形控件的使用
- (ExpandableListView)android中树形组件的使用
- Android ExpandableListview使用gson解析树形结构数据。
- android开发之--ExpandableListView控件使用心得
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用 .
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- Android中ExpandableListView控件基本使用
- CMake调用shell脚步
- Java synchronized关键字用法
- 动态规划DP 之 解码的数目问题
- Netty学习之旅------源码分析Netty内存池分配机制初探--PoolArena、PoolChunk、PoolSubpage等数据结构分析
- SQL嵌套查询
- 树形ExpandableListView控件使用
- Android适配
- elasticsearch走过的一些坑
- Spark2.0.1 on yarn with hue 集群安装部署(六)livy安装测试
- C运行时库(C Run-time Library)详解
- 信号峰拟合的MATLAB程序,包括高斯拟合,多高斯拟合等多种类型
- 最大公约数
- C#(模板模式)将父窗体继承之后重写一个按钮的事件,为什么每次都要运行两次才结束?
- maven打包报错:Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no