• 效果展示:在搜索框输入手机号码,点击搜索图标,在RecyclerView中显示查询结果和推荐好友。
  • 思路:首先RecyclerView是多type的,也就是说RecyclerView中有不同的item,一个是绿色部分的标题,一个是真正的显示结果:用户的头像,性别,昵称,年龄等信息。
  • 步骤
  • 新建一个标题item的layout(layout_search_title_item)和显示用户信息item的laypout(layout_search_content_item);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_title"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:background="@color/colorAccent"
        android:paddingLeft="15dp"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    </TextView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="80dp">
        <!--头像加性别-->
        <RelativeLayout
            android:layout_width="60dp"
            android:layout_height="60dp">
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/circleImageView_item"
                android:src="@drawable/ic_launcher2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </de.hdodenhof.circleimageview.CircleImageView>
            <ImageView
                android:id="@+id/iv_sex"
                android:layout_alignParentBottom="true"
                android:src="@drawable/img_boy_icon"
                android:layout_alignParentRight="true"
                android:layout_width="20dp"
                android:layout_height="20dp">
            </ImageView>
        </RelativeLayout>
        <!--右边的文字-->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <!--昵称加年龄-->
            <LinearLayout
                android:gravity="center_vertical"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:textStyle="bold"
                    android:maxLines="1"
                    android:textColor="@color/colorPrimary"
                    android:gravity="center_vertical"
                    android:id="@+id/tv_nickName"
                    android:text="nickname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </TextView>
                <TextView
                    android:id="@+id/tv_age"
                    android:layout_marginLeft="20dp"
                    android:text="age"
                    android:maxLines="1"
                    android:gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </TextView>
            </LinearLayout>
            <!--联系-->
            <LinearLayout
                android:visibility="gone"
                android:orientation="horizontal"
                android:id="@+id/ll_contact_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/contact_name"
                    android:text="name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </TextView>
                <TextView
                    android:id="@+id/contact_phone"
                    android:layout_marginLeft="10dp"
                    android:text="phone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </TextView>
            </LinearLayout>
            <!--文字超过文本框用跑马灯效果-->
            <!--设置跑马灯效果无限循环-->
            <TextView
                android:text="desc"
                android:singleLine="true"
                android:gravity="center_vertical"
                android:id="@+id/tv_desc"
                android:marqueeRepeatLimit="marquee_forever"
                android:ellipsize="marquee"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="match_parent">
            </TextView>
        </LinearLayout>
        <ImageView
            android:src="@drawable/img_right_arrow"
            android:layout_width="30dp"
            android:layout_height="30dp">
        </ImageView>

    </LinearLayout>


</RelativeLayout>

布局效果:

recycleview notifyItemRangeInserted 会跳转到第一行 recyclerview viewtype_xml


recycleview notifyItemRangeInserted 会跳转到第一行 recyclerview viewtype_ci_02

  • 新建Add_FriendsActivity,并默认有布局,布局文件代码如下:布局中添加了include标签,主要是用于在显示没有结果的时候,显示没有数据的修饰图片。
<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UI.Add_FriendsActivity">
    <!--头部-->
    <LinearLayout
        android:gravity="center_vertical"
        android:background="@color/colorPrimary"
        android:paddingLeft="15dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <ImageView
            android:src="@drawable/img_contact_icon"
            android:layout_width="25dp"
            android:layout_height="wrap_content">
        </ImageView>
        <TextView
            android:gravity="center"
            android:text="从通讯录中导入"
            android:textColor="@android:color/white"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
    </LinearLayout>
    <LinearLayout
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/phone_num"
            android:layout_margin="10dp"
            android:inputType="phone"
            android:maxLength="11"
            android:paddingLeft="5dp"
            android:hint="@string/phonenumber_addfriends"
            android:background="@drawable/login_edit_bg"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp">
        </EditText>
        <ImageView
            android:id="@+id/iv_search"
            android:layout_marginRight="10dp"
            android:src="@drawable/img_search"
            android:layout_width="50dp"
            android:layout_height="30dp">
        </ImageView>

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </androidx.recyclerview.widget.RecyclerView>

    <include layout="@layout/layout_empty_view"
        android:id="@+id/empty_data"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

    </include>


</LinearLayout>

布局效果:

recycleview notifyItemRangeInserted 会跳转到第一行 recyclerview viewtype_android_03

  • 新建一个model的包,在包里新建一个AddFriendModel的类,这个类用于描述item里的所有属性,如果你学过《第一行代码》的话,就是类似于学RecyclerView组件时候的Fruit类。
package model;

/**
 * Created By LicaiWen
 * To DO:
 */
//这个类类似于Fruit类,包括显示的类型(标题还是内容),标题名字,id,头像,昵称,性别,简介等。
public class AddFriendModel {
    //类型,是标题还是内容
    private int type;
    //标题
    private String title;
    //内容
    private String userId;
    private String Photo;
    private String nickName;
    private int age;
    private boolean sex;
    private String desc;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    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 String getPhoto() {
        return Photo;
    }

    public void setPhoto(String photo) {
        Photo = photo;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
  • 接着是给RecyclerView写一个适配器:新建adapter的包,在里面新建Java类AddFriendAdapter继承RecyclerView.Adapter<recyclerView.holder>.
package com.example.meet.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.meet.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import helper.GlideHelper;
import model.AddFriendModel;

/**
 * Created By LicaiWen
 * To DO:
 */
//添加好友,搜索结果的recycleView的适配器
public class AddFriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_TITLE=0;
    public static final int TYPE_CONTENT=1;
   private Context context;
   private List<AddFriendModel> mlist;
   private LayoutInflater inflater;
    private OnClickListener clickListener=null;

//为什么要传入content,因为构造LayoutInflater
    //为什么要构造LayoutInflater?因为要加载item布局
    public AddFriendAdapter(Context context, List<AddFriendModel> mlist) {
        this.context = context;
        this.mlist = mlist;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
//用onCreateViewHolder的viewType判断是标题还是内容
    //onCreateViewHolder是利用viewHold而的构造函数创建viewHolder的实例并返回
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        if(viewType==TYPE_TITLE){
           return new TitleViewHolder(inflater.inflate(R.layout.layout_search_title_item,null));
        }else if(viewType==TYPE_CONTENT){
            return new ContentViewHolder(inflater.inflate(R.layout.layout_search_content_item,null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AddFriendModel addFriendModel=mlist.get(position);
        if(addFriendModel.getType()==TYPE_TITLE){
            ((TitleViewHolder)holder).tv_title.setText(addFriendModel.getTitle());
        }else if(addFriendModel.getType()==TYPE_CONTENT){
            //设置昵称
            ((ContentViewHolder)holder).tv_nickName.setText(addFriendModel.getNickName());
            //设置性别
            ((ContentViewHolder)holder).sex.setImageResource(addFriendModel.isSex()?R.drawable.img_boy_icon:R.drawable.img_girl_icon);
           //设置头像
            GlideHelper.loadUrl(context,addFriendModel.getPhoto(), ((ContentViewHolder)holder).circleImageView);
            //设置简介
            ((ContentViewHolder)holder).desc.setText(addFriendModel.getDesc());
            //设置年龄
            ((ContentViewHolder)holder).age.setText(addFriendModel.getAge()+"岁");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onClick(position);
                }
            }
        });


      /*  setClickListener(new OnClickListener() {
            @Override
            public void onClick( position) {
                if (clickListener != null) {
                    clickListener.onClick(position);
                }
            }
        });
*/
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }
//判断是标题还是内容
    @Override
    public int getItemViewType(int position) {
        return mlist.get(position).getType();
    }
//viewHolder是初始化item里的组件的函数
    class TitleViewHolder extends RecyclerView.ViewHolder{
         private TextView tv_title;

        public TitleViewHolder( View itemView) {
            super(itemView);
            tv_title=itemView.findViewById(R.id.tv_title);
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_nickName;
        private TextView age;
        private CircleImageView circleImageView;
        private ImageView sex;
        private TextView desc;


        public ContentViewHolder( View itemView) {
            super(itemView);
            tv_nickName=itemView.findViewById(R.id.tv_nickName);
            age=itemView.findViewById(R.id.tv_age);
            circleImageView=itemView.findViewById(R.id.circleImageView_item);
            sex=itemView.findViewById(R.id.iv_sex);
            desc=itemView.findViewById(R.id.tv_desc);
        }
    }
    public void setClickListener(OnClickListener listener){
      clickListener=listener;
    }
    public interface  OnClickListener{
        void onClick (int position);
    }
}
  • 现在到了在Activity中写 控制代码:
package com.example.meet.UI;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.framework.Manager.BmobManager;
import com.example.framework.base.BaseBackActivity;
import com.example.framework.bmob.MyBmobUser;
import com.example.framework.utils.CommonUtils;
import com.example.framework.utils.LogUtils;
import com.example.meet.Adapter.AddFriendAdapter;
import com.example.meet.R;

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

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import model.AddFriendModel;

public class Add_FriendsActivity extends BaseBackActivity implements View.OnClickListener {

    private EditText phoneNum;
    private ImageView ivSearch;
    private RecyclerView recycleview;
    private AddFriendAdapter addFriendAdapter;
    private List<AddFriendModel> mlist = new ArrayList<>();
    private View view_empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__friends);
        initView();
    }

    private void initView() {
        phoneNum = (EditText) findViewById(R.id.phone_num);
        ivSearch = (ImageView) findViewById(R.id.iv_search);
        recycleview = (RecyclerView) findViewById(R.id.recycleview);
        ivSearch.setOnClickListener(this);
        recycleview.setLayoutManager(new LinearLayoutManager(this));
        //每项item的下划线
        recycleview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        addFriendAdapter = new AddFriendAdapter(this, mlist);
        recycleview.setAdapter(addFriendAdapter);
        view_empty = findViewById(R.id.empty_data);
        addFriendAdapter.setClickListener(new AddFriendAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(Add_FriendsActivity.this, "你点击了item", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search:
                queryByPhone();
                // Toast.makeText(this, "已被点击", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    //通过电话查询
    private void queryByPhone() {
        String mobliePhone = phoneNum.getText().toString().trim();
        if (TextUtils.isEmpty(mobliePhone)) {
            Toast.makeText(this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            //过滤自己
            String phoneNum = BmobManager.getBmobManagerInstance().getMyBombUser().getMobilePhoneNumber();
            if (phoneNum.equals(mobliePhone)) {
                Toast.makeText(this, "不能查询自己", Toast.LENGTH_SHORT).show();
                return;
            }
            BmobManager.getBmobManagerInstance().queryMyBmobUserByPhone(mobliePhone, new FindListener<MyBmobUser>() {
                @Override
                public void done(List<MyBmobUser> list, BmobException e) {
                    if (e == null) {
                        if (!CommonUtils.isEmpty(list)) {
                            // mlist.add(list.get(0));
                            LogUtils.i(list.get(0).toString());
                            //在recyclerView显示查找结果和推荐结果
                            MyBmobUser myBmobUser = (MyBmobUser) list.get(0);
                            view_empty.setVisibility(View.GONE);
                            recycleview.setVisibility(View.VISIBLE);
                            mlist.clear();
                            addTitle("查询结果");
                            addContent(myBmobUser);
                            addFriendAdapter.notifyDataSetChanged();
                            LogUtils.i("mlist.get(0).toStrig:" + mlist.get(0).toString());
                            LogUtils.i("size:" + mlist.size() + "");
                            LogUtils.i("myBmobUser.toString:" + myBmobUser.toString());
                            //显示推荐好友
                            pushUser();

                        } else {
                            //显示空数据
                            view_empty.setVisibility(View.VISIBLE);
                            recycleview.setVisibility(View.GONE);
                            Toast.makeText(Add_FriendsActivity.this, "没有该用户", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(Add_FriendsActivity.this, "查询失败" + e.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

    }

    //用于显示标题
    public void addTitle(String title) {
        AddFriendModel addFriendModel = new AddFriendModel();
        addFriendModel.setType(AddFriendAdapter.TYPE_TITLE);
        addFriendModel.setTitle(title);
        mlist.add(addFriendModel);
        //刷新适配器
        // addFriendAdapter.notifyDataSetChanged();

    }

    //用于显示数据内容
    public void addContent(MyBmobUser myBmobUser) {
        AddFriendModel addFriendModel = new AddFriendModel();
        addFriendModel.setType(AddFriendAdapter.TYPE_CONTENT);
        addFriendModel.setUserId(myBmobUser.getObjectId());
        addFriendModel.setPhoto(myBmobUser.getPhoto());
        addFriendModel.setAge(myBmobUser.getAge());
        addFriendModel.setDesc(myBmobUser.getDoc());
        addFriendModel.setNickName(myBmobUser.getNickName());
        addFriendModel.setSex(myBmobUser.isSex());
        mlist.add(addFriendModel);
        //刷新适配器
        //addFriendAdapter.notifyDataSetChanged();
    }

    public void pushUser() {
        BmobManager.getBmobManagerInstance().queryAllUser(new FindListener<MyBmobUser>() {
            @Override
            public void done(List<MyBmobUser> list, BmobException e) {
                if (e == null) {
                    if (!CommonUtils.isEmpty(list)) {
                        addTitle("推荐好友");
                        int num = list.size() <= 100 ? list.size() : 100;
                        //不能自己推荐给自己
                        String phoneNum = BmobManager.getBmobManagerInstance().getMyBombUser().getMobilePhoneNumber();
                        for (int i = 0; i < num; i++) {
                            if (phoneNum.equals(list.get(i).getMobilePhoneNumber())) {
                                //跳过本次循环
                                continue;
                            }
                            addContent(list.get(i));
                        }
                        addFriendAdapter.notifyDataSetChanged();

                    }
                } else {
                    Toast.makeText(Add_FriendsActivity.this, "推荐失败:" + e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
  • 最后展示Activity中代码的相关类:
    MyBombUser是继承BmobUser,多写了用户的相关属性。
package com.example.framework.bmob;

import cn.bmob.v3.BmobUser;

/**
 * Created By LicaiWen
 * To DO:
 */
public class MyBmobUser extends BmobUser {


    //token属性
    //获取token的头像地址
    private String tokenPhoto;
    //获取token的昵称
    private String tokenNickName;
    //基本属性
    //昵称
    private String nickName;
    //头像
    private String photo;
    //其他属性
    //性别 true为男,false为女
    private boolean sex = true;
    //简介
    private String doc;
    //年龄
    private int age = 0;
    //生日
    private String birthday;
    //星座
    private String constellation;
    //爱好
    private String hobby;
    //用户id
    //private

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    private String status;

    public String getTokenPhoto() {
        return tokenPhoto;
    }

    public void setTokenPhoto(String tokenPhoto) {
        this.tokenPhoto = tokenPhoto;
    }

    public String getTokenNickName() {
        return tokenNickName;
    }

    public void setTokenNickName(String tokenNickName) {
        this.tokenNickName = tokenNickName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public String getDoc() {
        return doc;
    }

    public void setDoc(String doc) {
        this.doc = doc;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getConstellation() {
        return constellation;
    }

    public void setConstellation(String constellation) {
        this.constellation = constellation;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "MyBmobUser{" +
                "tokenPhoto='" + tokenPhoto + '\'' +
                ", tokenNickName='" + tokenNickName + '\'' +
                ", nickName='" + nickName + '\'' +
                ", photo='" + photo + '\'' +
                ", sex=" + sex +
                ", doc='" + doc + '\'' +
                ", age=" + age +
                ", birthday='" + birthday + '\'' +
                ", constellation='" + constellation + '\'' +
                ", hobby='" + hobby + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}

BombManager是MyBombUser的管理类,里面用了单例模式获取实例,和MyBmobUser的如查询等相关方法。

package com.example.framework.Manager;

import android.content.Context;
import android.util.Log;

import com.example.framework.bmob.MyBmobUser;
import com.example.framework.entity.StaticClass;
import com.example.framework.utils.LogUtils;

import java.io.File;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import io.rong.imlib.RongIMClient;

import static cn.bmob.v3.BmobUser.signOrLoginByMobilePhone;

/**
 * Created By LicaiWen
 * To DO:
 */
//bmob管理类
public class BmobManager {
    public static final String BMOB_APPID = "8c4aa55ae7076048f72fa40e05945293";

    private volatile static BmobManager bmobManager = null;

    private BmobManager() {

    }

    public static BmobManager getBmobManagerInstance() {
        if (bmobManager == null) {
            synchronized (BmobManager.class) {
                if (bmobManager == null) {
                    bmobManager = new BmobManager();
                }
            }


        }
        return bmobManager;
    }

    public void initBmob(Context context) {
        Bmob.initialize(context, BMOB_APPID);
        // RongIMClient.init(context,StaticClass.RONGYUN_APPID);


    }

    //发送验证码
    public void sendVerifyNumber(String phone, QueryListener<Integer> listener) {
        BmobSMS.requestSMSCode(phone, "", listener);
    }

    //核对验证码
    public void ensureVerifyNumber(String phone, String code, LogInListener<MyBmobUser> listener) {
        signOrLoginByMobilePhone(phone, code, listener);
    }

    //获取登录成功的对象
    public MyBmobUser getMyBombUser() {
        return BmobUser.getCurrentUser(MyBmobUser.class);
    }

    public boolean isLogining() {
        return BmobUser.isLogin();
    }

    //用于获取用户第一次填写的昵称和头像,更新用户信息
    public void firstUploadPhoto(final String nickName, File storageFile, final UploadPhotoListener listener) {

        //获取当前登录的bmob用户
        final MyBmobUser myBmobUser = getMyBombUser();
        //上传头像文件到bmob
        final BmobFile bmobFile = new BmobFile(storageFile);
        bmobFile.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //上传成功
                    //更新用户信息
                    myBmobUser.setNickName(nickName);
                    myBmobUser.setPhoto(bmobFile.getFileUrl());
                    //更新用户token
                    myBmobUser.setTokenNickName(nickName);
                    myBmobUser.setTokenPhoto(bmobFile.getFileUrl());
                    myBmobUser.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                //更新成功
                                listener.uploadDone();
                            } else {
                                //更新失败
                                listener.uploadFail(e);
                            }
                        }
                    });

                } else {
                    //上传失败
                    listener.uploadFail(e);
                }
            }
        });


    }

    //根据电话查询用户
    public void queryMyBmobUserByPhone(String mobilePhone, FindListener<MyBmobUser> listener) {
        queryBase("mobilePhoneNumber", mobilePhone, listener);
        // LogUtils.i(mobilePhone);
    }

    //    查询所有的用户
    public void queryAllUser(FindListener<MyBmobUser> listener) {
        BmobQuery<MyBmobUser> query = new BmobQuery<>();
        query.findObjects(listener);
    }

    public void queryBase(String key, String value, FindListener<MyBmobUser> listener) {
        BmobQuery<MyBmobUser> query = new BmobQuery<>();
        query.addWhereEqualTo(key, value);
        query.findObjects(listener);

    }
//写一个接口处理上传失败和成功;
    public interface UploadPhotoListener {
        void uploadDone();

        void uploadFail(BmobException e);
    }
}

CommonUtils是判断List是否为空的常用工具类。

package com.example.framework.utils;

import java.util.List;

/**
 * Created By LicaiWen
 * To DO:
 */
//常用工具类
public class CommonUtils {
    public  static boolean isEmpty(List list){
        if(list!=null&& list.size()>0){
            return false;
        }else {
            return true;
        }
    }
}