仿微信通讯录列表

来源:互联网 发布:淘宝上卖篮球鞋的店铺 编辑:程序博客网 时间:2024/05/22 06:31

自己项目中需要做一个通讯录,单是和微信不一样,微信通讯录头部的几列好像是固定的,但是项目中的头部是群组管理,是动态的,对其联系人还需要做首字母排序,效果倒是很容易做出来,但是这里只能放一个listview,不然list就不能实现随侧边栏字母滑动查找,所以自己想了个办法

分享一下,同时也是复习,希望看到的童鞋有帮助微笑


首先我重写了listview,在listview的头部添加了一个listview(注意:头部的list是添加到父list的第一行中,即position=0)

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class DrawListView extends ListView {  
  2.   
  3.     private Context context;  
  4.     private LinearLayout llList;  
  5.     public LineListView topList;  
  6.   
  7.     public DrawListView(Context context) {  
  8.         super(context);  
  9.         // TODO Auto-generated constructor stub  
  10.         this.context = context;  
  11.     }  
  12.   
  13.     public DrawListView(Context context, AttributeSet attrs, int defStyle) {  
  14.         super(context, attrs, defStyle);  
  15.         // TODO Auto-generated constructor stub  
  16.         this.context = context;  
  17.         initUI();  
  18.     }  
  19.   
  20.     public DrawListView(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.         // TODO Auto-generated constructor stub  
  23.         this.context = context;  
  24.         initUI();  
  25.     }  
  26.   
  27.     private void initUI() {  
  28.         // TODO Auto-generated method stub  
  29.         llList = (LinearLayout) LayoutInflater.from(context).inflate(  
  30.                 R.layout.top_layout, null);  
  31.         addHeaderView(llList);  
  32.   
  33.         topList = (LineListView) llList.findViewById(R.id.top_list);  
  34.     }  
  35.   
  36. }  

其中头部是linelistview(简单是自适应高度listview),这个就是父list头部的list用来做群组管理的

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class LineListView extends ListView {  
  2.   
  3.     public LineListView(Context context) {  
  4.         super(context);  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.   
  8.     public LineListView(Context context, AttributeSet attrs) {  
  9.         super(context, attrs);  
  10.         // TODO Auto-generated constructor stub  
  11.     }  
  12.       
  13.      public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){    
  14.          int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);    
  15.          super.onMeasure(widthMeasureSpec, mExpandSpec);    
  16.     }    
  17.   
  18. }  

在activity中使用:

其实也很简单,分别给对应的listview都加上监听器,经过测试发现没有问题,只是操作父list的时候要注意,他的第一行被子list给占了,不能用来显示数据

所以父list在获取数据的时候,请记得position-1(自己调试一下就知道了)

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     private DrawListView dlist;  
  4.     private ListAdapter lAdapter;  
  5.     private ListAdapter tAdapter;  
  6.   
  7.     private String[] names = new String[] { "路飞""乔巴""索隆""山治""娜美""罗宾",  
  8.             "乌索普""布鲁克""香克斯""罗杰""艾斯""桑尼号""梅丽号""弗兰奇""多福朗明哥" };  
  9.   
  10.     private String[] lol = new String[] { "好运姐""锤石""金克斯""阿狸""猴子""盖伦",  
  11.             "皇子""盲僧""日女""豹女""刀妹""刀锋""安妮""男枪""女警""鳄鱼""炮娘",  
  12.             "提莫""慎""赵信""VN""断头台""风女""莫甘娜""妖姬""&猪女""%炸弹人",  
  13.             "*寒冰""蛮子" };  
  14.     private List<String> l1 = new ArrayList<String>();  
  15.     private List<String> l2 = new ArrayList<String>();  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         dlist = (DrawListView) findViewById(R.id.list);  
  22.         dlist.setOnItemClickListener(new OnItemClickListener() {  
  23.   
  24.             @Override  
  25.             public void onItemClick(AdapterView<?> parent, View view,  
  26.                     int position, long id) {  
  27.                 // TODO Auto-generated method stub  
  28.                 Toast.makeText(MainActivity.this, l1.get(position-1), Toast.LENGTH_SHORT).show();  
  29.             }  
  30.         });  
  31.         getData();  
  32.   
  33.         lAdapter = new ListAdapter(this, l1);  
  34.         dlist.setAdapter(lAdapter);  
  35.           
  36.         tAdapter = new ListAdapter(this, l2);  
  37.         dlist.topList.setAdapter(tAdapter);  
  38.         dlist.topList.setOnItemClickListener(new OnItemClickListener() {  
  39.   
  40.             @Override  
  41.             public void onItemClick(AdapterView<?> parent, View view,  
  42.                     int position, long id) {  
  43.                 // TODO Auto-generated method stub  
  44.                 Toast.makeText(MainActivity.this, l2.get(position), Toast.LENGTH_SHORT).show();  
  45.                 addData();  
  46.                 tAdapter.notifyDataSetChanged();  
  47.             }  
  48.         });  
  49.     }  
  50.   
  51.     private void getData() {  
  52.         for (int i = 0; i < names.length; i++) {  
  53.             l1.add(names[i]);  
  54.         }  
  55.   
  56.         for (int i = 0; i < lol.length; i++) {  
  57.             l2.add(lol[i]);  
  58.         }  
  59.     }  
  60.       
  61.       
  62.     private void addData(){  
  63.         l1.add("我才是老大,懂?");  
  64.         l2.add("我才是老大,懂?");  
  65.     }  
  66. }  

简单的实现了在listview的头部加了个listview,这样listview就可以适应通讯录的侧边栏滑动到具体的字母排序的地方

Demo下载http://download.csdn.net/detail/u011440404/7784331

0 0
原创粉丝点击