android的ExpandableListView
来源:互联网 发布:js settimeout原理 编辑:程序博客网 时间:2024/05/18 11:01
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">写listview其实都大同小异,首先你得声明一个listview,一般这个时候需要一个布局listview来匹配这个listview,布局的作用就是来告诉电脑这个listview长什么样;其次你的listview得设置一个适配器adapter,适配器就是用来关联listview里面的什么位置显示什么内容</span>
ExpandableListView list = (ExpandableListView)findViewById(R.id.list);
list.setAdapter(adapter);
大体上就是这么两行简单的代码,之后就是填东西了,比如adapter你得自己写,还有listview要展示的内容,你也得写,一点一点补充完整就可以了。
如果需要点击里面的内容实现跳转或其他效果,再加一个listener就可以了,list.setonclicklistener(...)。
package com.example.expandablelistviewdemo4;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AbsListView.LayoutParams;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ExpandableListAdapter adapter = new BaseExpandableListAdapter(){private int[] images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};private String[] armTypes = new String[]{"A","B","C"};private String[][] arms = new String[][]{{"1","2","3"},{"11","12","13"},{"21","22","23"}};@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn arms[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {// TODO Auto-generated method stubLinearLayout ll = new LinearLayout(MainActivity.this);ll.setOrientation(0);ImageView logo = new ImageView(MainActivity.this);logo.setImageResource(images[groupPosition]);ll.addView(logo);TextView textView = getTextView();textView.setText(getChild(groupPosition,childPosition).toString());ll.addView(textView);return ll;}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn arms[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn armTypes[groupPosition];}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn armTypes.length;}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {// TODO Auto-generated method stubTextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());return textView;}private TextView getTextView(){@SuppressWarnings("deprecation")AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,40);TextView textView = new TextView(MainActivity.this);textView.setLayoutParams(lp);return textView;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}};ExpandableListView list = (ExpandableListView)findViewById(R.id.list);list.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition, long id) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent);return false;}});list.setAdapter(adapter);}}
0 0
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android的ExpandableListView
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android-ExpandableListView的使用
- Android ExpandableListView的技巧
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- android学习:ExpandableListView的运用
- android之 ExpandableListView的使用
- Android中ExpandableListView的使用
- 『IOS』objective-C: nonatomic retain copy assgin 等属性详解
- iOS 用CornerStone配置SVN,HTTP及svn简单使用说明
- svn异常:subversion.javahl.ClientException
- 第十一周 项目6:(4)输出1000以内的所有回文素数
- 【转】VS2010 设置main函数输入参数
- android的ExpandableListView
- SVN 创建分支
- cocos2dx3.0之读取cocostudio的UI控件
- Quartz 概要学习笔记
- 《c程序设计语言》读书笔记-3.5-按要求进制位数字转字符串
- android 带你从源码分析LayoutInflater工作原理
- switch老化时间过长导致主机不通解决一例
- php 单例模式应用-改进版
- 动态内存分配