1. 程式人生 > >Android -聯絡人列表

Android -聯絡人列表

1:dataBinding配置

    首先在gradle配置databinding ,

   

android {
   ...
    dataBinding {
        enabled = true
    }
   ...
}

2:dataBinding使用

activity的佈局如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
    <data>
        <variable
            name="presenter"
            type="com.zj.quickindex.MainActivity"/>
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expanded_menu"
            ></ExpandableListView>

        <android.support.v7.widget.RecyclerView
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_gravity="center_vertical|right"
            android:id="@+id/recycle_list"
            ></android.support.v7.widget.RecyclerView>

    </FrameLayout>
</layout>

然後是呼叫。

private ActivityMainBinding binding;  
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil. setContentView(this,R.layout.activity_main);
        binding.setPresenter(this);
}

其中ActivityMainBinding 命名是佈局 activity_main 去除下劃線後+binding;

最後呼叫ActivityMainBinding 的時候,可先make project 。

3:字母索引

字母索引這塊,根據佈局很明顯我這裡用的是recycleListview;

adapter這塊就不寫了,十分簡單,

主要是recycleListView 點選以及滑動時的處理;這塊沒有中間部分選中字母的展示,所以不做贅述。

 binding.recycleList.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                    
                //判斷adapter是否為空
                if (coinBarAdapter.getDataCount()>0){
                    //每個字母item的高度
                    float             height=binding.recycleList.getHeight()/coinBarAdapter.getDataCount();
                    
                    float y=0;
                    switch (event.getAction()){

                        case MotionEvent.ACTION_DOWN:

                            y=Math.abs(event.getY());
                            int downPos= (int) (y/height);//計算下標
                            //expanlistview跳轉對應的位置
                            binding.expandedMenu.setSelectedGroup(downPos);
                            break;

                        case MotionEvent.ACTION_MOVE:
                            y=Math.abs(event.getY());
                            int pos= (int) (y/height);

                            binding.expandedMenu.setSelectedGroup(pos);
//                            showToast(coinBarAdapter.getDataList().get(pos));
                            break;
                        case MotionEvent.ACTION_UP:


                            break;
                    }
                }
                return false;
            }
        });

程式碼如上,主要是ontouchListener的處理,首先計算出每個item的高度,記錄手指按下的位置,算出對應的pos下標;

手指移動時的處理相同;

4:聯絡人列表

這塊直接用了expanlistview,

adapter程式碼直接貼出

package com.zj.quickindex;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.List;

/**
 * create by zj on 2018/11/5
 */
public class ExpandCoinAdapter extends BaseExpandableListAdapter {
    private List<String> parentList;
    private List<List<PairInfoBean>> childList;
    private Context context;
    public ExpandCoinAdapter(Context context, List<String> parentList, List<List<PairInfoBean>> childList){
        this.context=context;
        this.parentList=parentList;
        this.childList=childList;
    }

    @Override
    public int getGroupCount() {
        return parentList==null?0:parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList==null?0:childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
       convertView= LayoutInflater.from(context).inflate(R.layout.item_coin_sel_parent,null);
        TextView textView=convertView.findViewById(R.id.tv_parent);
        textView.setText(parentList.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(context).inflate(R.layout.item_coin_sel_child,null);
        TextView textView=convertView.findViewById(R.id.tv_coin_name);
        textView.setText(childList.get(groupPosition).get(childPosition).pairName);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    public static class  ViewHolderParent{


    }
}

view中沒有使用viewholder,可優化

另外兩個小問題

 //去除expanlistview父佈局的箭頭
 binding.expandedListview.setGroupIndicator(null);
//預設展開所有
for (int i=0;i<expandCoinAdapter.getGroupCount();i++){
            binding.expandedListView.expandGroup(i);
}

專案地址