Android ExpandableListView
来源:互联网 发布:应用数据可以删除吗 编辑:程序博客网 时间:2024/05/22 06:43
ExpandableListVIew 是可展开收起的列表,相关XML属性
android:childDivider 指定各组内各子列表项之间的分隔条
android:chhildIndicator 显示在子列表项旁边的Drawable对象
android:groupIndicator 显示在组列表项旁边的Drawable对象
ExpandableListAdapter有三种常用方式:
1.扩展BaseExpandableListAdapter
2.使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter
3.使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter
本例程使用第一种方式实现
Item子表列项事件监听:
setOnItemClickListener();
setOnItemSelectedListener();
setOnItemLongClickListener();
package shortcut.song.com.myapplication;import android.database.DataSetObserver;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import org.w3c.dom.Text;public class ExpandableListViewActivity extends AppCompatActivity { ExpandableListView mExpandableListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable_list_view); ExpandableListAdapter expandableListAdapter = new BaseExpandableListAdapter() { private int[] logos = new int[] { R.drawable.img1, R.drawable.img2, R.drawable.img3, }; private String[] animalTypes = new String[] { "dog","cat","fish" }; private String[][] animal = new String[][] { {"dog1","dog2","dog3","dog4"}, {"cat1","cat2","cat3"}, {"fish1","fish2"} }; @Override public int getGroupCount() { return animalTypes.length; } @Override public int getChildrenCount(int groupPosition) { return animal[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return animalTypes[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return animal[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(ExpandableListViewActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36,0,0,0); textView.setTextSize(20); return textView; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView logoImage = new ImageView(ExpandableListViewActivity.this); logoImage.setImageResource(logos[groupPosition]); ll.addView(logoImage); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition,childPosition).toString()); return textView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }; mExpandableListView = (ExpandableListView)findViewById(R.id.expandable); mExpandableListView.setAdapter(expandableListAdapter); }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_expandable_list_view" 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="shortcut.song.com.myapplication.ExpandableListViewActivity"> <ExpandableListView android:id="@+id/expandable" android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>
效果如下图:
0 0
- ExpandableListView Android
- android ExpandableListView
- android ExpandableListView
- android expandablelistview
- android ExpandableListView
- android ExpandableListView
- android ExpandableListView
- Android ExpandableListView
- Android ExpandableListView
- Android ExpandableListView
- Android ExpandableListView显示模板
- android ExpandableListView使用
- android-ExpandableListView(2)
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android控件之ExpandableListView
- Android 自定义 ExpandableListView
- Android ExpandableListView样式定制
- ActivityThread解析
- 基于dubbo框架下的RPC通讯协议性能测试
- 几个applicationcontext实现类
- 第11章 样式(1)——概述、创建和使用
- Windows安装redis
- Android ExpandableListView
- docker入门
- 一只小蜜蜂...
- [BZOJ2055]80人环游世界(有源汇有上下界的费用流)
- Jackson框架介绍
- C语言 · 高精度乘法
- 汽水瓶——华为研发工程师编程题
- UVA-804 模拟
- 消息队列----RabbitMQ基础概念详细介绍