李晨晨:
本次主要将上周实现的图片加载方面的功能整合到photoSelectActivity。效果如下图所示:
左图为图片显示及选择界面,分为3列;右图为不同图片文件夹的选择。
主要内容如下
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_photo_selector);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//弹出一个窗口,让背后的窗口变暗一点
- initView();
- initImageList();
- initPopupWindow();
- loadImageData();
- }
1.initView主要绑定view id及设置OnClickListenere;
- private void initView() {
- mImageListView = (RecyclerView) findViewById(R.id.recycler_view);
- mBtnSource = (RadioButton) findViewById(R.id.btn_source_img);
- mTvFolderName = (TextView) findViewById(R.id.tv_folder_name);
- mTvPreview = (TextView) findViewById(R.id.tv_preview);
- mIvBack = (ImageView) findViewById(R.id.iv_back);
- mTvSend = (TextView) findViewById(R.id.tv_send);
- mLayoutBottom = (RelativeLayout) findViewById(R.id.layout_bottom);
- mIvBack.setOnClickListener(this);
- mTvSend.setOnClickListener(this);
- mTvFolderName.setOnClickListener(this);
- mTvPreview.setOnClickListener(this);
- mBtnSource.setOnClickListener(this);
- // 文件选择按钮,只有在加载数据完成并且数据不为空的时候才可以点击
- mTvFolderName.setClickable(false);
- // 发送和预览按钮只有在有照片选择的情况下才可以点击
- mTvPreview.setClickable(false);
- mTvSend.setClickable(false);
- }
2.initImageList主要用于初始化照片选择列表,使用recycle view显示
- private void initImageList() {
- mImageBeans = new ArrayList<>();
- mImageListView.setLayoutManager(new GridLayoutManager(this, 3));
- mImageListView.addItemDecoration(new GridItemDecoration(this));
- mImageAdapter = new RecycleViewAdapter<ImageBean>(this, mImageBeans) {
- @Override
- public int setItemLayoutId(int position) {
- return R.layout.item_image_sel;
- }
- @Override
- public void bindView(RViewHolder holder, final int position) {
- // 使用Glide加载照片,它有缓存策略,避免 OOM
- final ImageBean item = mImageBeans.get(position);
- ImageView imageView = holder.getImageView(R.id.iv_img);//有就拿到,没有就先加入RViewHolder的mViews列表再返回
- Glide.with(PhotoSelectActivity.this)
- .load(item.getPath()).diskCacheStrategy(DiskCacheStrategy.ALL)
- .error(R.drawable.bg_img_defalut)
- .into(imageView);
- // 单击照片本身,在预览窗口打开该照片
- imageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mShowItem = item;
- previewByItem();
- }
- });
- // 照片右上角的选择器,根据照片被选择与否显示不同状态 icon
- final RadioButton radioButton = (RadioButton) holder.getConvertView()
- .findViewById(R.id.img_sel_status);
- radioButton.setChecked(item.isSelected());
- // 选择框单击事件监听,用于更新照片选择状态
- radioButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ImageBean imageBean = mImageBeans.get(position);
- if (imageBean.isSelected()) {
- radioButton.setChecked(false);
- imageBean.setSelected(false);
- mSelectedImages.remove(imageBean);
- updateBtnState();
- } else if (mSelectedImages.size() < 9) {
- radioButton.setChecked(true);
- imageBean.setSelected(true);
- mSelectedImages.add(imageBean);
- updateBtnState();
- } else {
- radioButton.setChecked(false);
- Toast.makeText(PhotoSelectActivity.this,
- "一次只能选择9张喔~",Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- };
- mImageListView.setAdapter(mImageAdapter);
- }
选择框状态改变需相应的改变“预览”和“发送”按钮的显示文字和可点击状态:
- private void updateBtnState() {
- String size = String.valueOf(mSelectedImages.size());
- String tvSend = "发送(" + size + "/9)";
- String tvPre = "预览(" + size + "/9)";
- mTvSend.setText(tvSend);
- mTvPreview.setText(tvPre);
- if (mSelectedImages.isEmpty()) {
- mTvSend.setClickable(false);
- mTvSend.setTextColor(getResources().getColor(R.color.blue_gray));
- mTvPreview.setClickable(false);
- mTvPreview.setTextColor(getResources().getColor(R.color.blue_gray));
- } else {
- mTvSend.setClickable(true);
- mTvSend.setTextColor(getResources().getColor(R.color.colorAccent));
- mTvPreview.setClickable(true);
- mTvPreview.setTextColor(getResources().getColor(R.color.colorAccent));
- }
- }
3.initPopupWindow主要用于初始化图片文件夹选择器
- private void initPopupWindow() {
- // 弹窗初始化,高度设置为屏幕的 3/4
- View rootView = LayoutInflater.from(this).inflate(R.layout.popup_window,
- mLayoutBottom, false);
- int popupHeight = (int) (this.getResources().getDisplayMetrics().heightPixels * 0.75f);
- mFolderWindow = new PopupWindow(rootView, LinearLayout.LayoutParams.MATCH_PARENT, popupHeight);
- mFolderWindow.setOutsideTouchable(true);
- mFolderWindow.setFocusable(true);
- mFolderWindow.setAnimationStyle(R.style.popup_window_anim);
- // 在PopupWindow隐藏后,将背景恢复正常亮度
- mFolderWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
- @Override
- public void onDismiss() {
- lightOn();
- }
- });
- // 文件夹列表初始化
- mFolderBeans = new ArrayList<>();
- RecyclerView folderListView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
- folderListView.setLayoutManager(new LinearLayoutManager(this));
- mFolderAdapter = new RecycleViewAdapter<FolderBean>(this, mFolderBeans) {
- @Override
- public int setItemLayoutId(int position) {
- return R.layout.item_folder;
- }
- @Override
- public void bindView(RViewHolder holder, int position) {
- String name = mFolderBeans.get(position).getName();
- int size = mFolderBeans.get(position).getImageList().size();
- boolean selected = mFolderBeans.get(position).isSelected();
- ImageBean image = mFolderBeans.get(position).getImageList().get(0);
- holder.setText(R.id.tv_folder_name, name);
- holder.setText(R.id.tv_image_count, String.valueOf(size) + " 张");
- holder.setVisible(R.id.iv_sel_status, selected);
- ImageView imageView = holder.getImageView(R.id.iv_cover);
- Glide.with(PhotoSelectActivity.this)
- .load(image.getPath()).diskCacheStrategy(DiskCacheStrategy.ALL)
- .error(R.drawable.bg_img_defalut)
- .into(imageView);
- }
- };
- mFolderAdapter.setItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(RViewHolder holder, int position) {
- // 如果选择了新的文件夹
- if (position != mSelFolderIndex) {
- // 将上个文件夹的选择状态去除
- mFolderBeans.get(mSelFolderIndex).setSelected(false);
- mFolderAdapter.notifyItemChanged(mSelFolderIndex);
- // 将选择的文件夹做更新
- mFolderBeans.get(position).setSelected(true);
- mFolderAdapter.notifyItemChanged(position);
- // 保存当前选择的文件夹
- mSelFolderIndex = position;
- mSelectedFolder = mFolderBeans.get(position);
- mTvFolderName.setText(mSelectedFolder.getName());
- // 关闭弹窗并更新图片列表
- updateImageList();
- }
- hidePopup();
- }
- });
- folderListView.setAdapter(mFolderAdapter);
- }
4.loadImageData主要用于加载手机照片数据(另一个线程,在上周写的ImageUtils中实现)
- private void loadImageData() {
- ImageUtils.loadImageList(this, new ImageUtils.OnLoadImageCallBack() {
- @Override
- public void callBack(List<FolderBean> folderList) {
- if (folderList.isEmpty()) {
- Toast.makeText(PhotoSelectActivity.this,
- "无照片~",Toast.LENGTH_SHORT).show();
- } else {
- mSelectedFolder = folderList.get(0);
- mSelFolderIndex = 0;
- folderList.get(0).setSelected(true);
- mFolderBeans.clear();
- mFolderBeans.addAll(folderList);
- // 加载数据在子线程完成的,需要使用mHandler去通知主线程跟新 UI
- mHandler.sendEmptyMessage(0x100);
- }
- }
- });
- }
mHandler通知主线程后,要处理Message
- @SuppressLint("HandlerLeak")
- private class MyHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- if (msg.what == 0x100) {
- mFolderAdapter.notifyDataSetChanged();
- updateImageList();
- // 有照片,可以点击
- mTvFolderName.setClickable(true);
- }
- }
- }
- /**
- * 根据选择的文件夹,更新图片列表
- */
- private void updateImageList() {
- mImageBeans.clear();
- mImageBeans.addAll(mSelectedFolder.getImageList());
- mImageAdapter.notifyDataSetChanged();
- }
5.OnClickListener的具体实现
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.tv_folder_name) {
- if (mFolderWindow.isShowing()) {
- hidePopup();
- } else {
- showPopup();
- }
- } else if (v.getId() == R.id.iv_back) {
- this.setResult(RESULT_CANCELED);
- this.finish();
- } else if (v.getId() == R.id.tv_send) {
- sendImage();
- } else if (v.getId() == R.id.tv_preview) {
- previewByBtn();
- } else if (v.getId() == R.id.btn_source_img) {
- if (mBtnSource.isChecked()) {
- mBtnSource.setChecked(false);
- isSourceImage = false;
- } else {
- mBtnSource.setChecked(true);
- isSourceImage = true;
- }
- }
- }
- /**
- * 显示和隐藏文件夹选择框 显示在底部布局之上
- */
- private void showPopup() {
- int[] location = new int[2];
- mLayoutBottom.getLocationOnScreen(location);
- mFolderWindow.showAtLocation(mLayoutBottom, Gravity.NO_GRAVITY, location[0],
- location[1] - mFolderWindow.getHeight());
- lightOff();
- }
- private void hidePopup() {
- mFolderWindow.dismiss();
- }
- /**
- * popupWindow 显示的时候 将图片列表区域变暗
- * popupWindow 隐藏的时候恢复
- */
- private void lightOn(){
- WindowManager.LayoutParams attributes = getWindow().getAttributes();
- attributes.alpha = 1.0f;
- getWindow().setAttributes(attributes);
- }
- private void lightOff(){
- WindowManager.LayoutParams attributes = getWindow().getAttributes();
- attributes.alpha = 0.3f;
- getWindow().setAttributes(attributes);
- }
6.图片显示RecycleView所用的ItemDecoration
- public class GridItemDecoration extends RecyclerView.ItemDecoration {
- private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
- private Drawable mDivider;
- public GridItemDecoration(Context context) {
- final TypedArray a = context.obtainStyledAttributes(ATTRS);
- mDivider = a.getDrawable(0);
- a.recycle();
- }
- @Override
- public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
- drawHorizontal(c, parent);
- drawVertical(c, parent);
- }
- private int getSpanCount(RecyclerView parent) {
- // 列数
- int spanCount = -1;
- RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
- if (layoutManager instanceof GridLayoutManager) {
- spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
- } else if (layoutManager instanceof StaggeredGridLayoutManager) {
- spanCount = ((StaggeredGridLayoutManager) layoutManager)
- .getSpanCount();
- }
- return spanCount;
- }
- private void drawHorizontal(Canvas c, RecyclerView parent) {
- int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
- .getLayoutParams();
- final int left = child.getLeft() - params.leftMargin;
- final int right = child.getRight() + params.rightMargin
- + mDivider.getIntrinsicWidth();
- final int top = child.getBottom() + params.bottomMargin;
- final int bottom = top + mDivider.getIntrinsicHeight();
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- }
- }
- private void drawVertical(Canvas c, RecyclerView parent) {
- final int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- final View child = parent.getChildAt(i);
- final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
- .getLayoutParams();
- final int top = child.getTop() - params.topMargin;
- final int bottom = child.getBottom() + params.bottomMargin;
- final int left = child.getRight() + params.rightMargin;
- final int right = left + mDivider.getIntrinsicWidth();
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- }
- }
- private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
- RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
- if (layoutManager instanceof GridLayoutManager) {
- // 如果是最后一列,则不需要绘制右边
- if ((pos + 1) % spanCount == 0) {
- return true;
- }
- } else if (layoutManager instanceof StaggeredGridLayoutManager) {
- int orientation = ((StaggeredGridLayoutManager) layoutManager)
- .getOrientation();
- if (orientation == StaggeredGridLayoutManager.VERTICAL) {
- // 如果是最后一列,则不需要绘制右边
- if ((pos + 1) % spanCount == 0) {
- return true;
- }
- } else {
- childCount = childCount - childCount % spanCount;
- if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
- return true;
- }
- }
- return false;
- }
- private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
- RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
- if (layoutManager instanceof GridLayoutManager) {
- childCount = childCount - childCount % spanCount;
- if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
- return true;
- } else if (layoutManager instanceof StaggeredGridLayoutManager) {
- int orientation = ((StaggeredGridLayoutManager) layoutManager)
- .getOrientation();
- // StaggeredGridLayoutManager 且纵向滚动
- if (orientation == StaggeredGridLayoutManager.VERTICAL) {
- childCount = childCount - childCount % spanCount;
- // 如果是最后一行,则不需要绘制底部
- if (pos >= childCount)
- return true;
- } else { // StaggeredGridLayoutManager 且横向滚动
- // 如果是最后一行,则不需要绘制底部
- if ((pos + 1) % spanCount == 0) {
- return true;
- }
- }
- }
- return false;
- }
- @Override
- public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
- int spanCount = getSpanCount(parent);
- int childCount = parent.getAdapter().getItemCount();
- if (isLastRaw(parent, itemPosition, spanCount, childCount)){ // 如果是最后一行,则不需要绘制底部
- outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
- } else if (isLastColumn(parent, itemPosition, spanCount, childCount)){// 如果是最后一列,则不需要绘制右边
- outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
- } else {
- outRect.set(0, 0, mDivider.getIntrinsicWidth(),
- mDivider.getIntrinsicHeight());
- }
- }
- }
仝心:
这几天主要在实现录音功能。
首先自定义了一个音频记录管理器AudioRecordManager用于录音,定义prepareAudio方法,用于设置输出路径,音频源、麦克风,输出格式和音频编码。
- public void prepareAudio(){
- try {
- hasPrepare = false;
- File dir = new File(mAudioDir);
- if (!dir.exists()){
- dir.mkdirs();
- }
- mMediaRecorder = new MediaRecorder();
- String fileName = UUID.randomUUID().toString() + ".arm";
- File file = new File(dir,fileName);
- mCurrentFilePath = file.getAbsolutePath();
- // 设置输出路径
- mMediaRecorder.setOutputFile(mCurrentFilePath);
- // 设置音频源,麦克风
- mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- // 设置输出格式
- mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
- // 设置音频编码
- mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- // 准备
- mMediaRecorder.prepare();
- mMediaRecorder.start();
- hasPrepare = true;
- if (mStateListener != null){
- mStateListener.prepareFinish(mCurrentFilePath);
- }
- } catch (IOException e) {
- if (mStateListener != null){
- mStateListener.prepareError(e.getMessage());
- }
- e.printStackTrace();
- }
- }
接下来获取音量等级,等级上限作为参数传入
- public int getVoiceLevel(int maxLevel){
- try {
- if (hasPrepare){
- // getMaxAmplitude = 0 - 32767
- return maxLevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1;
- }
- }catch (Exception e){
- return 1;
- }
- return 1;
- }
- public void releaseAudio(){
- mMediaRecorder.stop();
- mMediaRecorder.release();
- mMediaRecorder = null;
- hasPrepare = false;
- }
- public void cancelAudio(){
- releaseAudio();
- if (mCurrentFilePath != null){
- File file = new File(mCurrentFilePath);
- file.delete();
- }
- }
接下来定义音频对话管理器AudioDialogManager类用于在和好友的对话中发送录音。
- private Context mContext;
- private Dialog mDialog;
- private LayoutInflater mInflater;
- private ImageView mIvRecord;
- private ImageView mIvVoiceLevel;
- private TextView mTvTip;
- public RecordDialogManager(Context context){
- mContext = context;
- mInflater = LayoutInflater.from(mContext);
- }
显示之前的消息记录中的录音的方法:
- public void showDialogRecord(){
- View view = mInflater.inflate(R.layout.dialog_audio_record_button,null);
- mDialog = new Dialog(mContext, R.style.Theme_Audio_Record_Button);
- mDialog.setContentView(view);
- mIvRecord = (ImageView) mDialog.findViewById(R.id.iv_record);
- mIvVoiceLevel = (ImageView) mDialog.findViewById(R.id.iv_voice_level);
- mTvTip = (TextView) mDialog.findViewById(R.id.tv_dialog_tip);
- mDialog.show();
- }
在聊天中发送录音的操作指示,包括取消发送、提示录音时间过短、取消发送
- public void showRecording(){
- if (mDialog != null && mDialog.isShowing()){
- mIvRecord.setImageResource(R.drawable.recorder);
- mIvVoiceLevel.setVisibility(View.VISIBLE);
- mTvTip.setText(mContext.getString(R.string.move_up_cancel));
- }
- }
- public void showDialogToShort(){
- if (mDialog != null && mDialog.isShowing()){
- mIvRecord.setImageResource(R.drawable.voice_to_short);
- mIvVoiceLevel.setVisibility(View.GONE);
- mTvTip.setText(mContext.getString(R.string.record_to_short));
- }
- }
- public void showDialogWantCancel(){
- if (mDialog != null && mDialog.isShowing()){
- mIvRecord.setImageResource(R.drawable.cancel);
- mIvVoiceLevel.setVisibility(View.GONE);
- mTvTip.setText(mContext.getString(R.string.release_cancel));
- }
- }
- public void updateVoiceLevel(int level){
- if (mDialog != null && mDialog.isShowing()){
- int resId = mContext.getResources().getIdentifier("v"+level,
- "drawable",mContext.getPackageName());
- mIvVoiceLevel.setImageResource(resId);
- }
- }
张静:
本周我完成了ContractFragment中点击“添加朋友”和“验证提醒”后分别会跳转进入的界面以及相关功能实现
一. SearchUserActivity(根据账户搜索用户)—— 点击“添加朋友”后跳转入的
1. activity_search_user.xml
- <?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"
- android:fitsSystemWindows="true">
- <include layout="@layout/title_layout"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp">
- <EditText
- android:id="@+id/et_user_account"
- android:paddingLeft="5dp"
- android:paddingRight="5dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="45dp"
- android:background="@drawable/et_back_ground"
- android:textColorHint="@color/hint_color"
- android:hint="@string/please_input_account"/>
- <ImageView
- android:id="@+id/iv_search"
- android:layout_marginTop="5dp"
- android:background="@color/app_blue_color"
- android:layout_width="40dp"
- android:layout_height="40dp"
- android:src="@mipmap/search"/>
- </LinearLayout>
- </LinearLayout>
2. 初始化回调
有三种情况:
(1)成功:
a.若找不到该账户,显示消息“查无此人哦,请检查后再试~”
b.若成功找到,跳转到FriendInfoActivity,上周博客写过该Activity,同时设置flag为FLAG_ADD_FRIEND,即显示的该账户只能
选择添加到通讯录,没有发送消息和视频聊天选项
(2)失败:显示消息“搜索失败,返回码:xxx”
(3)出错:显示消息“搜索出错”
- private void initCallBack(){
- mRequestCallback = new RequestCallback<List<NimUserInfo>>() {
- @Override
- public void onSuccess(List<NimUserInfo> param) {
- if (param.isEmpty()){
- ToastUtils.showMessage(SearchUserActivity.this,"查无此人喔,请检查后再试~~");
- return;
- }
- Intent intent = new Intent(SearchUserActivity.this,FriendInfoActivity.class);
- intent.putExtra("FLAG",FriendInfoActivity.FLAG_ADD_FRIEND);
- intent.putExtra("NimUserInfo",param.get(0));
- startActivity(intent);
- }
- @Override
- public void onFailed(int code) {
- ToastUtils.showMessage(SearchUserActivity.this,"搜索失败,返回码:" + code);
- }
- @Override
- public void onException(Throwable exception) {
- ToastUtils.showMessage(SearchUserActivity.this,"搜索出错:" + exception.getMessage());
- }
- };
- }
ToastUtils.java
- package com.ezreal.ezchat.commonlibrary.utils;
- import android.content.Context;
- import android.os.Handler;
- import android.os.Looper;
- import android.widget.Toast;
- /**
- * Created by 张静
- */
- public class ToastUtils {
- private static Handler handler = new Handler(Looper.getMainLooper());
- public static void showMessage(final Context context, final String msg) {
- showMessage(context, msg, Toast.LENGTH_SHORT);
- }
- private static void showMessage(final Context act, final String msg,
- final int len) {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(act,msg,len).show();
- }
- });
- }
- }
3. 查询朋友
若检测到的所查询的账号为空或者是自己的账号,都会提示,无法成功查询
通过NimClient的getService接口获取到UserService(用户资料操作相关接口)服务实例,调用fetchUserInfo方法从服务器获取用户资料
- @OnClick(R.id.iv_search)
- public void searchFriend(){
- String account = mEtUserAccount.getText().toString().trim();
- if (TextUtils.isEmpty(account)){
- ToastUtils.showMessage(SearchUserActivity.this,"账号不能为空");
- return;
- }
- if (account.equals(NimUserHandler.getInstance().getMyAccount())){
- ToastUtils.showMessage(SearchUserActivity.this,"不能添加自己为好友");
- return;
- }
- List<String> accounts = new ArrayList<>(1);
- accounts.add(account);
- NIMClient.getService(UserService.class).fetchUserInfo(accounts).setCallback(mRequestCallback);
- }
4. 返回
- @OnClick(R.id.iv_back_btn)
- public void backOnClick(){
- finish();
- }
附上完整 SearchUserActivity.java
- package com.ezreal.ezchat.activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.text.TextUtils;
- import android.widget.EditText;
- import com.ezreal.ezchat.R;
- import com.ezreal.ezchat.handler.NimUserHandler;
- import com.netease.nimlib.sdk.NIMClient;
- import com.netease.nimlib.sdk.RequestCallback;
- import com.netease.nimlib.sdk.uinfo.UserService;
- import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
- import com.ezreal.ezchat.commonlibrary.utils.ToastUtils;
- import java.util.ArrayList;
- import java.util.List;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- /**
- * 根据账户搜索用户
- * Created by 张静
- */
- public class SearchUserActivity extends BaseActivity {
- @BindView(R.id.et_user_account)
- EditText mEtUserAccount;
- private static final String TAG = SearchUserActivity.class.getSimpleName();
- private RequestCallback<List<NimUserInfo>> mRequestCallback;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setStatusBarColor(R.color.app_blue_color);
- setContentView(R.layout.activity_search_user);
- setTitleBar("添加朋友",true,false);
- ButterKnife.bind(this);
- initCallBack();
- }
- private void initCallBack(){
- mRequestCallback = new RequestCallback<List<NimUserInfo>>() {
- @Override
- public void onSuccess(List<NimUserInfo> param) {
- if (param.isEmpty()){
- ToastUtils.showMessage(SearchUserActivity.this,"查无此人喔,请检查后再试~~");
- return;
- }
- Intent intent = new Intent(SearchUserActivity.this,FriendInfoActivity.class);
- intent.putExtra("FLAG",FriendInfoActivity.FLAG_ADD_FRIEND);
- intent.putExtra("NimUserInfo",param.get(0));
- startActivity(intent);
- }
- @Override
- public void onFailed(int code) {
- ToastUtils.showMessage(SearchUserActivity.this,"搜索失败,返回码:" + code);
- }
- @Override
- public void onException(Throwable exception) {
- ToastUtils.showMessage(SearchUserActivity.this,"搜索出错:" + exception.getMessage());
- }
- };
- }
- @OnClick(R.id.iv_search)
- public void searchFriend(){
- String account = mEtUserAccount.getText().toString().trim();
- if (TextUtils.isEmpty(account)){
- ToastUtils.showMessage(SearchUserActivity.this,"账号不能为空");
- return;
- }
- if (account.equals(NimUserHandler.getInstance().getMyAccount())){
- ToastUtils.showMessage(SearchUserActivity.this,"不能添加自己为好友");
- return;
- }
- List<String> accounts = new ArrayList<>(1);
- accounts.add(account);
- NIMClient.getService(UserService.class).fetchUserInfo(accounts).setCallback(mRequestCallback);
- }
- @OnClick(R.id.iv_back_btn)
- public void backOnClick(){
- finish();
- }
- }