自己项目中需要做一个通讯录,单是和微信不一样,微信通讯录头部的几列好像是固定的,但是项目中的头部是群组管理,是动态的,对其联系人还需要做首字母排序,效果倒是很容易做出来,但是这里只能放一个listview,不然list就不能实现随侧边栏字母滑动查找,所以自己想了个办法
分享一下,同时也是复习,希望看到的童鞋有帮助
首先我重写了listview,在listview的头部添加了一个listview(注意:头部的list是添加到父list的第一行中,即position=0)
- public class DrawListView extends ListView {
- private Context context;
- private LinearLayout llList;
- public LineListView topList;
- public DrawListView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- this.context = context;
- }
- public DrawListView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- this.context = context;
- initUI();
- }
- public DrawListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- this.context = context;
- initUI();
- }
- private void initUI() {
- // TODO Auto-generated method stub
- llList = (LinearLayout) LayoutInflater.from(context).inflate(
- R.layout.top_layout, null);
- addHeaderView(llList);
- topList = (LineListView) llList.findViewById(R.id.top_list);
- }
- }
其中头部是linelistview(简单是自适应高度listview),这个就是父list头部的list用来做群组管理的
- public class LineListView extends ListView {
- public LineListView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public LineListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
- int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, mExpandSpec);
- }
- }
在activity中使用:
其实也很简单,分别给对应的listview都加上监听器,经过测试发现没有问题,只是操作父list的时候要注意,他的第一行被子list给占了,不能用来显示数据
所以父list在获取数据的时候,请记得position-1(自己调试一下就知道了)
- public class MainActivity extends Activity {
- private DrawListView dlist;
- private ListAdapter lAdapter;
- private ListAdapter tAdapter;
- private String[] names = new String[] { "路飞", "乔巴", "索隆", "山治", "娜美", "罗宾",
- "乌索普", "布鲁克", "香克斯", "罗杰", "艾斯", "桑尼号", "梅丽号", "弗兰奇", "多福朗明哥" };
- private String[] lol = new String[] { "好运姐", "锤石", "金克斯", "阿狸", "猴子", "盖伦",
- "皇子", "盲僧", "日女", "豹女", "刀妹", "刀锋", "安妮", "男枪", "女警", "鳄鱼", "炮娘",
- "提莫", "慎", "赵信", "VN", "断头台", "风女", "莫甘娜", "妖姬", "&猪女", "%炸弹人",
- "*寒冰", "蛮子" };
- private List<String> l1 = new ArrayList<String>();
- private List<String> l2 = new ArrayList<String>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- dlist = (DrawListView) findViewById(R.id.list);
- dlist.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, l1.get(position-1), Toast.LENGTH_SHORT).show();
- }
- });
- getData();
- lAdapter = new ListAdapter(this, l1);
- dlist.setAdapter(lAdapter);
- tAdapter = new ListAdapter(this, l2);
- dlist.topList.setAdapter(tAdapter);
- dlist.topList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, l2.get(position), Toast.LENGTH_SHORT).show();
- addData();
- tAdapter.notifyDataSetChanged();
- }
- });
- }
- private void getData() {
- for (int i = 0; i < names.length; i++) {
- l1.add(names[i]);
- }
- for (int i = 0; i < lol.length; i++) {
- l2.add(lol[i]);
- }
- }
- private void addData(){
- l1.add("我才是老大,懂?");
- l2.add("我才是老大,懂?");
- }
- }
简单的实现了在listview的头部加了个listview,这样listview就可以适应通讯录的侧边栏滑动到具体的字母排序的地方