本篇来获取好友列表。为了像QQ一样,点击展开好友列表,所以用了ExpandableListView。
1、先来看一下效果,
2、先写布局文件,activity_linkman.xml里面拖了一个ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/catalogopen" >
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20sp"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_presence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv_username"
android:layout_alignBottom="@+id/tv_username"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/tv_username" />
<TextView
android:id="@+id/tv_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_username"
android:layout_below="@+id/tv_presence"
android:paddingTop="10sp"
android:paddingBottom="5sp"
android:text="@string/tv_friends"
android:textSize="20sp" />
<ExpandableListView
android:id="@+id/elv_linkman"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_friends" >
</ExpandableListView>
</RelativeLayout>
3、strings.xml里的代码:
<!-- 联系人 -->
<string name="tv_friends">好友</string>
<string name="imageDescription">group indicator</string>
<string name="headxiang">头像</string>
<string name="tv_friendName">廖伍星</string>
<string name="tv_sign">sorry,我忘了我想说什么了。</string>
4、LinkmanActivity.java类,先获取组和好友,获取组和好友还是看smack文档,再把它们放到ExpandableListView中,这个组件和listView差不多,同样是拖一个ExpandableListView控件,一看就明白了。
package org.hkby.lwx.activity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hkby.lwx.adapter.AllEntryExpandableAdapter;
import org.hkby.lwx.common.XmppTool;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
/**
* linkman
* @author liaowuxing
*
*/
public class LinkmanActivity extends Activity {
/**
* called when this activity is first create
*/
private ExpandableListView elv_linkman;
private List<String> groutList;
private List<List<String>> childList;
private TextView tv_username;
private TextView tv_presence;
private String account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_linkman);
SharedPreferences sp = getSharedPreferences("username",
Activity.MODE_PRIVATE);
account = sp.getString("account", "");
initView();
initClick();
getData();
}
public void initView() {
elv_linkman = (ExpandableListView) this.findViewById(R.id.elv_linkman);
//not set group indicator
elv_linkman.setGroupIndicator(null);
//login account
tv_username = (TextView) this.findViewById(R.id.tv_username);
tv_username.setText(account);
//TODO Online status
tv_presence = (TextView) this.findViewById(R.id.tv_presence);
tv_presence.setText("在线");
}
public void initClick() {
elv_linkman.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent intent = new Intent(LinkmanActivity.this,
ChatActivity.class);
Bundle bundle = new Bundle();
String username = childList.get(groupPosition).get(childPosition);
bundle.putString("name", username);
intent.putExtra("b_name", bundle);
startActivity(intent);
return true;
}
});
}
/**
* get all friends and set in the adapter
*/
public void getData() {
groutList = new ArrayList<String>();
childList = new ArrayList<List<String>>();
Roster roster = XmppTool.getConnection().getRoster();
Collection<RosterGroup> entriesGroup = roster.getGroups();
for (RosterGroup group : entriesGroup) {
Collection<RosterEntry> entries = group.getEntries();
groutList.add(group.getName());
List<String> tempList = new ArrayList<String>();
for (RosterEntry entry : entries) {
tempList.add(entry.getName());
}
childList.add(tempList);
}
AllEntryExpandableAdapter adapter = new AllEntryExpandableAdapter(groutList, childList, LinkmanActivity.this);
elv_linkman.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (XmppTool.getConnection() != null) {
XmppTool.closeConnection();
}
}
}
5、我们需要在建一个包,来专门放adapter,命名为:org.hkby.lwx.adapter,再建一个类AllEntryExpandableAdapter。
package org.hkby.lwx.adapter;
import java.util.List;
import org.hkby.lwx.activity.R;
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;
/**
* ExpandableListView's adapter
* @author liaowuxing
*
*/
public class AllEntryExpandableAdapter extends BaseExpandableListAdapter {
private List<String> groupList;
private List<List<String>> childList;
private Context context;
private LayoutInflater mInflater;
public AllEntryExpandableAdapter(List<String> groupList,
List<List<String>> childList, Context context) {
super();
this.groupList = groupList;
this.childList = childList;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(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) {
if (convertView == null) {
GroupViewHolder groupViewHolder = new GroupViewHolder();
convertView = mInflater.inflate(R.layout.group_item, null);
groupViewHolder.tv_groupName = (TextView)convertView.findViewById(R.id.tv_group);
groupViewHolder.im_icon = (ImageView)convertView.findViewById(R.id.im_icon);
convertView.setTag(groupViewHolder);
}
GroupViewHolder gHolder = (GroupViewHolder) convertView.getTag();
String groupName = groupList.get(groupPosition);
if (groupName != null) {
gHolder.tv_groupName.setText(groupName);
}else {
gHolder.tv_groupName.setText("");
}
if (isExpanded) {
gHolder.im_icon.setBackgroundResource(R.drawable.icon2);
}else {
gHolder.im_icon.setBackgroundResource(R.drawable.icon);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
ChildViewHolder cViewHolder = new ChildViewHolder();
convertView = mInflater.inflate(R.layout.child_item, null);
cViewHolder.tv_childName = (TextView)convertView.findViewById(R.id.tv_childName);
convertView.setTag(cViewHolder);
}
ChildViewHolder cHolder = (ChildViewHolder)convertView.getTag();
String childName = childList.get(groupPosition).get(childPosition);
if (childName != null) {
cHolder.tv_childName.setText(childName);
}else {
cHolder.tv_childName.setText("");
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class GroupViewHolder{
private TextView tv_groupName;
private ImageView im_icon;
}
private class ChildViewHolder{
private TextView tv_childName;
}
}
6、怎么样,不难吧,group和child分的很明显,listView是一个布局,它需要两个布局,一个是父布局,一个子布局,
父布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/catalogtopbg"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="55dp"
android:textSize="24sp" />
<ImageView
android:id="@+id/im_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:contentDescription="@string/imageDescription" />
</RelativeLayout>
子布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/im_headIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/headxiang"
android:layout_marginLeft="42dp"
android:layout_marginTop="5sp"
android:src="@drawable/headxiang" />
<TextView
android:id="@+id/tv_childName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/im_headIcon"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/im_headIcon"
android:textStyle="bold"
android:text="@string/tv_friendName"
android:textSize="18sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_childName"
android:layout_below="@+id/tv_childName"
android:text="@string/tv_sign" />
</RelativeLayout>
8、ok,好友列表完成,下一篇写聊天。