ExpandableListView的使用
来源:互联网 发布:php 今日头条 编辑:程序博客网 时间:2024/06/05 11:49
andriod ExpandableListView的使用
在iOS中UITableView被经常用到,当我们设置tableview的style为group的时候,它就以分组的形式呈现出来,同样的,在andriod中也有类似的UI控件:ExpandableListView。
下面来实现这样一个效果:
1、布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:cacheColorHint="#00000000" android:listSelector="#00000000" > </ExpandableListView></LinearLayout>
2、java文件
package com.example.expandablelistview2;import android.R.color;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AbsListView;import android.widget.AbsListView.LayoutParams;import android.widget.AbsoluteLayout;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.Toast;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.os.Build;public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { // 设置组视图的图片 int[] logs = new int[]{R.drawable.wj,R.drawable.bmw,R.drawable.b}; // 设置组视图显示的文字 private String[] generalsTypes = new String[] {"奥迪","宝马","奔驰"}; // 子视图显示的文字 private String[][] generals = new String[][]{ { "奥迪Q1", "奥迪Q2", "奥迪A3", "奥迪Q4", "奥迪Q5", "奥迪Q6" }, { "宝马X1", "宝马X2", "宝马X3", "宝马X4", "宝马X5", "宝马X6" }, { "奔驰E1", "奔驰S2", "奔驰S3", "奔驰S4", "奔驰S5" } }; // 子视图的图片 public int[][] generallogos = new int[][]{ { R.drawable.wj, R.drawable.wj, R.drawable.wj, R.drawable.wj, R.drawable.wj, R.drawable.wj }, { R.drawable.bmw, R.drawable.bmw, R.drawable.bmw, R.drawable.bmw, R.drawable.bmw, R.drawable.bmw }, { R.drawable.b, R.drawable.b, R.drawable.b, R.drawable.b, R.drawable.b } }; // 获得文字的方法 TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView( MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); // textView.setTextColor(color.black); return textView; } // 重写ExpandableListAdapter方法 @Override public boolean isChildSelectable(int arg0, int arg1) { // TODO Auto-generated method stub return true; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } @Override public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { // TODO Auto-generated method stub // 代码创建ImageView LinearLayout lineaLayout = new LinearLayout(MainActivity.this); lineaLayout.setOrientation(0); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logs[arg0]); logo.setPadding(50, 15, 0, 0); lineaLayout.addView(logo); // 代码创建textView TextView textView = getTextView();// textView.setTextColor(color.black); textView.setText(getGroup(arg0).toString()); lineaLayout.addView(textView); return lineaLayout; } @Override public long getGroupId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public int getGroupCount() { // TODO Auto-generated method stub return generalsTypes.length; } @Override public Object getGroup(int arg0) { // TODO Auto-generated method stub return generalsTypes[arg0]; } @Override public int getChildrenCount(int arg0) { // TODO Auto-generated method stub return generals[arg0].length; } @Override public View getChildView(int arg0, int arg1, boolean arg2, View arg3, ViewGroup arg4) { // TODO Auto-generated method stub LinearLayout lineraLayout = new LinearLayout(MainActivity.this); lineraLayout.setOrientation(0); ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(generallogos[arg0][arg1]); imageView.setPadding(10, 15, 0, 0); lineraLayout.addView(imageView); TextView textView = getTextView(); textView.setText(getChild(arg0, arg1).toString()); textView.setTextSize(15); lineraLayout.addView(textView); return lineraLayout; } @Override public long getChildId(int arg0, int arg1) { // TODO Auto-generated method stub return arg1; } @Override public Object getChild(int arg0, int arg1) { // TODO Auto-generated method stub return generals[arg0][arg1]; } }; ExpandableListView expandableListView = (ExpandableListView)this.findViewById(R.id.list); expandableListView.setAdapter(adapter); expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你点击了" + adapter.getChild(arg2, arg3), 1).show(); return false; } }); }}
0 0
- ExpandableListView的使用
- ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- 转载:ExpandableListView的使用
- ExpandableListView的使用
- expandablelistview的使用
- Android ExpandableListView的使用
- ExpandableListView组件的使用
- ExpandableListView的基本使用
- ExpandableListView的使用
- ExpandableListView的使用示例
- ExpandableListView的使用
- ExpandableListView的使用总结
- Android ExpandableListView的使用
- ExpandableListView的使用
- Android ExpandableListView的使用
- ExpandableListview的使用介绍
- VS2010中为OpenCV工程创建属性单
- 【dp】NOIP2010提高组乌龟棋
- <css 十八>图片的透明
- Knight Moves(HDU 1372)(BFS)
- 新手必须掌握的Linux命令
- ExpandableListView的使用
- 类加载器
- UVA - 414 Machined Surfaces
- 【OpenCV】功能:展示OpenCV的图像绘制功能
- Caffe源码阅读(3)Softmax层和SoftmaxLoss层
- BestCoder Round #81 (div.2) CC String
- 一个三流城市小团队程序员的随笔(1)
- POJ2253
- Java File