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
原创粉丝点击