最终实现的效果:
群聊的整体是ListView,在适配器中,他含有三种子View,一个是搜索框,一个是新建群组,还有一个是群组列表。真个Adapter继承自ArrayAdapter。
这里重写的方法包括:
1.getItemViewType
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else if (position == getCount() - 1) {
return 1;
} else {
return 2;
}
}
2.getViewTypeCount
@Override
public int getViewTypeCount() {
return 3;
}
3.getCount:
@Override
public int getCount() {
return super.getCount() + 2;
}
4.getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == 0) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.search_bar_with_padding, null);
}
final EditText query = (EditText) convertView
.findViewById(R.id.query);
final ImageButton clearSearch = (ImageButton) convertView
.findViewById(R.id.search_clear);
query.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
getFilter().filter(s);
if (s.length() > 0) {
clearSearch.setVisibility(View.VISIBLE);
} else {
clearSearch.setVisibility(View.INVISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
clearSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
query.getText().clear();
}
});
} else if (getItemViewType(position) == 1) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_add_group, null);
}
((ImageView) convertView.findViewById(R.id.avatar))
.setImageResource(R.drawable.roominfo_add_btn);
((TextView) convertView.findViewById(R.id.name)).setText("新建群组");
} else {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_group, null);
}
((TextView) convertView.findViewById(R.id.name)).setText(getItem(
position - 1).getGroupName());
}
return convertView;
}
完整的代码:
package com.dystu.impro.adapter;
import java.util.List;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.dystu.impro.R;
import com.easemob.chat.EMGroup;
public class GroupAdapter extends ArrayAdapter<EMGroup> {
private LayoutInflater inflater;
public GroupAdapter(Context context, int res, List<EMGroup> groups) {
super(context, res, groups);
this.inflater = LayoutInflater.from(context);
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else if (position == getCount() - 1) {
return 1;
} else {
return 2;
}
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getCount() {
return super.getCount() + 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == 0) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.search_bar_with_padding, null);
}
final EditText query = (EditText) convertView
.findViewById(R.id.query);
final ImageButton clearSearch = (ImageButton) convertView
.findViewById(R.id.search_clear);
query.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
getFilter().filter(s);
if (s.length() > 0) {
clearSearch.setVisibility(View.VISIBLE);
} else {
clearSearch.setVisibility(View.INVISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
clearSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
query.getText().clear();
}
});
} else if (getItemViewType(position) == 1) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_add_group, null);
}
((ImageView) convertView.findViewById(R.id.avatar))
.setImageResource(R.drawable.roominfo_add_btn);
((TextView) convertView.findViewById(R.id.name)).setText("新建群组");
} else {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_group, null);
}
((TextView) convertView.findViewById(R.id.name)).setText(getItem(
position - 1).getGroupName());
}
return convertView;
}
}