android开发步步为营之5:ExpandableListView模拟QQ好友列表
来源:互联网 发布:朝鲜 中国 关系 知乎 编辑:程序博客网 时间:2024/06/05 06:28
ExpandableListView可展开的列表视图,本文我们要学习的是,如何使用这个控件,实现QQ好友列表这样的效果。
一、先学习理论知识
public class
ExpandableListView
extends ListView
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.ListView
android.widget.ExpandableListView
ClassOverview
Aview that shows items in a vertically scrolling two-level list. This differsfrom the ListViewby allowing two levels: groups which can individually be expanded to show itschildren. The items come from the ExpandableListAdapterassociated with this view.
Expandablelists are able to show an indicator beside each item to display the item'scurrent state (the states are usually one of expanded group, collapsed group,child, or last child). Use setChildIndicator(Drawable) or setGroupIndicator(Drawable) (or the corresponding XML attributes) to set these indicators (see thedocs for each method to see additional state that each Drawable can have). Thedefault style for anExpandableListViewprovides indicators which will be shown next to Views given to the ExpandableListView.The layouts android.R.layout.simple_expandable_list_item_1 andandroid.R.layout.simple_expandable_list_item_2 (which should be used withSimpleCursorTreeAdapter) contain the preferred position information for indicators.
Thecontext menu information set by an ExpandableListViewwill be a ExpandableListView.ExpandableListContextMenuInfo object withpackedPositionbeing a packed position that can be used with getPackedPositionType(long) and the other similar methods.
Note: You cannot usethe value wrap_contentfor the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also notstrictly specified (for example, if the parent were ScrollView you could notspecify wrap_content since it also can be any length. However, you can usewrap_content if the ExpandableListView parent has a specific size, such as 100pixels.
二、 实践
第一步设计页面
因为我们继承了ExpandableList,所以我们不必要另外再设计一个程序界面,如果只是继承了Activity,那就必须设计一个页面,里面放上android:id="@+id/android:list"的ExpandableListView。这里我们用到了自定义的子项目页面视图evchildview.xml
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imgHead"android:src="@drawable/icon" android:layout_alignParentTop="true"android:layout_alignParentLeft="true"></ImageView>
<TextView android:layout_width="wrap_content"android:text="name"android:layout_height="wrap_content"android:id="@+id/tvName" android:layout_alignBottom="@+id/imgHead"android:layout_toRightOf="@+id/imgHead" android:layout_marginLeft="16dp" android:layout_marginBottom="15dp"></TextView>
</RelativeLayout>
第二步、设计Activity ExpandableListViewTest
/**
* ExpandableListView example
*/
package com.figo.helloworld;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author zhuzhifei
* @版权所有
*/
public class ExpandableListViewTest extends ExpandableListActivity {
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generatedmethod stub
super.onCreate(savedInstanceState);
//如果没有继承ExpandableListActivity采用方法一
//setContentView(R.layout.expandablelistviewtest);
//expandableListView=(ExpandableListView)findViewById(R.id.expandableListView1);
//expandableListView.setAdapter(expandableListAdapterTest);
//继承了ExpandableListActivity采用方法二
ExpandableListAdapterTest expandableListAdapterTest = newExpandableListAdapterTest(
ExpandableListViewTest.this);
setListAdapter(expandableListAdapterTest);
}
/**
* 点击子项时,触发点击事件
*/
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
TextView tvName = (TextView) v.findViewById(R.id.tvName);
Toast.makeText(ExpandableListViewTest.this, "name:" + tvName.getText(),
Toast.LENGTH_LONG).show();
return super.onChildClick(parent, v, groupPosition, childPosition,id);
}
/**
* 组合拢事件
*/
@Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generatedmethod stub
super.onGroupCollapse(groupPosition);
}
/**
* 组展开事件
*/
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generatedmethod stub
super.onGroupExpand(groupPosition);
}
}
第三步、设计ExpandableListView的Adapter
/**
* ExpandableListView数据适配器
*/
package com.figo.helloworld;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* @author zhuzhifei
* @版权所有
*/
public class ExpandableListAdapterTest extends BaseExpandableListAdapter {
public String[] groups = { "我的好友", "复旦", "南大", "同事" };
public String[][] children = { { "刘备", "曹操", "孙权", "奥巴马" },
{ "复复", "旦旦", "日月", "光华" }, { "阿南", "大大" }, { "马超", "赵云", "林丹" } };
public int[][] figures = {
{ R.drawable.lb, R.drawable.cc, R.drawable.sq, R.drawable.abm },
{ R.drawable.ff, R.drawable.dd, R.drawable.ry, R.drawable.gh },
{ R.drawable.an, R.drawable.dada },
{ R.drawable.mc, R.drawable.zy, R.drawable.ld } };
Context context;
public ExpandableListAdapterTest(Context ct) {
context = ct;
}
/*
* 获取组数
*/
@Override
public int getGroupCount() {
return groups.length;
}
/*
* 获取当前组的子项
*/
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
/*
* 获取当前组数据
*/
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
/*
* 获取当前子项数据
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[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;
}
/*
* 获取组视图
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
/*
* 获取子视图,这里采用自定义的xml页面,包含一个图片和一个文本框
*
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.evchildview, null);
TextView tvName = (TextView) layout.findViewById(R.id.tvName);
tvName.setText(getChild(groupPosition,childPosition).toString());
ImageView imgHead = (ImageView) layout.findViewById(R.id.imgHead);
imgHead.setImageResource(getFigure(groupPosition,childPosition));
return layout;
}
/*
* 子项是否可选
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 组视图,这里只显示一个简单的文本框
*
* @return
*/
public TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(context);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
return textView;
}
/**
* 获取肖像编号
* @param groupPosition
* @param childPosition
* @return
*/
public int getFigure(int groupPosition, int childPosition) {
return figures[groupPosition][childPosition];
}
}
第四步、AndroidManifest.xml注册Activity
<activity android:name=".ExpandableListViewTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
第五步、运行效果
合拢效果
展开效果
点击某项效果
- android开发步步为营之5:ExpandableListView模拟QQ好友列表
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- Android ExpandableListView实现QQ好友列表效果
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView) .
- Android之实现QQ好友分组(ExpandableListView)
- ExpandableListView仿QQ好友列表
- ExpandableListView仿QQ好友列表
- 仿QQ好友列表 ExpandableListView
- Android ExpandableListView 展开列表控件(手机QQ好友列表)
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- 65 Android ExpandableListView (QQ好友列表的实现)
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- C#利用反射来加载程序集,并调用程序集中有关类的方法
- unicode、utf-8、ansi的故事
- CentOS下升级系统 yum update命令 自己配置更新源
- Oracle Fusion Middleware Ⅱ: Weblogic、UCM、WebCenter Portal
- 利用数据细分目标客户群
- android开发步步为营之5:ExpandableListView模拟QQ好友列表
- Eclipse中加载Nutch遇到的问题
- 用递归方法 实现 输出正整数和等于n的所有正整数和式
- table合并单元格(Jquery 插件)
- centos6.2更新yum源
- 将nutch-1.4加载到eclipse中
- UITextView
- IPhone-多视图
- 黑马程序员---Java基础之properties集合和编码解码