android中expandablelistview的学习
来源:互联网 发布:数据准确的重要性 编辑:程序博客网 时间:2024/05/21 03:55
android中经常需要用到expanablelistview,今天学习了expandablelistview,并附上源码
首先定义parent布局的xml文件expandable_parent_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/imageid" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /><TextView android:id="@+id/textid" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" /></LinearLayout>
child的布局文件如下,比较简单,只包含一个TextView,expandalbe_child_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textid" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
在实现自定义的adapter,继承自BaseExpandableListAdapter,,,,MyExpandableAdapter.java如下:
package com.example.expandablelistview;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyExpandableAdapter extends BaseExpandableListAdapter {private Context context;private String [][]child = new String[][]{{"firstone","firsttwo","firstthree","firstfour"},{"secondone","secondtwo","secondthree","secondfour"},{"thirdone","thirdtwo","thirdthree"}};private int[]images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};private String[]parent = new String[]{"parentone","parenttwo","parentthree"};public MyExpandableAdapter(Context context) {super();this.context = context;}@Overridepublic Object getChild(int arg0, int arg1) {// TODO Auto-generated method stubreturn child[arg0][arg1];}@Overridepublic long getChildId(int arg0, int arg1) {// TODO Auto-generated method stubreturn arg1;}@Overridepublic View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {// TODO Auto-generated method stubView view = LayoutInflater.from(context).inflate(R.layout.expandalbe_child_item,null);TextView textView = (TextView) view.findViewById(R.id.textid);textView.setText(child[arg0][arg1]);return view;}@Overridepublic int getChildrenCount(int arg0) {// TODO Auto-generated method stubreturn child[arg0].length;}@Overridepublic Object getGroup(int arg0) {// TODO Auto-generated method stubreturn parent[arg0];}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn parent.length;}@Overridepublic long getGroupId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {// TODO Auto-generated method stubView view = LayoutInflater.from(context).inflate(R.layout.expandable_parent_item,null);ImageView imageView = (ImageView) view.findViewById(R.id.imageid);TextView textView = (TextView) view.findViewById(R.id.textid);imageView.setImageResource(images[arg0]);textView.setText(parent[arg0]);return view;<pre name="code" class="java">package com.example.expandablelistview;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ExpandableListView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandlistid);MyExpandableAdapter adapter = new MyExpandableAdapter(this);listView.setAdapter(adapter);}@Overridepublic 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;}}
}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isChildSelectable(int arg0, int arg1) {// TODO Auto-generated method stubreturn false;}}
最后,MainActivity.java如下;
package com.example.expandablelistview;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ExpandableListView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandlistid);MyExpandableAdapter adapter = new MyExpandableAdapter(this);listView.setAdapter(adapter);}@Overridepublic 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;}}
源码下载链接:http://download.csdn.net/detail/mockingbirds/8052699
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的使用
- 如何写Makefile(二)——规则篇(下)
- (第二章)Unity3D用键盘控制物体旋转
- java代理模式总结
- 4 UI系统和事件处理
- Winsock 2 入门指南
- android中expandablelistview的学习
- 交叉编译iftop
- JVM-翻译]揭开java.lang.OutOfMemoryError面纱之一
- Lua学习之1 :C++调用Lua函数
- IOS开发中用到的加密算法
- jquery获得select option的值 和对select option的操作
- 比较JS合并数组的各种方法及其优劣
- 数据库自动备份
- 2-06. 数列求和(20)