android中expandablelistview的学习
16lz
2021-01-25
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
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android(安卓)拍照7.0适配(源码)
- Android使用Google Map API创建的一个根据经纬度定位的程序二
- Android(安卓)Jni , 不同 arm 架构 兼容
- Kotlin的配置(一)
- android如何阻止app_name在标题栏中闪现