实训第四周(2)

李晨晨:

本次主要实现图片和文件夹的属性描述和设置类,以及读取SD卡图片列表工具类。

1.重点是读取SD卡图片列表并保存到相应的list

[java] view plain copy
  1. public class ImageUtils {  
  2.   
  3.     public static void loadImageList(final Context context, final OnLoadImageCallBack callBack) {  
  4.         new Thread(new Runnable() {  
  5.             @Override  
  6.             public void run() {  
  7.                 ContentResolver resolver = context.getContentResolver();  
  8.                 Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  
  9.                         new String[]{MediaStore.Images.ImageColumns._ID,  
  10.                                 MediaStore.Images.ImageColumns.DATA,  
  11.                                 MediaStore.Images.ImageColumns.DISPLAY_NAME,  
  12.                                 MediaStore.Images.ImageColumns.SIZE,  
  13.                                 MediaStore.Images.ImageColumns.DATE_ADDED},  
  14.                         nullnull, MediaStore.Images.ImageColumns.DATE_ADDED + " desc");  
  15.                 List<ImageBean> imageBeans = new ArrayList<>();  
  16.                 if (cursor != null) {  
  17.                     ImageBean bean;  
  18.                     while (cursor.moveToNext()) {  
  19.                         long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));  
  20.                         String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));  
  21.                         String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));  
  22.                         long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE));  
  23.                         long date = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_ADDED));  
  24.                         bean = new ImageBean(id, name, path, size, date);  
  25.                         imageBeans.add(bean);  
  26.                     }  
  27.                     cursor.close();  
  28.                 }  
  29.                 callBack.callBack(splitImage2Folder(imageBeans));  
  30.             }  
  31.         }).start();  
  32.     }  
  33.   
  34.     private static List<FolderBean> splitImage2Folder(List<ImageBean> imageList) {  
  35.         List<FolderBean> folderList = new ArrayList<>();  
  36.         if (!imageList.isEmpty()) {  
  37.   
  38.             // 生成全部图片文件夹  
  39.             FolderBean allFolder = new FolderBean("全部图片");  
  40.             for (ImageBean bean : imageList){  
  41.                 if (bean.getSize() > 50 * 1024){  
  42.                     allFolder.addImage(bean);  
  43.                 }  
  44.             }  
  45.             folderList.add(allFolder);  
  46.   
  47.             // 根据图片路径划分文件夹  
  48.             for (int i = 0; i < imageList.size(); i++) {  
  49.                 String path = imageList.get(i).getPath();  
  50.                 String name = getFolderName(path);  
  51.                 if (!TextUtils.isEmpty(name)) {  
  52.                     FolderBean folder = getFolder(name, folderList);  
  53.                     folder.addImage(imageList.get(i));  
  54.                 }  
  55.             }  
  56.         }  
  57.   
  58.         return folderList;  
  59.     }  
  60.   
  61.     private static String getFolderName(String path) {  
  62.         if (!TextUtils.isEmpty(path)) {  
  63.             String[] strings = path.split(File.separator);  
  64.             if (strings.length >= 2) {  
  65.                 return strings[strings.length - 2];  
  66.             }  
  67.         }  
  68.         return "";  
  69.     }  
  70.   
  71.     private static FolderBean getFolder(String name, List<FolderBean> folders) {  
  72.         for (int i = 0; i < folders.size(); i++) {  
  73.             FolderBean folder = folders.get(i);  
  74.             if (name.equals(folder.getName())) {  
  75.                 return folder;  
  76.             }  
  77.         }  
  78.         FolderBean newFolder = new FolderBean(name);  
  79.         folders.add(newFolder);  
  80.         return newFolder;  
  81.     }  
  82.   
  83.     public interface OnLoadImageCallBack {  
  84.         void callBack(List<FolderBean> folderList);  
  85.     }  
  86.   
  87. }  
loadImageList主要用于新建一个线程,该线程在后台读取所有的图片并记录下其基本信息放到ImageBean中

将所有的ImageBean都加到一个列表中,并用splitImage2Folder方法将所有图片划分到不同文件夹。所有过程完成后该线程产生一个callBack。

2.该过程中用到的ImageBean和FolderBean

[java] view plain copy
  1. public class ImageBean implements Serializable{  
  2.   
  3.     private long id;  
  4.     private String name;  
  5.     private String path;  
  6.     private long size;  
  7.     private long date;  
  8.     private boolean isSelected = false;  
  9.   
  10.     public ImageBean() {  
  11.     }  
  12.   
  13.     public ImageBean(long id, String name, String path, long size, long date) {  
  14.         this.id = id;  
  15.         this.name = name;  
  16.         this.path = path;  
  17.         this.size = size;  
  18.         this.date = date;  
  19.     }  
  20.   
  21.     public long getId() {  
  22.         return id;  
  23.     }  
  24.   
  25.     public void setId(long id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getName() {  
  30.         return name;  
  31.     }  
  32.   
  33.     public void setName(String name) {  
  34.         this.name = name;  
  35.     }  
  36.   
  37.     public String getPath() {  
  38.         return path;  
  39.     }  
  40.   
  41.     public void setPath(String path) {  
  42.         this.path = path;  
  43.     }  
  44.   
  45.     public long getSize() {  
  46.         return size;  
  47.     }  
  48.   
  49.     public void setSize(long size) {  
  50.         this.size = size;  
  51.     }  
  52.   
  53.     public long getDate() {  
  54.         return date;  
  55.     }  
  56.   
  57.     public void setDate(long date) {  
  58.         this.date = date;  
  59.     }  
  60.   
  61.     public boolean isSelected() {  
  62.         return isSelected;  
  63.     }  
  64.   
  65.     public void setSelected(boolean selected) {  
  66.         isSelected = selected;  
  67.     }  
  68. }  


[java] view plain copy
  1. public class FolderBean {  
  2.   
  3.     private String name;  
  4.     private List<ImageBean> mImageList;  
  5.     private boolean isSelected = false;  
  6.   
  7.     public FolderBean(String name) {  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     public FolderBean(String name, List<ImageBean> imageList) {  
  12.         this.name = name;  
  13.         mImageList = imageList;  
  14.     }  
  15.   
  16.     public void addImage(ImageBean imageBean){  
  17.         if (mImageList == null){  
  18.             mImageList = new ArrayList<>();  
  19.         }  
  20.         mImageList.add(imageBean);  
  21.     }  
  22.   
  23.   
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.   
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32.     public List<ImageBean> getImageList() {  
  33.         return mImageList;  
  34.     }  
  35.   
  36.     public void setImageList(List<ImageBean> imageList) {  
  37.         mImageList = imageList;  
  38.     }  
  39.   
  40.     public boolean isSelected() {  
  41.         return isSelected;  
  42.     }  
  43.   
  44.     public void setSelected(boolean selected) {  
  45.         isSelected = selected;  
  46.     }  
  47. }  



仝心:

 在前几次工作的基础上对已完成的工作部分进行了一定的整合,将个性化字体键盘文本输入功能、语音识别功能和原本的系统键盘功能组合到了一起,实现了文本输入布局的基本功能。

各种组件监听功能的初始化,包括文本框触摸、输入监听;表情输入监听。未来还将添加录音按钮初始化和录音监听。

[java] view plain copy
  1. private void initListener() {  
  2.         // 文本输入框触摸监听  
  3.         mEtInput.setOnClickListener(new MyOnClickListener());  
  4.         // 文本输入框输入监听  
  5.         mEtInput.addTextChangedListener(new TextWatcher() {  
  6.             @Override  
  7.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  8.   
  9.             }  
  10.   
  11.             @Override  
  12.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  13.                 if (s.toString().length() > 0) {  
  14.                     mIvMore.setVisibility(View.GONE);  
  15.                     mBtnSend.setVisibility(View.VISIBLE);  
  16.                 } else {  
  17.                     mIvMore.setVisibility(View.VISIBLE);  
  18.                     mBtnSend.setVisibility(View.GONE);  
  19.                 }  
  20.             }  
  21.   
  22.             @Override  
  23.             public void afterTextChanged(Editable s) {  
  24.   
  25.             }  
  26.         });  
  27.         //表情输入监听  
  28.         mExpressLayout.setOnExpressSelListener(new ExpressLayout.OnExpressSelListener() {  
  29.             @Override  
  30.             public void onEmojiSelect(EmojiBean emojiBean) {  
  31.                 // 如果点击了表情,则添加到输入框中  
  32.   
  33.                 int curPosition = mEtInput.getSelectionStart();  
  34.                 String tag=mChangeModeLayout.getTag().toString();  
  35.                 switch(tag){  
  36.                     case "1":  
  37.                         // 获取当前光标位置,在指定位置上添加表情图片文本  
  38.                         StringBuilder sb = new StringBuilder(mEtInput.getText().toString());  
  39.                         sb.insert(curPosition, emojiBean.getEmojiName());  
  40.                         // 特殊文字处理,将表情等转换一下  
  41.                         SpannableString spannableString = EmojiUtils.text2Emoji(getContext(),  
  42.                                 sb.toString(), mEtInput.getTextSize());  
  43.                         mEtInput.setText(spannableString);  
  44.                         // 将光标设置到新增完表情的右侧  
  45.                         mEtInput.setSelection(curPosition + emojiBean.getEmojiName().length());  
  46.                         break;  
  47.                     case "2":  
  48.                         //Drawable drawable = getResources().getDrawable(emojiBean.getResIndex());  
  49.   
  50.                         Bitmap bmp = BitmapFactory.decodeResource(getResources(),emojiBean.getResIndex());  
  51.                         SpannableString ss=getBitmapMime(bmp);  
  52.   
  53.                         insertPhotoToEditText(ss);  
  54.                         count++;  
  55.                         inputBmpList.add(curPosition,bmp);  
  56.                         break;  
  57.                     default:  
  58.                         break;  
  59.                 }  
  60.   
  61.             }  
  62.   
  63.             @Override  
  64.             public void onEmojiDelete() {  
  65.                 // 调用系统的删除操作  
  66.                 mEtInput.dispatchKeyEvent(new KeyEvent(  
  67.                         KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));  
  68.             }  
  69.         });  
  70.   
  71.         mEtInput.setFocusable(true);  
  72.         mEtInput.requestFocus();//否则第一次点文本框时会弹出系统键盘  
  73.         hideSoftInput(1);  
  74.     }  

表情包按键的相关事件处理,以下的case1表示键盘输入模式,case2表示语音识别模式,case3表示录音模式(暂时还没有实现,将在接下来的工作中完成)。

[java] view plain copy
  1. @OnClick(R.id.iv_expression)  
  2.     public void clickExpressBtn(){  
  3.         String tag=mIvInputType.getTag().toString();  
  4.         switch(tag){  
  5.             case "1":  
  6.                 if(mExpressLayout.isShown())  
  7.                 {  
  8.                     lockContentHeight();  
  9.                     mExpressLayout.setVisibility(GONE);  
  10.                     mIvExpress.setImageResource(R.mipmap.expression);  
  11.                     showSoftInput();  
  12.                     unLockContentHeight();  
  13.                 }else{  
  14.                     if(isSoftInputShow()){  
  15.                         lockContentHeight();  
  16.                         hideSoftInput(3);  
  17.                     }  
  18.                     mExtensionLayout.setVisibility(GONE);  
  19.                     mExpressLayout.setVisibility(VISIBLE);  
  20.                     mIvExpress.setImageResource(R.mipmap.keyboard);  
  21.                     unLockContentHeight();  
  22.                 }  
  23.                 break;  
  24.             case "2":  
  25.                 if(mExpressLayout.isShown()){  
  26.                     mExpressLayout.setVisibility(GONE);  
  27.                     mIvExpress.setImageResource(R.mipmap.expression);  
  28.                     mSoundRecogLayout.setVisibility(VISIBLE);  
  29.                 }else{  
  30.                     mExtensionLayout.setVisibility(GONE);  
  31.                     mSoundRecogLayout.setVisibility(GONE);  
  32.                     mExpressLayout.setVisibility(VISIBLE);  
  33.                     mIvExpress.setImageResource(R.mipmap.keyboard);  
  34.                 }  
  35.                 break;  
  36.             case "3":  
  37.   
  38.                 break;  
  39.             default:  
  40.                 break;  
  41.         }  
  42.     }  

最后是针对发送键按钮的监听,这里分为了发送系统文本和自定义文本的两种情况,发送自定义文本是要先对打字过程中存储的所有图片进行拼接,包括字母间的左右拼接和行间的上下拼接,其中inputList中存储打字时产生的图片,currentList中存储当前拼接的图片。拼接完成后将拼接后的图片以发送图片的形式发送出去。

[java] view plain copy
  1. //发送键,发送键  
  2.     @OnClick(R.id.tv_btn_send)  
  3.     public void sendTextMessage() {  
  4.         String tag=mChangeModeLayout.getTag().toString();  
  5.         switch (tag){  
  6.             case "1":  
  7.                 //正常文本模式,系统键盘  
  8.                 String text = mEtInput.getText().toString();  
  9.                 mEtInput.getText().clear();  
  10.                 //语音识别相关清空  
  11.                 mIatResults.clear();  
  12.                 result_string="";  
  13.                 new_piece="";  
  14.                 if (mLayoutListener != null) {  
  15.                     mLayoutListener.sendBtnClick(text);  
  16.                 }  
  17.                 break;  
  18.             case "2":  
  19.                 //个性化文本模式,自定义键盘  
  20.                 Bitmap bitmap;//文本框所有图片合成的bitmap  
  21.                 if(inputBmpList.size() == 0)  
  22.                     break;  
  23.                 else if(inputBmpList.size() == 1){  
  24.                     bitmap = newBitmap(inputBmpList.get(0));  
  25.                 }else {  
  26.                     bitmap = newBitmap(inputBmpList.get(0));  
  27.                     for (int i = 0; i < inputBmpList.size() - 1; i++) {  
  28.                         if(bitmap.getWidth()<800)  
  29.                         {  
  30.                             bitmap = newBitmap(bitmap, inputBmpList.get(i + 1));  
  31.                         }  
  32.                         else{  
  33.                             currentBmpList.add(bitmap);  
  34.                             bitmap = null;  
  35.                             bitmap = newBitmap(inputBmpList.get(i + 1));  
  36.                         }  
  37.                     }  
  38.                     currentBmpList.add(bitmap);  
  39.   
  40.                 }  
  41.                 int longest;  
  42.                 if(currentBmpList.size()>0){  
  43.                     bitmap = currentBmpList.get(0);  
  44.                 }  
  45.                 for(int i = 0;i< currentBmpList.size() - 1;i++){  
  46.                     if(bitmap.getWidth()>currentBmpList.get(i+1).getWidth())  
  47.                         longest = bitmap.getWidth();  
  48.                     else  
  49.                         longest = currentBmpList.get(i+1).getWidth();  
  50.                     bitmap = newBitmap(bitmap,currentBmpList.get(i+1),longest);  
  51.                 }  
  52.                 bitmap = scaleBitmap(bitmap,0.6f);  
  53.                 currentBmpList.clear();  
  54.                 //将bitmap的背景色改为蓝色  
  55.                 bitmap=changeBackground(bitmap);  
  56.   
  57.   
  58.                 //将合成的图保存到指定路径,用于图片发送  
  59.                 Bitmap bb;  
  60.                 bb=bitmap.copy(Bitmap.Config.ARGB_8888,true);  
  61.                 String pathName=Constant.APP_CACHE_IMAGE+System.currentTimeMillis()+".jpg";  
  62.                 File dirFile=new File(pathName);  
  63.                 if(!dirFile.exists()){  
  64.                     dirFile.getParentFile().mkdir();  
  65.                     try{  
  66.                         dirFile.createNewFile();  
  67.                     }catch(IOException e){  
  68.                         e.printStackTrace();  
  69.                     }  
  70.                 }  
  71.   
  72.                 FileOutputStream out=null;  
  73.                 try{  
  74.                     out = new FileOutputStream(dirFile);  
  75.                     bb.compress(Bitmap.CompressFormat.JPEG,90,out);  
  76.                     System.out.println("-------------保存到sd指定目录文件下--------------");  
  77.   
  78.                 }catch(FileNotFoundException e){  
  79.                     e.printStackTrace();  
  80.                 }  
  81.                 try{  
  82.                     out.flush();  
  83.                     out.close();  
  84.                 }catch (IOException e){  
  85.                     e.printStackTrace();  
  86.                 }  
  87.   
  88.                 mEtInput.getText().clear();  
  89.                 if (mLayoutListener != null) {  
  90.                     mLayoutListener.photoTextBtnClick(pathName);  
  91.                 }  
  92.                 count = 0;  
  93.                 inputBmpList.clear();  
  94.                 break;  
  95.             default:  
  96.                 break;  
  97.         }  
  98.     }  
图片拼接的方法
[java] view plain copy
  1. //左右拼接  
  2.     private Bitmap newBitmap(Bitmap bit1,Bitmap bit2){  
  3.   
  4.         int width = bit1.getWidth()+bit2.getWidth();  
  5.         int height = bit1.getHeight();  
  6.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
  7.         Canvas canvas = new Canvas(bitmap);  
  8.         canvas.drawBitmap(bit1, 00null);  
  9.         canvas.drawBitmap(bit2, bit1.getWidth(), 0null);  
  10.         return bitmap;  
  11.     }  
  12.     //上下拼接  
  13.     private Bitmap newBitmap(Bitmap bit1,Bitmap bit2,int length){  
  14.   
  15.         int width = length;  
  16.         int height = bit1.getHeight()+bit2.getHeight();  
  17.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
  18.         Canvas canvas = new Canvas(bitmap);  
  19.         canvas.drawBitmap(bit1, 00null);  
  20.         canvas.drawBitmap(bit2, 0, bit1.getHeight(), null);  
  21.         return bitmap;  
  22.     }  



张静:

本周接下来完成了如下工作:

1. RequestFriendActivity(接上篇博客,当点击“添加到通讯录”时,跳转至此)

单击“发送请求”时,选择好友请求方式为VERIFY_REQUEST(发起好友验证请求)

通过NimClient的getService接口获取到FriendService(好友管理/好友关系/黑名单关系/消息提醒相关操作)服务实例,调用addFriend方法进行好友请求,附上信息msg

[java] view plain copy
  1. package com.ezreal.ezchat.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.annotation.Nullable;  
  5. import android.widget.EditText;  
  6.   
  7. import com.ezreal.ezchat.R;  
  8. import com.netease.nimlib.sdk.InvocationFuture;  
  9. import com.netease.nimlib.sdk.NIMClient;  
  10. import com.netease.nimlib.sdk.RequestCallback;  
  11. import com.netease.nimlib.sdk.RequestCallbackWrapper;  
  12. import com.netease.nimlib.sdk.friend.FriendService;  
  13. import com.netease.nimlib.sdk.friend.constant.VerifyType;  
  14. import com.netease.nimlib.sdk.friend.model.AddFriendData;  
  15. import com.ezreal.ezchat.commonlibrary.utils.ToastUtils;  
  16.   
  17. import butterknife.BindView;  
  18. import butterknife.ButterKnife;  
  19. import butterknife.OnClick;  
  20.   
  21. /** 
  22.  * Created by 张静 
  23.  */  
  24.   
  25. public class RequestFriendActivity extends BaseActivity {  
  26.   
  27.     @BindView(R.id.et_request_msg)  
  28.     EditText mEtRequestMsg;  
  29.     private RequestCallback<Void> mRequestCallback;  
  30.     @Override  
  31.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setStatusBarColor(R.color.app_blue_color);  
  34.         setContentView(R.layout.activity_request_friend);  
  35.         setTitleBar("添加好友",true,false);  
  36.         ButterKnife.bind(this);  
  37.         initCallBack();  
  38.     }  
  39.   
  40.     private void initCallBack(){  
  41.         mRequestCallback = new RequestCallbackWrapper<Void>() {  
  42.             @Override  
  43.             public void onResult(int code, Void result, Throwable exception) {  
  44.                 if (exception != null){  
  45.                     ToastUtils.showMessage(RequestFriendActivity.this,"请求出错,请重试:" + exception.getMessage());  
  46.                 }else {  
  47.                     if (code == 200){  
  48.                         ToastUtils.showMessage(RequestFriendActivity.this,"请求已发出~");  
  49.                         finish();  
  50.                     }else {  
  51.                         ToastUtils.showMessage(RequestFriendActivity.this,"请求异常,请重试,异常代码:" + code);  
  52.                     }  
  53.                 }  
  54.             }  
  55.         };  
  56.     }  
  57.   
  58.     @OnClick(R.id.tv_send_request)  
  59.     public void sendRequest(){  
  60.         String account = getIntent().getStringExtra("account");  
  61.         String msg = "";  
  62.         if (mEtRequestMsg.getText() != null){  
  63.             msg = mEtRequestMsg.getText().toString().trim();  
  64.         }  
  65.         VerifyType type = VerifyType.VERIFY_REQUEST;  
  66.         InvocationFuture<Void> addFriend = NIMClient.getService(FriendService.class)  
  67.                 .addFriend(new AddFriendData(account, type, msg));  
  68.         addFriend.setCallback(mRequestCallback);  
  69.     }  
  70.   
  71.   
  72. }  


2. activity_request_friend.xml

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true">  
  8.   
  9.     <include layout="@layout/title_layout"/>  
  10.   
  11.     <EditText  
  12.         android:id="@+id/et_request_msg"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:minHeight="50dp"  
  16.         android:hint="@string/request_friend_hint"  
  17.         android:textColorHint="@color/hint_color"  
  18.         android:background="@drawable/et_back_ground"/>  
  19.   
  20.     <TextView  
  21.         android:id="@+id/tv_send_request"  
  22.         android:layout_marginTop="10dp"  
  23.         android:gravity="center"  
  24.         android:textSize="18sp"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="40dp"  
  27.         android:background="@color/app_blue_color"  
  28.         android:textColor="@color/white_color"  
  29.         android:text="@string/send_request"/>  
  30. </LinearLayout>  
    


3. 继续接上篇中ContractFragment的内容,加载好友列表(loadFriendList)

通过NimFriendHandler获得好友信息,notifyDataSetChanged使如果适配器内容改变时需强制调用getView来刷新每个Item内容

[java] view plain copy
  1. private void loadFriendList() {  
  2.         mFriendList.clear();  
  3.         mFriendList.addAll(NimFriendHandler.getInstance().getFriendInfos());  
  4.         mViewAdapter.notifyDataSetChanged();  
  5.     }  


4. 点击“添加朋友”,跳转至SearchUserActivity(根据账户搜索用户)

[java] view plain copy
  1. @OnClick(R.id.layout_add_friend)  
  2.     public void addFriend() {  
  3.         startActivity(new Intent(getContext(), SearchUserActivity.class));  
  4.     }  

5. 点击“验证提醒”,跳转至CheckNotifyListActivity

[java] view plain copy
  1. @OnClick(R.id.layout_msg_notify)  
  2.     public void openMsgNotifyActivity() {  
  3.         //hindUnReadMsgView();  
  4.         startActivityForResult(new Intent(getContext(),  
  5.                 CheckNotifyListActivity.class), REQUEST_CHECK_NOTI);  
  6.     }  


6. 得到新打开的Activity关闭后返回的数据

[java] view plain copy
  1. @Override  
  2.     public void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.         super.onActivityResult(requestCode, resultCode, data);  
  4.         if (requestCode == REQUEST_CHECK_NOTI) {  
  5.             if (resultCode == CheckNotifyListActivity.RESULT_HAVE_CHANGE) {  
  6.                 loadFriendList();  
  7.             }  
  8.         }  
  9.     }  



附上完整ContractFragment.java

[java] view plain copy
  1. package com.ezreal.ezchat.fragment;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.widget.LinearLayoutManager;  
  5. import android.support.v7.widget.RecyclerView;  
  6. import android.view.View;  
  7. import android.widget.Toast;  
  8.   
  9.   
  10. //import com.ezreal.chat.MainActivity;  
  11. import com.ezreal.ezchat.R;  
  12. import com.ezreal.ezchat.activity.FriendInfoActivity;  
  13. import com.ezreal.ezchat.activity.CheckNotifyListActivity;  
  14.   
  15. import com.ezreal.ezchat.activity.SearchUserActivity;  
  16. import com.ezreal.ezchat.handler.NimFriendHandler;  
  17. import com.ezreal.ezchat.handler.NimFriendHandler.OnFriendUpdateListener;  
  18. import com.ezreal.ezchat.handler.NimSysMsgHandler;  
  19. import com.ezreal.ezchat.handler.NimSysMsgHandler.SystemMessageListener;  
  20. import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;  
  21. import com.ezreal.ezchat.commonlibrary.adapter.OnItemClickListener;  
  22. import com.ezreal.ezchat.commonlibrary.adapter.RViewHolder;  
  23. import com.ezreal.ezchat.commonlibrary.adapter.RecycleViewAdapter;  
  24. import com.ezreal.ezchat.commonlibrary.utils.TextUtils;  
  25.   
  26. import java.util.ArrayList;  
  27. import java.util.List;  
  28.   
  29. import butterknife.BindView;  
  30. import butterknife.ButterKnife;  
  31. import butterknife.OnClick;  
  32.   
  33. /** 
  34.  * Created by 张静 
  35.  */  
  36.   
  37. public class ContractFragment extends BaseFragment {  
  38.   
  39.     public static final int REQUEST_CHECK_NOTI = 0x5000;  
  40.   
  41.     @BindView(R.id.rcv_friend)  
  42.     RecyclerView mRecyclerView;  
  43.     private List<NimUserInfo> mFriendList;  
  44.     private RecycleViewAdapter<NimUserInfo> mViewAdapter;  
  45.   
  46.     @Override  
  47.     public int setLayoutID() {  
  48.         return R.layout.fragment_contract;  
  49.     }  
  50.   
  51.     @Override  
  52.     public void initView(View rootView) {  
  53.         ButterKnife.bind(this, rootView);  
  54.         mFriendList = new ArrayList<>();  
  55.         mViewAdapter = new RecycleViewAdapter<NimUserInfo>(getContext(), mFriendList) {  
  56.             @Override  
  57.             public int setItemLayoutId(int position) {  
  58.                 return R.layout.item_friend;  
  59.             }  
  60.   
  61.             @Override  
  62.             public void bindView(RViewHolder holder, int position) {  
  63.                 NimUserInfo item = mFriendList.get(position);  
  64.                 holder.setImageByUrl(getContext(), R.id.iv_head_picture,  
  65.                         item.getAvatar(), R.mipmap.bg_img_defalut);  
  66.                 holder.setText(R.id.tv_friend_nick, item.getName());  
  67.             }  
  68.         };  
  69.         mViewAdapter.setItemClickListener(new OnItemClickListener() {  
  70.             @Override  
  71.             public void onItemClick(RViewHolder holder, int position) {  
  72.                 Intent intent = new Intent(getContext(), FriendInfoActivity.class);  
  73.                 intent.putExtra("NimUserInfo", mFriendList.get(position));  
  74.                 intent.putExtra("FLAG", FriendInfoActivity.FLAG_SHOW_FRIEND);  
  75.                 startActivity(intent);  
  76.             }  
  77.         });  
  78.   
  79.         mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));  
  80.         mRecyclerView.setAdapter(mViewAdapter);  
  81.   
  82.         NimFriendHandler.getInstance().setUpdateListener(new OnFriendUpdateListener() {  
  83.             @Override  
  84.             public void friendUpdate() {  
  85.                 loadFriendList();  
  86.             }  
  87.         });  
  88.   
  89.         loadFriendList();  
  90.   
  91.     }  
  92.   
  93.     private void loadFriendList() {  
  94.         mFriendList.clear();  
  95.         mFriendList.addAll(NimFriendHandler.getInstance().getFriendInfos());  
  96.         mViewAdapter.notifyDataSetChanged();  
  97.     }  
  98.   
  99.   
  100.     @OnClick(R.id.layout_add_friend)  
  101.     public void addFriend() {  
  102.         startActivity(new Intent(getContext(), SearchUserActivity.class));  
  103.     }  
  104.   
  105.     @OnClick(R.id.layout_msg_notify)  
  106.     public void openMsgNotifyActivity() {  
  107.         //hindUnReadMsgView();  
  108.         startActivityForResult(new Intent(getContext(),  
  109.                 CheckNotifyListActivity.class), REQUEST_CHECK_NOTI);  
  110.     }  
  111.   
  112.     @Override  
  113.     public void onActivityResult(int requestCode, int resultCode, Intent data) {  
  114.         super.onActivityResult(requestCode, resultCode, data);  
  115.         if (requestCode == REQUEST_CHECK_NOTI) {  
  116.             if (resultCode == CheckNotifyListActivity.RESULT_HAVE_CHANGE) {  
  117.                 loadFriendList();  
  118.             }  
  119.         }  
  120.     }  
  121. }  


阅读更多

没有更多推荐了,返回首页