功能实现:在gridview中显式系统的通讯录中联系人的信息(包括头像等详细信息并且长按可实现删除功能)
具体代码如下:
ContactFragment 联系人fragment
import java.util.List;
import com.tarena.adapter.ContactAdapter;
import com.tarena.yl.R;
import com.tarena.yy.biz.ContactManager;
import com.tarena.yy.biz.DialogManager;
import com.tarena.yy.entity.Contact;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
public class ContactFragment extends BaseFragment {
private ContactAdapter adapter;
private GridView grid_contact;
public ContactFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView=inflater.inflate(R.layout.fragment_contact, container, false);
initialUI();
setListener();
return contentView;
}
private void initialUI() {
//设置actionbar
actionBar =(LinearLayout) contentView.findViewById(R.id.contact_actionbar);
initialActionBar(-1, "联系人", R.drawable.ic_search);
grid_contact=(GridView) contentView.findViewById(R.id.gridView1_contact);
adapter=new ContactAdapter(getActivity());
grid_contact.setAdapter(adapter);
}
private void setListener() {
//添加监听
grid_contact.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Contact contact=(Contact) arg0.getItemAtPosition(arg2);
if(arg2==0){//实现联系人添加的功能
DialogManager.showAddContactDialog(getActivity());
}else if(arg2>0){
//实现查看联系人详细信息
Contact contact= adapter.getItem(arg2);
DialogManager.showDetailContactDialog(getActivity(),contact);
}
}
});
//实现长按删除该联系人
grid_contact.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Contact contact=adapter.getItem(arg2);
DialogManager.deleteContact(getActivity(), adapter, contact);
return true;
}
});
}
private void refresh() {
//清空适配器的所有数据
adapter.clearDatas();
//查询所有的联系人
List<Contact> contacts=ContactManager.getAllContact(getActivity());
//创建默认的联系人对象
Contact contact =new Contact(-1, "添加联系人",null, null, null, -1);
//添加到集合中第一位置
contacts.add(0, contact);
adapter.addDatas(contacts);
}
//每次fragment被激活的时候都会回调方法
@Override
public void onResume() {
super.onResume();
refresh();
}
ContactAdapter适配器代码
public class ContactAdapter extends MyBaseAdapter<Contact> {
public ContactAdapter(Context context) {
super(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder = null;
// 使用listview(gridview)的优化机制
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.inflate_grid_contact_01, null);
holder.image_header = (ImageView) convertView.findViewById(R.id.image_contact_grid);
holder.text_name = (TextView) convertView.findViewById(R.id.text_contact_grid);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
// 获得适配的数据项
Contact contact = (Contact) getItem(position);
if (position == 0) {
holder.image_header.setImageResource(R.drawable.ic_add_contact);
} else {
// 获得联系人头像
Bitmap header = ContactManager.getPhotoByPhotoId(context, contact.getPhoto_id());
header = ImageManager.formatBitmap(header);
holder.image_header.setImageBitmap(header);
}
holder.text_name.setText(contact.getName());
return convertView;
}
class viewHolder {
ImageView image_header;
TextView text_name;
}