Android studio 學習 之 內容提供者獲得手機聯繫人 多佈局

效果

介紹:讀取手機聯繫人,左邊佈局用recyclerview實現 右邊用listview實現
最終實現點擊右邊字母 左邊滑動到字母相應位置(scrollToPosition) 並且點擊實現撥打電話功能
在這裏插入圖片描述
activity佈局

       <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".homework.Activity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_weight="8"
        android:layout_width="0dp"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
    <ListView
        android:id="@+id/listid"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

Phone類

public class Phone {
    public static final int TITLE=0;
    public static final int PERSON=1;
    private String name;
    private String number;
    private int type;
    private String title;

    public Phone(String name, String number, int type, String title) {
        this.name = name;
        this.number = number;
        this.type = type;
        this.title = title;
    }

    public static int getTITLE() {
        return TITLE;
    }

    public static int getPERSON() {
        return PERSON;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

適配器

    public class Myrecycleadpater extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private ArrayList<Phone> phones;
    private OnListItemClick onListItemClick;

    public void setOnListItemClick(OnListItemClick onListItemClick) {
        this.onListItemClick = onListItemClick;
    }

    public Myrecycleadpater(Context context, ArrayList<Phone> phones) {
        this.context = context;
        this.phones = phones;
    }

    @Override
    public int getItemViewType(int position) {
        if (phones.get(position).getType()==Phone.TITLE){
            return 0;
        }else {
            return 1;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i==0){
            return new Titleholder(LayoutInflater.from(context).inflate(R.layout.item_title,viewGroup,false));
        }else {
            return new Personholder(LayoutInflater.from(context).inflate(R.layout.item_person,viewGroup,false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (getItemViewType(i)==0){
            Titleholder titleholder = (Titleholder) viewHolder;
             titleholder.title.setText(phones.get(i).getTitle());
        }else {
            Personholder personholder = (Personholder) viewHolder;
            personholder.name.setText(phones.get(i).getName());
            personholder.phone.setText(phones.get(i).getNumber());
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onListItemClick.OnItemClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return phones.size();
    }
    class Titleholder extends RecyclerView.ViewHolder{
        TextView title;
        public Titleholder(@NonNull View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.tv_title);
        }
    }
    class Personholder extends RecyclerView.ViewHolder{
        TextView name;
        TextView phone;
        public Personholder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            phone = itemView.findViewById(R.id.tv_phone);
        }
    }
}

適配器中的兩個佈局xml
recycler_item1

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="180dp">
    <ImageView
        android:id="@+id/pp_thumb"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:background="#fff"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="200dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/pp_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="佛山市順德魯粵傢俱廠"
            android:gravity="center"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#16000000"/>

        <TextView
            android:id="@+id/pp_address"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="廣東"
            android:textColor="#66000000"
            android:gravity="center"/>

    </LinearLayout>
</LinearLayout>

recycler_item2

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="180dp">
    <LinearLayout
        android:background="#fff"
        android:layout_height="200dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="佛山市順德魯粵傢俱廠"
            android:id="@+id/pp_title"/>

        <TextView
            android:background="#16000000"
            android:layout_height="3dp"
            android:layout_width="match_parent"/>

        <TextView android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="廣東"
            android:id="@+id/pp_address"
            android:textColor="#66000000"/>

    </LinearLayout>

    <ImageView
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:id="@+id/pp_thumb"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>

activity代碼

   public class Activity extends AppCompatActivity {
    private RecyclerView recycle;
    private ListView listid;
    private ArrayList<Phone> phones = new ArrayList<>();
    private ContentResolver contentResolver;
    private String tit=null;
    private Myrecycleadpater myrecycleadpater;
    private ArrayList<String> word = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_);

        recycle = (RecyclerView) findViewById(R.id.recycle);
        listid = (ListView) findViewById(R.id.listid);

        final String[] s ={"A", "B", "C", "D", "E", "F", "G", "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for (int i = 0; i <s.length; i++) {
            word.add(s[i]);
        }
        Mylistadpater mylistadpater = new Mylistadpater(this, word);
        listid.setAdapter(mylistadpater);


        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS
                    ,Manifest.permission.CALL_PHONE},101);
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycle.setLayoutManager(manager);
        myrecycleadpater = new Myrecycleadpater(this, phones);
        recycle.setAdapter(myrecycleadpater);

        myrecycleadpater.setOnListItemClick(new OnListItemClick() {
            @Override
            public void OnItemClick(int position) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+phones.get(position).getNumber()));
                startActivity(intent);
            }
        });
        listid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 String w = word.get(position);
                for (int i = 0; i < phones.size(); i++) {
                    if (phones.get(i).getTitle().equals(w)){
                        Toast.makeText(Activity.this, w, Toast.LENGTH_SHORT).show();
                        recycle.scrollToPosition(i);
                    }
                }
            }
        });

        Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(uri, null, null, null, "phonebook_label");
        if (cursor!=null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String title=cursor.getString(cursor.getColumnIndex("phonebook_label"));
                if (title.equals(tit)){
                    phones.add(new Phone(name,number,Phone.PERSON,title));
                }else {
                    phones.add(new Phone(name,number,Phone.TITLE,title));
                    phones.add(new Phone(name,number,Phone.PERSON,title));
                    tit=title;
                }
            }
        }
        myrecycleadpater.notifyDataSetChanged();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章