UI组件之ExpandableListView
来源:互联网 发布:火力银电淘宝哪家店有 编辑:程序博客网 时间:2024/05/18 01:23
UI组件之ExpandableListView
ExpandableListView是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用列表分为几组,每个组里有可以包含多个列表项。
ExpandableListView的整体思路
1.要给ExpandableListView设置适配器,必须先设置数据源。
2.数据源,就需要写一个适配器类去继承BaseExpandableListAdapter类
3.设置完数据源之后,直接给用ExpandableListView的setAdapter方法即可
demo
主界面布局
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ExpandableListView android:id="@+id/expand_listview" android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>
父列表界面布局
<?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" > <ImageView android:id="@+id/image_group" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
子列表界面布局
<?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:paddingLeft="30dp" android:orientation="horizontal" > <ImageView android:id="@+id/image_child" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/tv_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
适配器代码
package com.shake.day10_android_3.adapter;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;import com.shake.day10_android_3.R;/** * Created by Administrator on 2016/1/26. */public class MyAdapter extends BaseExpandableListAdapter { private String[] groups; private String[][] childs; private int[][] images; public MyAdapter(String[] groups, String[][] childs, int images[][]) { this.groups = groups; this.childs = childs; this.images = images; } @Override public int getGroupCount() { return groups.length; } @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childs[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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group, parent, false); } TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group); ImageView image_group = (ImageView) convertView.findViewById(R.id.image_group); tv_group.setText(groups[groupPosition]); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child, parent, false); } ImageView image_child = (ImageView) convertView.findViewById(R.id.image_child); TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child); tv_child.setText(childs[groupPosition][childPosition]); image_child.setImageResource(images[groupPosition][childPosition]); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }}
Activity代码
package com.shake.day10_android_3;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.Toast;import com.shake.day10_android_3.adapter.MyAdapter;public class MainActivity extends AppCompatActivity implements ExpandableListView.OnChildClickListener { private ExpandableListView listView; private String[] groups = {"美女", "也是美女"}; private String[][] childs = {{"baby", "陈乔恩", "高圆圆", "龚璎菲"}, {"李小璐", "唐嫣", "杨幂", "姚笛"}}; private int[][] images = {{R.drawable.baby, R.drawable.chenqiaoen, R.drawable.gaoyuanyuan, R.drawable.gongyinfei}, {R.drawable.lixiaolu, R.drawable.tangyan, R.drawable.yangmi, R.drawable.yaodi}}; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ExpandableListView) findViewById(R.id.expand_listview); myAdapter = new MyAdapter(groups, childs, images); listView.setAdapter(myAdapter); listView.setOnChildClickListener(this); } /** * listview的点击监听事件 * * @param parent * @param v * @param groupPosition * @param childPosition * @param id * @return */ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(this,childs[groupPosition][childPosition],Toast.LENGTH_LONG).show(); return true; }}
效果
0 0
- UI组件之ExpandableListView
- 组件使用之ExpandableListView
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- Android UI 之 ExpandableListActivity、ExpandableListView
- android学习之----ExpandableListView组件
- Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView
- Android之ExpandableListView(树形组件)的使用方法
- UI组件之Spinner
- UI组件之AutoCompleteTextView
- UI组件之TimePiker
- UI组件之PopupWindow
- UI组件之Notification
- vaadin之UI组件
- Android UI设计:ExpandableListView
- Android UI之TextView组件
- Android UI之CheckBox组件
- Android UI 之ListView组件
- Android UI 之Spinner组件
- GitHub上史上最全的Android开源项目分类汇总
- Particle Filter Tutorial 粒子滤波:从推导到应用(二)
- 自己动手开发一个 Web 服务器(一)
- RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout
- 数据库SQL优化大总结之 百万级数据库优化方案
- UI组件之ExpandableListView
- 理解Python的With语句
- Ubuntu下vim+gdb环境搭建
- GPU编程—cuda并行机制
- 最优雅退出 Android 应用程序的 6 种方式
- 依赖倒转原则
- 设置vs2010的默认包含路径
- (总结)密码破解之王:Ophcrack彩虹表(Rainbow Tables)原理详解(附:120G彩虹表下载)
- UITextView 禁止弹出复制