上一节,已经完成了来电管家的界面设计,那么下面就要实现具体的功能了,如何将添加的黑白名单显示呢?这里用到了ListView,那么,如果需要删除黑白名单呢,是一个个长按弹出菜单删除,还是将所的黑白名单清空呢,这都不符合用户的需求,往往,都是删除多个,这就有个问题了,如何在ListView中删除指定的多个item呢??可能大家想到了,要用到CheckBox。
先看图:
可以看出,当处于删除模式时,底部按钮也变成了删除与返回,中间也显示了当前共选择了多少项,而且在ListView的每一个Item右边也显示出了CheckBox,用于多选。
这一节,只实现如何显示,并且为ListView添加监听器,下节再实现的黑白名单的增加与删除。
黑白名单的布局文件上一节已给出,既然用到了ListView,必然要有Adapter,这里依然采用的是BaseAdapter,主要代码如下:
class Adapter extends BaseAdapter{ private Context context; private LayoutInflater inflater=null; private HashMap<Integer, View> mView ; public HashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox public HashMap<Integer, Boolean> ischeck; private TextView txtcount; public Adapter(Context context,TextView txtcount) { this.context = context; this.txtcount = txtcount; inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mView = new HashMap<Integer, View>(); visiblecheck = new HashMap<Integer, Integer>(); ischeck = new HashMap<Integer, Boolean>(); if(isMulChoice){ for(int i=0;i<array.size();i++){ ischeck.put(i, false); visiblecheck.put(i, CheckBox.VISIBLE); } }else{ for(int i=0;i<array.size();i++) { ischeck.put(i, false); visiblecheck.put(i, CheckBox.INVISIBLE); } } } public int getCount() { // TODO Auto-generated method stub return array.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return array.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = mView.get(position); if(view==null) { view = inflater.inflate(R.layout.list_item, null); TextView id = (TextView)view.findViewById(R.id.tv_id); TextView name = (TextView)view.findViewById(R.id.tv_name); TextView number = (TextView)view.findViewById(R.id.tv_number); final CheckBox ceb = (CheckBox)view.findViewById(R.id.cb_choose); Person p = array.get(position); id.setText(p.id+""); name.setText(p.name); number.setText(p.number); ceb.setChecked(ischeck.get(position)); ceb.setVisibility(visiblecheck.get(position)); view.setOnLongClickListener(new Onlongclick()); view.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if(isMulChoice){ if(ceb.isChecked()){ ceb.setChecked(false); selectid.remove(array.get(position)); }else{ ceb.setChecked(true); selectid.add(array.get(position)); } txtcount.setText("共选择了"+selectid.size()+"项"); }else { //Toast.makeText(context, "点击了"+array.get(position), Toast.LENGTH_LONG).show(); } } }); mView.put(position, view); } return view; } class Onlongclick implements OnLongClickListener{ public boolean onLongClick(View v) { // TODO Auto-generated method stub isMulChoice = true; selectid.clear(); add_layout.setVisibility(View.GONE); delete_layout.setVisibility(View.VISIBLE); for(int i=0;i<array.size();i++) { adapter.visiblecheck.put(i, CheckBox.VISIBLE); } adapter = new Adapter(context,txtcount); lv_show.setAdapter(adapter); return true; } } }
其中的Onlongclick是给ListView的item添加长按监听器,实现弹出CheckBox,及底部删除按钮的功能。这里面用到了控件的隐藏与显示,剩下的就是控件的声明与定义。并为ListView绑定监听器。
private List<Person> array = new ArrayList<Person>(); private List<Person> selectid = new ArrayList<Person>(); private ListView lv_show; private CheckBox cb; private TextView tv_count; private RelativeLayout add_layout; private RelativeLayout delete_layout; private Adapter adapter;
其中Person是自定义的类:
Person.java
public class Person { String name; String number; int id; public Person(int id,String name,String number) { this.id = id; this.name = name; this.number = number; } }
tv_count = (TextView)findViewById(R.id.tv_select); add_layout = (RelativeLayout)findViewById(R.id.add_layout); delete_layout = (RelativeLayout)findViewById(R.id.delete_layout); lv_show = (ListView)findViewById(R.id.lv_show);
这里只是主要的代码,完整的代码我会打包上传的。
时间: 2024-11-05 13:50:28