模仿微信界面

1运行效果图

微信
联系人
发现
我
加好按钮

2建立top.xml显示头工具条的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    android:background="#2D2E2E"
    android:layout_height="wrap_content">

    <TextView
        android:text="微信"
        android:textSize="15sp"
        android:paddingLeft="15dp"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv_search"
        android:onClick="searchClick"
        android:paddingRight="40dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/actionbar_search_icon"/>
    <ImageView
        android:onClick="addClick"
        android:id="@+id/iv_add"
        android:layout_gravity="center"
        android:paddingRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/actionbar_add_icon"/>
</LinearLayout>

3建立Activity_main布局显示上下工具条和每个工具条对应的内容布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/include"
        layout="@layout/top"/>
    <android.support.v4.view.ViewPager
        android:layout_weight="1"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ccc"/>
        <LinearLayout
            android:baselineAligned="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"
                android:onClick="wechatClick"
                android:layout_width="0dp"
                android:layout_height="match_parent">
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:id="@+id/wechat_icon_select"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/wechat_select"/>
                    <ImageView
                        android:id="@+id/wechat_icon_normal"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/wechat_normal"/>
                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/wechat_text_select"
                        android:text="微信"
                        android:textColor="#51c650"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <TextView
                        android:id="@+id/wechat_text_normal"
                        android:text="微信"
                        android:textColor="#999999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </RelativeLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:orientation="vertical"
                android:onClick="contactClick">
                <RelativeLayout
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:id="@+id/contact_icon_select"
                        android:layout_width="30dp"
                        android:background="@mipmap/contact_select"
                        android:layout_height="30dp" />
                    <ImageView
                        android:id="@+id/contact_icon_normal"
                        android:layout_width="30dp"
                        android:background="@mipmap/contact_normal"
                        android:layout_height="30dp" />
                </RelativeLayout>
                <RelativeLayout
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/contact_text_select"
                        android:text="联系人"
                        android:textColor="#51c650"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                    <TextView
                        android:id="@+id/contact_text_normal"
                        android:text="联系人"
                        android:textColor="#999999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </RelativeLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"
                android:onClick="findClick">
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/find_icon_select"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/find_select" />

                    <ImageView
                        android:id="@+id/find_icon_normal"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/find_normal" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                <TextView
                    android:id="@+id/find_text_select"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="发现"
                    android:textColor="#51C650"
                    />

                <TextView
                    android:id="@+id/find_text_normal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="发现"
                    android:textColor="#999999"/>
                </RelativeLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"
                android:onClick="meClick">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/me_icon_select"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/me_select" />

                    <ImageView
                        android:id="@+id/me_icon_normal"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/me_normal" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/me_text_select"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="我"
                        android:textColor="#51C650"/>

                    <TextView
                        android:id="@+id/me_text_normal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="我"
                        android:textColor="#999999"/>
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

4建立微信框布局fragment_1.xml用来显示微信里的内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

5建立item_chat.xml布局用显示微信框里的每条内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/item_img"
        android:src="@mipmap/default_useravatar"
        android:layout_width="64dp"
        android:layout_height="64dp" />
    <LinearLayout
        android:id="@+id/item_layout"
        android:layout_marginLeft="5dp"
        android:layout_weight="4"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_marginTop="12dp"
            android:singleLine="true"
            android:textColor="#353535"
            android:textSize="16sp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/item_content"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:ellipsize="end"
            android:textColor="#AAAAAA"
            android:textSize="14sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:id="@+id/item_time"
        android:layout_weight="1"
        android:layout_marginTop="12dp"
        android:layout_gravity="center"
        android:textColor="#AAAAAA"
        android:textSize="12sp"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>

6建立微信框布局fragment_2.xml用来显示联系人里的内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_alignParentTop="true"
        android:fadingEdge="none"
        android:background="#f0f0f0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/title"
            android:layout_marginLeft="10dip"
            android:textColor="@android:color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <ListView
        android:id="@+id/contectListView"
        android:fadingEdge="none"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
    <ImageButton
        android:id="@+id/az"
        android:background="@mipmap/a_z"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
    <RelativeLayout
        android:id="@+id/az_toast_layout"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_centerInParent="true"
        android:background="@mipmap/section_toast"
        android:visibility="gone">
        <TextView
            android:id="@+id/az_toast"
            android:layout_centerInParent="true"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

7建立item_connect.xml布局用显示联系人框里的每条内容的布局

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:background="#F0F0F0"
        android:orientation="vertical"
        android:id="@+id/key_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/key"
            android:layout_marginLeft="10dip"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/value_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dip">
        <ImageView
            android:id="@+id/valueImg"
            android:layout_marginRight="20dip"
            android:src="@mipmap/default_useravatar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/value"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

8建立微信框布局fragment_3.xml用来显示发现里的内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#DBDBDB"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/find_listview"
        android:background="#DBDBDB"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

9建立item_find.xml布局用显示发现框里的每条内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/find_distance"
        android:background="#DBDBDB"
        android:orientation="vertical"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="20dp"/>
        <LinearLayout
            android:background="#F5FFFA"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/find_left_icon"
                android:layout_marginLeft="0dp"
                android:padding="10dp"
                android:scaleType="fitCenter"
                android:src="@mipmap/default_useravatar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/find_text"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:text="朋友圈"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/find_right_icon"
                android:layout_marginRight="0dp"
                android:scaleType="fitCenter"
                android:padding="10dp"
                android:src="@mipmap/default_useravatar"
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
</LinearLayout>

10建立微信框布局fragment_4.xml用来显示我里的内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#DBDBDB"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/me_listview"
        android:background="#DBDBDB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

11建立item_me.xml布局用显示我框里的每条内容的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
    android:id="@+id/me_distance"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="#DBDBDB"
    android:orientation="vertical"
    android:visibility="gone"
    />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F5FFFA"
        android:orientation="horizontal"
        >
        <ImageView android:id="@+id/me_left_icon"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:padding="10dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/default_useravatar"></ImageView>

        <TextView android:id="@+id/me_text"
            android:layout_weight="4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp" />

        <ImageView android:id="@+id/me_right_icon"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:scaleType="fitCenter"
            android:padding="10dp"
            android:src="@mipmap/default_useravatar"
            android:visibility="gone"></ImageView>"
    </LinearLayout>
</LinearLayout>

12加号图片按钮响应布局item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:padding="3dp"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:background="@android:color/black">
    /*
    不加点击无响应
    android:descendantFocusability="blocksDescendants"
    */
    <ImageView
        android:id="@+id/iv_tb"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/user"/>
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content" />

</LinearLayout>

13开始布局activity_start.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@mipmap/welcome"
    android:layout_height="match_parent">

</LinearLayout>

14建立bean文件夹在该目录下建立ChatBean.java


public class ChatBean {
    private int resourceId;

    private String title;

    private String contant;

    private String time;

    public int getResourceId() {
        return resourceId;
    }

    public void setResourceId(int resourceId) {
        this.resourceId = resourceId;
    }

    public String getTitle() {
        return title;
    }

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

    public String getContant() {
        return contant;
    }

    public void setContant(String contant) {
        this.contant = contant;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

15建立文件夹custom建立Fragment1的适配器MyChatAdapter.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class MyChatAdapter extends ArrayAdapter<ChatBean> {
    Context context;

    int resource;

    List<ChatBean> objects;

    public MyChatAdapter(Context context, int resource, List<ChatBean> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ImageView imageView;
        TextView title;
        TextView content;
        TextView time;

        if(convertView != null){
            view = convertView;
        }else {
            view = LayoutInflater.from(context).inflate(resource,null);
        }
        imageView = (ImageView)view.findViewById(R.id.item_img);
        title = (TextView)view.findViewById(R.id.item_title);
        content = (TextView)view.findViewById(R.id.item_content);
        time = (TextView)view.findViewById(R.id.item_time);
        imageView.setImageResource(objects.get(position).getResourceId());
        title.setText(objects.get(position).getTitle());
        content.setText(objects.get(position).getContant());
        time.setText(objects.get(position).getTime());
        return view;
    }
}

16建立Fragment文件夹下建立Fragment1.java显示微信的内容

package com.example.imitatewechat.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;



public class Fragment1 extends Fragment {

    private View view;

    ListView mListView;

    private ArrayAdapter<ChatBean> adapter;

    private List<ChatBean> objects;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_1,container,false);
        ListView wechat = (ListView)view.findViewById(R.id.myListView);
        wechat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               /*打开聊天界面
                Intent intent = new Intent(getContext(), ChatActivity.class);
                intent.putExtra("name",objects.get(i).getTitle());
                startActivity(intent);*/
                Toast.makeText(getContext(),"第" + i + "条" ,Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView)view.findViewById(R.id.myListView);
        objects = new ArrayList<>();
        ChatBean chatBean = new ChatBean();
        chatBean.setResourceId(R.mipmap.default_useravatar);
        chatBean.setTitle("秦霸先");
        chatBean.setContant("我是怒苍山主");
        chatBean.setTime("昨天");
        objects.add(chatBean);

        chatBean = new ChatBean();
        chatBean.setResourceId(R.mipmap.myicon);
        chatBean.setTitle("影");
        chatBean.setContant("你好!");
        chatBean.setTime("今天");
        objects.add(chatBean);

        chatBean = new ChatBean();
        chatBean.setResourceId(R.mipmap.default_useravatar);
        chatBean.setTitle("石刚");
        chatBean.setContant("我是怒苍山五虎上将气冲塞北石刚");
        chatBean.setTime("昨天");
        objects.add(chatBean);

        chatBean=new ChatBean();
        chatBean.setResourceId(R.mipmap.default_useravatar);
        chatBean.setTitle("韩毅");
        chatBean.setContant("我是怒苍山五虎上将小吕布韩毅");
        chatBean.setTime("昨天");
        objects.add(chatBean);

        adapter = new MyChatAdapter(getActivity(),R.layout.item_chat,objects);
        mListView.setAdapter(adapter);
    }
}

17在bean文件夹下建立UserBean.java联系人的属性


public class UserBean {
    private String user;

    private String sort;

    private int imageId;

    private String locatino;

    public String getLocatino() {
        return locatino;
    }

    public void setLocatino(String locatino) {
        this.locatino = locatino;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

18在custom文件夹下建立联系人适配器MyConnectAdapter.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;


import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class MyConnectAdapter extends ArrayAdapter<UserBean> {

    Context context;

    int resource;

    List<UserBean> objects;

    SectionIndexer sectionIndexer;


    public MyConnectAdapter(Context context, int resource, List<UserBean> objects,SectionIndexer sectionIndexer) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
        this.sectionIndexer = sectionIndexer;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        LinearLayout keyLayout;
        TextView keyTextView;
        ImageView valueImageView;
        TextView valueTextView;
        if(convertView != null){
            view = convertView;
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.item_connect,null);
        }
        keyLayout = (LinearLayout)view.findViewById(R.id.key_layout);
        keyTextView = (TextView)view.findViewById(R.id.key);
        valueImageView = (ImageView)view.findViewById(R.id.valueImg);
        valueTextView = (TextView)view.findViewById(R.id.value);

        valueImageView.setImageResource(objects.get(position).getImageId());
        valueTextView.setText(objects.get(position).getUser());

        int secIndex = sectionIndexer.getSectionForPosition(position);
        if(position == sectionIndexer.getPositionForSection(secIndex)){
            if(objects.get(position).getSort().equals("#")){
                keyLayout.setVisibility(View.GONE);
            }else
                keyLayout.setVisibility(View.VISIBLE);
            keyTextView.setText(objects.get(position).getSort());
        }else {
            keyLayout.setVisibility(View.GONE);
        }

        return view;
    }
}

19在custom下建立MyCursor.java

import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;

import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class MyCursor implements Cursor {

    List<UserBean> list;

    int position;

    public MyCursor(List<UserBean> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getPosition() {
        return 0;
    }

    @Override
    public boolean move(int i) {
        return false;
    }

    @Override
    public boolean moveToPosition(int i) {
        if(i<-1||i>getCount()){
            return false;
        }

        this.position = i;
        return true;
    }

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

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

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



    @Override
    public boolean moveToPrevious() {

        return false;
    }

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

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

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

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

    @Override
    public int getColumnIndex(String s) {
        return 0;
    }

    @Override
    public int getColumnIndexOrThrow(String s) throws IllegalArgumentException {
        return 0;
    }

    @Override
    public String getColumnName(int i) {
        return null;
    }

    @Override
    public String[] getColumnNames() {
        return new String[0];
    }

    @Override
    public int getColumnCount() {
        return 0;
    }

    @Override
    public byte[] getBlob(int i) {
        return new byte[0];
    }

    @Override
    public String getString(int i) {
        return list.get(position).getSort();
    }

    @Override
    public void copyStringToBuffer(int i, CharArrayBuffer charArrayBuffer) {

    }

    @Override
    public short getShort(int i) {
        return 0;
    }

    @Override
    public int getInt(int i) {
        return 0;
    }

    @Override
    public long getLong(int i) {
        return 0;
    }

    @Override
    public float getFloat(int i) {
        return 0;
    }

    @Override
    public double getDouble(int i) {
        return 0;
    }

    @Override
    public int getType(int i) {
        return 0;
    }

    @Override
    public boolean isNull(int i) {
        return false;
    }

    @Override
    public void deactivate() {

    }

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

    @Override
    public void close() {

    }

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

    @Override
    public void registerContentObserver(ContentObserver contentObserver) {

    }

    @Override
    public void unregisterContentObserver(ContentObserver contentObserver) {

    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void setNotificationUri(ContentResolver contentResolver, Uri uri) {

    }

    @Override
    public Uri getNotificationUri() {
        return null;
    }

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

    @Override
    public void setExtras(Bundle bundle) {

    }

    @Override
    public Bundle getExtras() {
        return null;
    }

    @Override
    public Bundle respond(Bundle bundle) {
        return null;
    }
}

20在custom下建立MyActionProvider.java

import android.content.Context;
import android.view.ActionProvider;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;


/**
 * Created by Administrator on 2017/9/10.
 */

public class MyActionProvider extends ActionProvider {

    Context context;

    public MyActionProvider(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateActionView() {
        return null;
    }

    @Override
    public boolean hasSubMenu() {
        return true;
    }

    @Override
    public void onPrepareSubMenu(SubMenu subMenu) {
        subMenu.clear();
        subMenu.add(context.getString(R.string.plus_group_chat))
                .setIcon(R.mipmap.ofm_group_chat_icon)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        return true;
                    }
                });
        subMenu.add(context.getString(R.string.plus_add_friend))
                .setIcon(R.mipmap.ofm_add_icon)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        return false;
                    }
                });
        subMenu.add(context.getString(R.string.plus_video_chat))
                .setIcon(R.mipmap.ofm_video_icon)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        return false;
                    }
                });
        subMenu.add(context.getString(R.string.plus_scan))
                .setIcon(R.mipmap.ofm_qrcode_icon)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        return false;
                    }
                });
        subMenu.add(context.getString(R.string.plus_take_photo))
                .setIcon(R.mipmap.ofm_camera_icon)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        return false;
                    }
                });
        super.onPrepareSubMenu(subMenu);
    }
}

21在Fragment文件夹下Fragment2.java显示联系人



import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class Fragment2 extends Fragment {

    LinearLayout titleLayout;

    TextView title;

    ListView mListView;

    RelativeLayout sectionToastLayout;

    TextView sectionToastText;

    ImageButton alphabetButton;

    AlphabetIndexer alphabetIndexer;

    ArrayAdapter<UserBean> mAdapter;

    int lastFirstVisibleItem = -1;

    List<UserBean> listUserBeans;

    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_2,container,false);
        titleLayout = (LinearLayout)view.findViewById(R.id.title_layout);
        titleLayout.bringToFront();
        title = (TextView)view.findViewById(R.id.title);
        mListView = (ListView)view.findViewById(R.id.contectListView);
        alphabetButton = (ImageButton)view.findViewById(R.id.az);
        sectionToastLayout = (RelativeLayout)view.findViewById(R.id.az_toast_layout);
        sectionToastText = (TextView)view.findViewById(R.id.az_toast);
        ListView connect = (ListView)view.findViewById(R.id.contectListView);
        connect.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(i<=3){
                    Toast.makeText(getContext(),"我是: " + listUserBeans.get(i).getUser() , Toast.LENGTH_SHORT).show();
                }else {
                 /*打开联系人详情界面   
                 Intent intent = new Intent(getContext(), UserActivity.class);
                    intent.putExtra("name", listUserBeans.get(i).getUser());
                    intent.putExtra("location", listUserBeans.get(i).getLocatino());
                    intent.putExtra("imageId",listUserBeans.get(i).getImageId()+"");
                    startActivity(intent);*/
                    Toast.makeText(getContext(),"第" + i + "条" ,Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listUserBeans = new ArrayList<>();

        UserBean userBean = new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("B");
        userBean.setUser("Boll");
        userBean.setLocatino("西安");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.myicon);
        userBean.setSort("Y");
        userBean.setUser("影");
        userBean.setLocatino("西安");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.newfriend);
        userBean.setSort("#");
        userBean.setUser("新的朋友");
        userBean.setLocatino("");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.ofm_group_chat_icon);
        userBean.setSort("#");
        userBean.setLocatino("");
        userBean.setUser("群聊");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.aap);
        userBean.setSort("#");
        userBean.setLocatino("");
        userBean.setUser("标签");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.a68);
        userBean.setSort("#");
        userBean.setLocatino("");
        userBean.setUser("公众号");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("A");
        userBean.setLocatino("西安");
        userBean.setUser("Allen");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("Z");
        userBean.setLocatino("西安");
        userBean.setUser("Zippo");
        listUserBeans.add(userBean);
        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);

        userBean.setSort("D");
        userBean.setLocatino("西安");
        userBean.setUser("Dippo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("E");
        userBean.setLocatino("西安");
        userBean.setUser("Eippo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("O");
        userBean.setLocatino("西安");
        userBean.setUser("Oippo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("I");
        userBean.setLocatino("西安");
        userBean.setUser("Iippo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("X");
        userBean.setLocatino("西安");
        userBean.setUser("Xippo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("X");
        userBean.setLocatino("西安");
        userBean.setUser("Xypo");
        listUserBeans.add(userBean);

        userBean=new UserBean();
        userBean.setImageId(R.mipmap.default_useravatar);
        userBean.setSort("Z");
        userBean.setLocatino("西安");
        userBean.setUser("Zippt");
        listUserBeans.add(userBean);

        Collections.sort(listUserBeans, new Comparator<UserBean>() {
            @Override
            public int compare(UserBean userBean, UserBean t1) {
                return userBean.getSort().compareTo(t1.getSort());
            }
        });

        alphabetIndexer = new AlphabetIndexer(new MyCursor(listUserBeans),1,alphabet);

        mAdapter = new MyConnectAdapter(getActivity(),R.layout.item_connect,listUserBeans,alphabetIndexer);
        mListView.setAdapter(mAdapter);
        setAlpabetListener();
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                int section = alphabetIndexer.getSectionForPosition(i);
                int nextSecPosition = alphabetIndexer.getPositionForSection(section + 1);
                if(i != lastFirstVisibleItem){
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
                            titleLayout.getLayoutParams();
                    params.topMargin = 0;
                    titleLayout.setLayoutParams(params);
                    title.setText(String.valueOf(alphabet.charAt(section)));
                }
                if(nextSecPosition == i +1){
                    View childView = absListView.getChildAt(0);
                    if(childView != null){
                        int titleHeight = titleLayout.getHeight();
                        int bottom = childView.getBottom();
                        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
                                titleLayout.getLayoutParams();
                        if(bottom < titleHeight){
                            float pushedDistance = bottom - titleHeight;
                            params.topMargin = (int) pushedDistance;
                            titleLayout.setLayoutParams(params);
                        }else{
                            if(params.topMargin != 0){
                                params.topMargin = 0;
                                titleLayout.setLayoutParams(params);
                            }
                        }
                    }
                }
                lastFirstVisibleItem = i;
            }
        });
    }
    private void setAlpabetListener() {
        alphabetButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float alphabetHeight = alphabetButton.getHeight();
                float y = motionEvent.getY();
                int sectionPoistion = (int) ((y / alphabetHeight)/(1f /27f));
                if(sectionPoistion < 0){
                    sectionPoistion = 0;
                }else if(sectionPoistion >26){
                    sectionPoistion = 26;
                }
                String sectionLetter = String.valueOf(alphabet.charAt(sectionPoistion));
                int position = alphabetIndexer.getPositionForSection(sectionPoistion);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN :
                        alphabetButton.setBackgroundResource(R.mipmap.a_z_click);
                        sectionToastLayout.setVisibility(View.VISIBLE);
                        sectionToastText.setText(sectionLetter);
                        mListView.setSelection(position);
                        break;
                    case MotionEvent.ACTION_MOVE :
                        sectionToastText.setText(sectionLetter);
                        mListView.setSelection(position);
                        break;
                    default:
                        alphabetButton.setBackgroundResource(R.mipmap.a_z);
                        sectionToastLayout.setVisibility(View.GONE);
                }
                return true;
            }
        });
    }
}

22在bean文件夹下建立FindBean.java临时存储发现的属性


public class FindBean {

    private boolean isDistanceShow;

    private int leftImgId;

    private String title;

    private boolean isrightImgShow;

    private int rightImgId;

    public boolean isDistanceShow(){
        return isDistanceShow;
    }

    public void setDistanceShow(boolean distanceShow) {
        isDistanceShow = distanceShow;
    }

    public int getLeftImgId() {
        return leftImgId;
    }

    public void setLeftImgId(int leftImgId) {
        this.leftImgId = leftImgId;
    }

    public String getTitle() {
        return title;
    }

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

    public int getRightImgId() {
        return rightImgId;
    }

    public void setRightImgId(int rightImgId) {
        this.rightImgId = rightImgId;
    }

    public void setIsrightImgShow(boolean isrightImgShow) {
        this.isrightImgShow = isrightImgShow;
    }

    public boolean isIsrightImgShow(){
        return isrightImgShow;
    }
}

23在custom文件夹下建立发现适配器MyFindAdapter.java


import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class MyFindAdapter extends ArrayAdapter {

    Context context;

    int resource;

    List<FindBean> objects;

    public MyFindAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        LinearLayout topView;
        ImageView leftImageView;
        ImageView rightImageView;
        TextView title;
        if(convertView != null){
            view = convertView;
        }else {
            view = LayoutInflater.from(context).inflate(resource,null);
        }

        topView = (LinearLayout) view.findViewById(R.id.find_distance);
        leftImageView = (ImageView)view.findViewById(R.id.find_left_icon);
        rightImageView = (ImageView)view.findViewById(R.id.find_right_icon);
        title = (TextView)view.findViewById(R.id.find_text);

        if(objects.get(position).getLeftImgId() != 0){
            leftImageView.setImageResource(objects.get(position).getLeftImgId());
        }
        if(objects.get(position).isIsrightImgShow()){
            rightImageView.setImageResource(objects.get(position).getRightImgId());
            rightImageView.setVisibility(View.VISIBLE);
        }else {
            rightImageView.setVisibility(View.GONE);
        }
        if (objects.get(position).isDistanceShow()){
            topView.setVisibility(View.VISIBLE);
        }else {
            topView.setVisibility(View.GONE);
        }
        title.setText(objects.get(position).getTitle());

        return view;
    }
}

24在Fragment文件夹下Fragment3.java显示发现

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class Fragment3 extends Fragment {
    ListView findListView;
    ArrayAdapter<FindBean> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_3,container,false);
        findListView = (ListView)view.findViewById(R.id.find_listview);
        findListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(),"第" + i + "条" ,Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<FindBean> lists = new ArrayList<>();
        FindBean findBean = new FindBean();
        findBean.setDistanceShow(true);
        findBean.setIsrightImgShow(true);
        findBean.setLeftImgId(R.mipmap.find_friends);
        findBean.setRightImgId(R.mipmap.find_right);
        findBean.setTitle("朋友圈");
        lists.add(findBean);
        findBean=new FindBean();
        findBean.setDistanceShow(true);
        findBean.setIsrightImgShow(false);
        findBean.setLeftImgId(R.mipmap.find_erweima);
        findBean.setTitle("扫一扫");
        lists.add(findBean);

        findBean=new FindBean();
        findBean.setDistanceShow(false);
        findBean.setIsrightImgShow(false);
        findBean.setLeftImgId(R.mipmap.find_yaoyiyao);
        findBean.setTitle("摇一摇");
        lists.add(findBean);

        findBean=new FindBean();
        findBean.setDistanceShow(true);
        findBean.setIsrightImgShow(false);
        findBean.setLeftImgId(R.mipmap.find_fujin);
        findBean.setTitle("附近的人");
        lists.add(findBean);

        findBean=new FindBean();
        findBean.setDistanceShow(true);
        findBean.setIsrightImgShow(false);
        findBean.setLeftImgId(R.mipmap.find_gouwu);
        findBean.setTitle("购物");
        lists.add(findBean);

        findBean=new FindBean();
        findBean.setDistanceShow(false);
        findBean.setIsrightImgShow(false);
        findBean.setLeftImgId(R.mipmap.find_youxi);
        findBean.setTitle("游戏");
        lists.add(findBean);

        adapter = new MyFindAdapter(getActivity(),R.layout.item_find,lists);
        findListView.setAdapter(adapter);
    }
}

25在bean文件夹下建立MeBean.java临时存储发现的属性

public class MeBean {

    private boolean isDistanceShow;

    private int leftImgId;

    private String title;

    private boolean isrightImgShow;

    private int rightImgId;

    public boolean isDistanceShow() {
        return isDistanceShow;
    }

    public void setDistanceShow(boolean isDistanceShow) {
        this.isDistanceShow = isDistanceShow;
    }

    public int getLeftImgId() {
        return leftImgId;
    }

    public void setLeftImgId(int leftImgId) {
        this.leftImgId = leftImgId;
    }

    public String getTitle() {
        return title;
    }

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

    public int getRightImgId() {
        return rightImgId;
    }

    public void setRightImgId(int rightImgId) {
        this.rightImgId = rightImgId;
    }

    public boolean isIsrightImgShow() {
        return isrightImgShow;
    }

    public void setIsrightImgShow(boolean isrightImgShow) {
        this.isrightImgShow = isrightImgShow;
    }
}

26在custom文件夹下建立我的适配器MyMeAdapter.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class MyMeAdapter extends ArrayAdapter {

    Context context;

    int resource;

    List<MeBean> objects;

    public MyMeAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        LinearLayout topView;

        ImageView leftImageView;

        ImageView rightImageView;

        TextView title;

        if(convertView != null){
            view = convertView;
        }else {
            view = LayoutInflater.from(context).inflate(resource,null);
        }
        topView = (LinearLayout)view.findViewById(R.id.me_distance);
        leftImageView=(ImageView)view.findViewById(R.id.me_left_icon);
        rightImageView=(ImageView)view.findViewById(R.id.me_right_icon);
        title=(TextView)view.findViewById(R.id.me_text);

        if(objects.get(position).getLeftImgId()!=0){
            leftImageView.setImageResource(objects.get(position).getLeftImgId());
        }

        if(objects.get(position).isIsrightImgShow()){
            rightImageView.setImageResource(objects.get(position).getRightImgId());
            rightImageView.setVisibility(View.VISIBLE);
        }
        else {
            rightImageView.setVisibility(View.GONE);
        }
        if(objects.get(position).isDistanceShow()){
            topView.setVisibility(View.VISIBLE);
        }else {
            topView.setVisibility(View.GONE);
        }
        title.setText(objects.get(position).getTitle());

        return view;
    }
}

27在Fragment文件夹下Fragment4.java显示我的内容

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class Fragment4 extends Fragment {

    ListView meListView;

    ArrayAdapter<MeBean> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_4,container,false);
        meListView = (ListView)view.findViewById(R.id.me_listview);
        meListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(),"第" + i + "条" ,Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<MeBean> lists = new ArrayList<>();
        MeBean meBean = new MeBean();
        meBean.setDistanceShow(true);
        meBean.setIsrightImgShow(true);
        meBean.setLeftImgId(R.mipmap.logo);
        meBean.setRightImgId(R.mipmap.qrcode);
        meBean.setTitle("zgj");
        lists.add(meBean);

        meBean=new MeBean();
        meBean.setDistanceShow(true);
        meBean.setIsrightImgShow(false);
        meBean.setLeftImgId(R.mipmap.akc);
        meBean.setTitle("钱包");
        lists.add(meBean);

        meBean=new MeBean();
        meBean.setDistanceShow(true);
        meBean.setIsrightImgShow(false);
        meBean.setLeftImgId(R.mipmap.ake);
        meBean.setTitle("收藏");
        lists.add(meBean);

        meBean=new MeBean();
        meBean.setDistanceShow(false);
        meBean.setIsrightImgShow(false);
        meBean.setLeftImgId(R.mipmap.akb);
        lists.add(meBean);
        meBean.setTitle("相册");

        meBean=new MeBean();
        meBean.setDistanceShow(false);
        meBean.setIsrightImgShow(false);
        meBean.setLeftImgId(R.mipmap.agu);
        meBean.setTitle("表情");
        lists.add(meBean);

        meBean=new MeBean();
        meBean.setDistanceShow(true);
        meBean.setIsrightImgShow(false);
        meBean.setLeftImgId(R.mipmap.akf);
        meBean.setTitle("设置");
        lists.add(meBean);

        adapter = new MyMeAdapter(getActivity(),R.layout.item_me,lists);
        meListView.setAdapter(adapter);

    }
}

28建立activity文件夹将MainActivity拖进activity文件夹中

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity  {

    private ImageView wechatIconSelect;
    private ImageView wechatIconNormal;
    private ImageView contactIconSelect;
    private ImageView contactIconNormal;
    private ImageView findIconSelect;
    private ImageView findIconNormal;
    private ImageView meIconSelect;
    private ImageView meIconNormal;

    private List<ImageView> mListImageViews;
    private List<TextView> mListTextViews;
    private List<Fragment> mList;


    private FragmentPagerAdapter adapter;

    private TextView wechatTextSelect;
    private TextView wechatTextNormal;
    private TextView contactTextSelect;
    private TextView contactTextNormal;
    private TextView findTextSelect;
    private TextView findTextNormal;
    private TextView meTextSelect;
    private TextView meTextNormal;

    private ViewPager pager;

    private int curIndex;

    private final int WECHAT_INDEX = 0; // 微信菜单索引
    private final int CONTACT_INDEX = 1; // 联系人菜单索引
    private final int FIND_INDEX = 2; // 发现菜单索引
    private final int ME_INDEX = 3; // 我菜单索引

    private int[] imgIds = {R.mipmap.am6,R.mipmap.am2,
            R.mipmap.am3, R.mipmap.am4,R.mipmap.an5};

    private final int ICON_LIST_DIALOG = 1;

    private ListView listView;

    private List<String> datas;
    private List<Integer> imageId;

    private PopupWindow popupWindow;

    private ImageView iv_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        setOverflowShowingAlways();
        initView();
    }

    private void initView(){
        wechatIconSelect = (ImageView)findViewById(R.id.wechat_icon_select);
        wechatIconNormal = (ImageView)findViewById(R.id.wechat_icon_normal);
        contactIconSelect = (ImageView)findViewById(R.id.contact_icon_select);
        contactIconNormal = (ImageView)findViewById(R.id.contact_icon_normal);
        findIconSelect = (ImageView)findViewById(R.id.find_icon_select);
        findIconNormal = (ImageView)findViewById(R.id.find_icon_normal);
        meIconSelect = (ImageView)findViewById(R.id.me_icon_select);
        meIconNormal = (ImageView)findViewById(R.id.me_icon_normal);

        mListImageViews = new ArrayList<>();

        mListImageViews.add(wechatIconSelect);
        mListImageViews.add(wechatIconNormal);
        mListImageViews.add(contactIconSelect);
        mListImageViews.add(contactIconNormal);
        mListImageViews.add(findIconSelect);
        mListImageViews.add(findIconNormal);
        mListImageViews.add(meIconSelect);
        mListImageViews.add(meIconNormal);

        wechatTextSelect = (TextView)findViewById(R.id.wechat_text_select);
        wechatTextNormal = (TextView)findViewById(R.id.wechat_text_normal);
        contactTextSelect = (TextView)findViewById(R.id.contact_text_select);
        contactTextNormal = (TextView)findViewById(R.id.contact_text_normal);
        findTextSelect = (TextView)findViewById(R.id.find_text_select);
        findTextNormal = (TextView)findViewById(R.id.find_text_normal);
        meTextSelect = (TextView)findViewById(R.id.me_text_select);
        meTextNormal = (TextView)findViewById(R.id.me_text_normal);

        mListTextViews = new ArrayList<>();

        mListTextViews.add(wechatTextSelect);
        mListTextViews.add(wechatTextNormal);
        mListTextViews.add(contactTextSelect);
        mListTextViews.add(contactTextNormal);
        mListTextViews.add(findTextSelect);
        mListTextViews.add(findTextNormal);
        mListTextViews.add(meTextSelect);
        mListTextViews.add(meTextNormal);

        pager = (ViewPager)findViewById(R.id.pager);

        mList = new ArrayList<>();

        mList.add(new Fragment1());
        mList.add(new Fragment2());
        mList.add(new Fragment3());
        mList.add(new Fragment4());

        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new MyOnPageChangeListener());


        wechatTextNormal.setAlpha(0f);
        wechatIconNormal.setAlpha(0f);
        contactIconSelect.setAlpha(0f);
        contactTextSelect.setAlpha(0f);
        findIconSelect.setAlpha(0f);
        findTextSelect.setAlpha(0f);
        meTextSelect.setAlpha(0f);
        meIconSelect.setAlpha(0f);

    }
    protected void setOverflowShowingAlways(){
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.
                    getDeclaredField("sHasPermanentMenuKey");
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config,false);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    public void searchClick(View view){
        Toast.makeText(getApplicationContext(),"search",Toast.LENGTH_SHORT).show();
    }


    public void addClick(View view){
        //showDialog(ICON_LIST_DIALOG);
        showPopupWindow();
    }


    private void showPopupWindow(){
        initListView();
        //外部可触摸
        popupWindow = new PopupWindow(listView,350,460);
        popupWindow.setOutsideTouchable(true);
        //设置空的背景色
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(iv_add,0,340,150);
        //popupWindow.showAsDropDown(iv_add,0,20);
    }
    private void initListView(){
        listView = new ListView(this);
        listView.setDividerHeight(0);
        listView.setBackgroundResource(R.mipmap.listview_background);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String s = datas.get(i);
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        imageId = new ArrayList<>();
        imageId.add(R.mipmap.am6);
        imageId.add(R.mipmap.am2);
        imageId.add(R.mipmap.am3);
        imageId.add(R.mipmap.am4);
        imageId.add(R.mipmap.an5);

        datas = new ArrayList<>();
        datas.add("发起群聊");
        datas.add("添加朋友");
        datas.add("扫一扫");
        datas.add("收付款");
        datas.add("帮助与反馈");
        listView.setAdapter(new MyAdapter());
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            View v;
            if(view == null){
                v = View.inflate(viewGroup.getContext(),R.layout.item,null);
            }else v = view;
            ImageView iv_tb = (ImageView)v.findViewById(R.id.iv_tb);
            iv_tb.setImageResource(imageId.get(i));
            TextView tv_number = (TextView) v.findViewById(R.id.tv_number);
            tv_number.setTextSize(15);
            tv_number.setTextColor(Color.WHITE);
            tv_number.setText(datas.get(i));

            return v;
        }
    }


    public void wechatClick(View view){
        if(curIndex != WECHAT_INDEX){
            colorChange(curIndex,WECHAT_INDEX);
            pager.setCurrentItem(WECHAT_INDEX,false);
        }
    }
    public void contactClick(View view){
        if(curIndex != CONTACT_INDEX){
            colorChange(curIndex,CONTACT_INDEX);
            pager.setCurrentItem(CONTACT_INDEX,false);
        }
    }
    public void findClick(View view){
        if(curIndex != FIND_INDEX){
            colorChange(curIndex,FIND_INDEX);
            pager.setCurrentItem(FIND_INDEX,false);
            Log.d("debug1","ok");
        }
    }
    public void meClick(View view){
        if(curIndex != ME_INDEX){
            colorChange(curIndex,ME_INDEX);
            pager.setCurrentItem(ME_INDEX,false);
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter{

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }
    }
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Log.d("debug1","position: " + position + ", positionOffset: " + positionOffset + ",  positionOffsetPixels: " + positionOffsetPixels);
            if( positionOffset > 0){
                Log.d("debug1"," position: " + position + " curIndex : "+ curIndex);
                if(position == curIndex){
                    //colorChange(position,position - 1);
                }
                else
                    colorChange(position,curIndex);
                Log.d("debug1"," position1: " + position + " curIndex1 : "+ curIndex);
            }
        }

        @Override
        public void onPageSelected(int position) {
            //Log.d("debug1"," position: " + position + " curIndex : "+ curIndex);
            curIndex = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }


    private void colorChange(int srcIndex, int destIndex){

        mListImageViews.get(srcIndex *2 + 1).setAlpha(1f);
        mListImageViews.get(srcIndex *2 ).setAlpha(0f);

        mListImageViews.get(destIndex * 2 +1 ).setAlpha(0f);
        mListImageViews.get(destIndex * 2 ).setAlpha(1f);

        mListTextViews.get(srcIndex * 2 + 1).setAlpha(1f);
        mListTextViews.get(srcIndex * 2).setAlpha(0f);

        mListTextViews.get(destIndex * 2 + 1).setAlpha(0f);
        mListTextViews.get(destIndex * 2 ).setAlpha(1f);
    }

}

29在activity下建立StartActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;


/**
 * Created by Administrator on 2017/9/14.
 */

public class StartActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(StartActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        },500);
    }
}

30更改配置文件AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imitatewechat">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.StartActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.MainActivity"/>
    </application>

</manifest>

源码下载

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值