实训第三周(2)

李晨晨:

本次主要实现了点击位置消息显示位置地图及信息的showLocActivity和相应的布局文件。


[java]  view plain  copy
  1. @Override  
  2.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setStatusBarColor(R.color.app_blue_color);  
  5.         setContentView(R.layout.activity_show_loc);  
  6.         ButterKnife.bind(this);  
  7.         setTitleBar("位置信息"truefalse);  
  8.         initMap(savedInstanceState);  
  9.         showMsgLocation();  
  10.     }  
1.initMap和上次写的selectLocActivity中的相同,下面为showMsgLocation;
[java]  view plain  copy
  1. private void showMsgLocation(){  
  2.         mIMMessage = (IMMessage) getIntent().getSerializableExtra("IMMessage");  
  3.         LocationAttachment attachment = (LocationAttachment) mIMMessage.getAttachment();  
  4.         if (attachment == null){  
  5.             ToastUtils.showMessage(this,"附件获取失败,请重试~");  
  6.             finish();  
  7.             return;  
  8.         }  
  9.   
  10.         double latitude = attachment.getLatitude();  
  11.         double longitude = attachment.getLongitude();  
  12.         if (latitude < 0.0 || longitude < 0.0){  
  13.             ToastUtils.showMessage(this,"地理坐标失效,无法显示!");  
  14.         }else {  
  15.             LatLng latLng = new LatLng(latitude,longitude);  
  16.             // 显示标记  
  17.             MarkerOptions options = new MarkerOptions();  
  18.             BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_map_center);  
  19.             options.icon(descriptor);  
  20.             options.position(latLng);  
  21.             mAMap.addMarker(options);  
  22.             // 移动地图视角  
  23.             CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, mZoomLevel);  
  24.             mAMap.animateCamera(cameraUpdate);  
  25.         }  
  26.   
  27.         String address = attachment.getAddress();  
  28.         if (!TextUtils.isEmpty(address)){  
  29.             mTvAddress.setText(address);  
  30.         }else {  
  31.             mTvAddress.setText("地址描述获取失败……");  
  32.         }  
  33.     }  

2.返回当前位置:

[java]  view plain  copy
  1. @OnClick(R.id.iv_my_location)  
  2.     public void location(){  
  3.         mLocationClient.startLocation();  
  4.     }  
  5.   
  6.     @Override  
  7.     public void onLocationChanged(AMapLocation location) {  
  8.         if (location != null) {  
  9.             if (location.getErrorCode() == 0) {  
  10.                 LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());  
  11.   
  12.                 MarkerOptions options = new MarkerOptions();  
  13.                 BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_my_loc);  
  14.                 options.icon(descriptor);  
  15.                 options.position(latLng);  
  16.                 mAMap.addMarker(options);  
  17.   
  18.                 CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, mZoomLevel);  
  19.                 mAMap.animateCamera(cameraUpdate);  
  20.             }  
  21.         }  
  22.     }  

3.过程中用到的LocationPoint

[java]  view plain  copy
  1. /** 
  2.  * 分享位置,单个位置点信息 
  3.  * Created by  
  4.  */  
  5.   
  6. public class LocationPoint {  
  7.   
  8.     private String mId;  
  9.     private String mName;  
  10.     private String mAddress;  
  11.     private boolean mSelected;  
  12.     private LatLonPoint mPoint;  
  13.   
  14.     public String getId() {  
  15.         return mId;  
  16.     }  
  17.   
  18.     public void setId(String id) {  
  19.         mId = id;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return mName;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         mName = name;  
  28.     }  
  29.   
  30.     public String getAddress() {  
  31.         return mAddress;  
  32.     }  
  33.   
  34.     public void setAddress(String address) {  
  35.         mAddress = address;  
  36.     }  
  37.   
  38.     public boolean isSelected() {  
  39.         return mSelected;  
  40.     }  
  41.   
  42.     public void setSelected(boolean selected) {  
  43.         mSelected = selected;  
  44.     }  
  45.   
  46.     public LatLonPoint getPoint() {  
  47.         return mPoint;  
  48.     }  
  49.   
  50.     public void setPoint(LatLonPoint point) {  
  51.         mPoint = point;  
  52.     }  
  53. }  

最终效果:




仝心:

这次我对聊天应用的表情包进行了编写,包括聊天表情图片的加载、聊天表情的发送(分为两部分,系统键盘输入时和自定义键盘输入时分情况处理)

EmojiUtils类中先定义两个个数组来存储所有的表情,一个其中存R.drawable中的图片,另一个存表情对应的所有字符串

[java]  view plain  copy
  1. private static final int[] EMOJI_INDEX = {  
  2.         R.drawable.d_hehe, // 呵呵  
  3.         R.drawable.d_keai, // 可爱  
  4.         R.drawable.d_taikaixin, // 太开心  
  5.         R.drawable.d_guzhang, // 鼓掌  
  6.         R.drawable.d_xixi, // 嘻嘻  
  7.         R.drawable.d_haha, // 哈哈  
  8.         R.drawable.d_xiaoku, // 笑哭  
  9.         R.drawable.d_tiaopi, // 调皮  
  10.         R.drawable.d_chanzui,// 馋嘴  
  11.         R.drawable.d_heixian, // 黑线  
  12.         R.drawable.d_han, // 汗  
  13.         R.drawable.d_wabishi, // 挖鼻屎  
  14.         R.drawable.d_heng, // 哼  
  15.         R.drawable.d_nu, // 怒  
  16.         R.drawable.d_kelian, // 可怜  
  17.         R.drawable.d_liulei, // 流泪  
  18.         R.drawable.d_daku, // 大哭  
  19.         R.drawable.d_haixiu,// 害羞  
  20.         R.drawable.d_aini, // 爱你  
  21.         R.drawable.d_qinqin,// 亲亲  
  22.         R.drawable.face_delete,// 删除键  
  23.   
  24.         R.drawable.d_doge, // doge  
  25.         R.drawable.d_miao, // miao  
  26.         R.drawable.d_yinxian, //阴险  
  27.         R.drawable.d_touxiao,// 偷笑  
  28.         R.drawable.d_ku, // 酷  
  29.         R.drawable.d_sikao, // 思考  
  30.         R.drawable.d_baibai, // 拜拜  
  31.         R.drawable.d_bishi, // 鄙视  
  32.         R.drawable.d_bizui, // 闭嘴  
  33.         R.drawable.d_chijing, // 吃惊  
  34.         R.drawable.d_dahaqi, // 打哈欠  
  35.         R.drawable.d_dalian, // 打脸  
  36.         R.drawable.d_ganmao, // 感冒  
  37.         R.drawable.d_kun, // 困  
  38.         R.drawable.d_zhouma, // 咒骂  
  39.         R.drawable.d_shengbing,// 生病  
  40.         R.drawable.d_shiwang, // 失望  
  41.         R.drawable.d_shuai, // 衰  
  42.         R.drawable.d_shuijiao, // 睡觉  
  43.         R.drawable.d_tu, // 吐  
  44.         R.drawable.face_delete,// 删除  
  45.   
  46.         R.drawable.d_weiqu, // 委屈  
  47.         R.drawable.d_xu, // 嘘  
  48.         R.drawable.d_yiwen, // 疑问  
  49.         R.drawable.d_yun, // 晕  
  50.         R.drawable.d_zuohengheng, // 左哼哼  
  51.         R.drawable.d_youhengheng, // 右哼哼  
  52.         R.drawable.d_zhuakuang,// 抓狂  
  53.         R.drawable.d_zhutou, // 猪头  
  54.         R.drawable.xinsui, // 心碎  
  55.         R.drawable.l_xin, // 心  
  56.         R.drawable.h_xihuanni, // 喜欢你  
  57.         R.drawable.h_buyao, // 不要  
  58.         R.drawable.h_bang, // 棒  
  59.         R.drawable.h_lai,  // 来  
  60.         R.drawable.h_ok,  // OK  
  61.         R.drawable.h_quantou, // 拳头  
  62.         R.drawable.h_ruo, // 弱  
  63.         R.drawable.h_woshou, // 握手  
  64.         R.drawable.h_shengli, //胜利  
  65.         R.drawable.h_zan, // 赞  
  66.         R.drawable.face_delete,// 删除  
  67.   
  68.         R.drawable.o_lazhu, // 蜡烛  
  69.         R.drawable.o_liwu, // 礼物  
  70.         R.drawable.o_dangao, // 蛋糕  
  71.         R.drawable.o_feiji, // 飞机  
  72.         R.drawable.o_ganbei, // 干杯  
  73.         R.drawable.o_weiguan, // 围观  
  74.         R.drawable.w_fuyun, // 云  
  75.         R.drawable.w_taiyang, // 太阳  
  76.         R.drawable.w_weifeng, // 微风  
  77.         R.drawable.w_xiayu, // 下雨  
  78.         R.drawable.w_yueliang, // 月亮  
  79.         R.drawable.face_delete // 删除  
  80. };  
  81.   
  82. private static final String[] EMOJI_NAME = {  
  83.         "[呵呵]",  
  84.         "[可爱]",  
  85.         "[太开心]",  
  86.         "[鼓掌]",  
  87.         "[嘻嘻]",  
  88.         "[哈哈]",  
  89.         "[笑哭]",  
  90.         "[调皮]",  
  91.         "[馋嘴]",  
  92.         "[黑线]",  
  93.         "[汗]",  
  94.         "[挖鼻屎]",  
  95.         "[哼]",  
  96.         "[怒]",  
  97.         "[可怜]",  
  98.         "[流泪]",  
  99.         "[大哭]",  
  100.         "[害羞]",  
  101.         "[爱你]",  
  102.         "[亲亲]",  
  103.         "[删除]",  
  104.   
  105.         "[doge]",  
  106.         "[miao]",  
  107.         "[阴险]",  
  108.         "[偷笑]",  
  109.         "[酷]",  
  110.         "[思考]",  
  111.         "[拜拜]",  
  112.         "[鄙视]",  
  113.         "[闭嘴]",  
  114.         "[吃惊]",  
  115.         "[打哈欠]",  
  116.         "[打脸]",  
  117.         "[感冒]",  
  118.         "[困]",  
  119.         "[咒骂]",  
  120.         "[生病]",  
  121.         "[失望]",  
  122.         "[衰]",  
  123.         "[睡觉]",  
  124.         "[吐]",  
  125.         "[删除]",  
  126.   
  127.         "[委屈]",  
  128.         "[嘘]",  
  129.         "[疑问]",  
  130.         "[晕]",  
  131.         "[左哼哼]",  
  132.         "[右哼哼]",  
  133.         "[抓狂]",  
  134.         "[猪头]",  
  135.         "[心碎]",  
  136.         "[心]",  
  137.         "[喜欢你]",  
  138.         "[不要]",  
  139.         "[棒]",  
  140.         "[来]",  
  141.         "[OK]",  
  142.         "[拳头]",  
  143.         "[弱]",  
  144.         "[握手]",  
  145.         "[胜利]",  
  146.         "[赞]",  
  147.         "[删除]",  
  148.   
  149.         "[蜡烛]",  
  150.         "[礼物]",  
  151.         "[蛋糕]",  
  152.         "[飞机]",  
  153.         "[干杯]",  
  154.         "[围观]",  
  155.         "[云]",  
  156.         "[太阳]",  
  157.         "[微风]",  
  158.         "[下雨]",  
  159.         "[月亮]",  
  160.         "[删除]",  
  161. };  

编写方法用于将字符串和表情图片在发送过程中进行匹配

[java]  view plain  copy
  1. public static SpannableString text2Emoji(Context context,final String source,final float textSize) {  
  2.         SpannableString spannableString = new SpannableString(source);  
  3.         Resources res = context.getResources();  
  4.         String regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]";  
  5.         Pattern patternEmotion = Pattern.compile(regexEmotion);  
  6.         Matcher matcherEmotion = patternEmotion.matcher(spannableString);  
  7.         while (matcherEmotion.find()) {  
  8.             // 获取匹配到的具体字符  
  9.             String key = matcherEmotion.group();  
  10.             // 匹配字符串的开始位置  
  11.             int start = matcherEmotion.start();  
  12.             // 利用表情名字获取到对应的图片  
  13.            Integer imgRes = getImgByName(key);  
  14.             if (imgRes != 0) {  
  15.                 // 压缩表情图片  
  16.                 int size = (int) (textSize * 13.0f / 10.0f);  
  17.                 Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);  
  18.                 Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);  
  19.                 ImageSpan span = new ImageSpan(context, scaleBitmap);  
  20.                 spannableString.setSpan(span, start, start + key.length(),  
  21.                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  22.             }  
  23.         }  
  24.         return spannableString;  
  25.     }  

表情包的布局类,接口内选择和删除的方法在另一个类中实现

[java]  view plain  copy
  1. private void init(){  
  2.     mExpressViews = new ArrayList<>();  
  3.     mEmojiLayout = new EmojiLayout(getContext());  
  4.     mEmojiLayout.setSelectListener(new EmojiLayout.OnEmojiSelectListener() {  
  5.         @Override  
  6.         public void emojiSelect(EmojiBean emojiBean) {  
  7.             if (mOnExpressSelListener != null){  
  8.                 mOnExpressSelListener.onEmojiSelect(emojiBean);  
  9.             }  
  10.         }  
  11.   
  12.         @Override  
  13.         public void emojiDelete() {  
  14.             if (mOnExpressSelListener != null){  
  15.                 mOnExpressSelListener.onEmojiDelete();  
  16.             }  
  17.         }  
  18.     });  





张静:

本周接下来我完成了MainActivity中声明的RecentMsgFragment, 完成界面中的“消息”部分

一. 先写一个BaseFragment,利于之后其他自定义Fragment继承

[java]  view plain  copy
  1. package com.ezreal.ezchat.fragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. /** 
  10.  * Created by 张静. 
  11.  */  
  12.   
  13. public abstract class BaseFragment extends Fragment {  
  14.   
  15.     protected View mRootView;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.     }  
  21.   
  22.     //创建该fragment的视图  
  23.     @Override  
  24.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  25.                              Bundle savedInstanceState) {  
  26.         if (mRootView == null) {  
  27.             mRootView = inflater.inflate(setLayoutID(), container, false);  
  28.             initView(mRootView);  
  29.         }  
  30.         //缓存的rootView需要判断是否已经被加过parent  
  31.         //如果有parent需要从parent删除,要不然会发生这个rootView已经有parent的错误。  
  32.         ViewGroup parent = (ViewGroup) mRootView.getParent();  
  33.         if (parent != null) {  
  34.             parent.removeView(mRootView);  
  35.         }  
  36.         return mRootView;  
  37.     }  
  38.   
  39.     public abstract int setLayoutID(); //设置加载的布局ID  
  40.     public abstract void initView(View rootView);  
  41.   
  42. }  


二. RecentContactBean

设置属性最近联系人,用户资料以及获取和设置两个属性的方法

[java]  view plain  copy
  1. package com.ezreal.ezchat.bean;  
  2.   
  3. import com.netease.nimlib.sdk.msg.model.RecentContact;  
  4. import com.netease.nimlib.sdk.uinfo.UserInfoProvider;  
  5.   
  6. /** 
  7.  * Created by 张静. 
  8.  */  
  9.   
  10. public class RecentContactBean {  
  11.     private RecentContact mRecentContact;  
  12.     private UserInfoProvider.UserInfo mUserInfo;  
  13.   
  14.     public RecentContact getRecentContact() {  
  15.         return mRecentContact;  
  16.     }  
  17.   
  18.     public void setRecentContact(RecentContact recentContact) {  
  19.         mRecentContact = recentContact;  
  20.     }  
  21.   
  22.     public UserInfoProvider.UserInfo getUserInfo() {  
  23.         return mUserInfo;  
  24.     }  
  25.   
  26.     public void setUserInfo(UserInfoProvider.UserInfo userInfo) {  
  27.         mUserInfo = userInfo;  
  28.     }  
  29. }  

三. RecentMsgFragment

1. 设置加载的布局ID

[java]  view plain  copy
  1. @Override  
  2. public int setLayoutID() {  
  3.     return R.layout.fragment_message;  
  4. }  
其中,fragment_message.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:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     android:background="@color/interval_color">  
  8.   
  9.     <android.support.v7.widget.RecyclerView  
  10.         android:id="@+id/rcv_message_list"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="match_parent"/>  
  13.   
  14. </LinearLayout>  

2.初始化RecyclerView  (RecycleViewAdapter以及RViewHolder已由另一位组员编写完成)

(1)为RecyclerView创建Adapter

       a.给item设置加载的布局ID

       b.找到目标位置的数据并绑定到holder上

          数据:头像,名称,最近对话,该次对话时间

(2)给RecyclerView的item添加点击事件

        若点击的该item获取到的会话类型为单聊(P2P),跳转到P2P聊天界面

(3)给RecyclerView设置适配器为mViewAdapter

[java]  view plain  copy
  1. private void initRecyclerView(){  
  2.         mLayoutManager = new LinearLayoutManager(getContext());  
  3.         mContactList = new ArrayList<>();  
  4.         mRecyclerView.setLayoutManager(mLayoutManager);  
  5.         mViewAdapter = new RecycleViewAdapter<RecentContactBean>(getContext(),mContactList) {  
  6.             @Override  
  7.             public int setItemLayoutId(int position) {  
  8.                 return R.layout.item_recent_msg;  
  9.             }  
  10.   
  11.             @Override  
  12.             public void bindView(RViewHolder holder, int position) {  
  13.                 RecentContactBean contactBean= mContactList.get(position);  
  14.                 holder.setImageByUrl(getContext(),R.id.iv_head_picture,  
  15.                         contactBean.getUserInfo().getAvatar(),R.mipmap.bg_img_defalut);  
  16.                 holder.setText(R.id.tv_recent_name,contactBean.getUserInfo().getName());  
  17.                 holder.setText(R.id.tv_recent_content,contactBean.getRecentContact().getContent());  
  18.                 String time = mDateFormat.format(new Date(contactBean.getRecentContact().getTime()));  
  19.                 holder.setText(R.id.tv_recent_time,time);  
  20.             }  
  21.         };  
  22.   
  23.         mViewAdapter.setItemClickListener(new OnItemClickListener() {  
  24.             @Override  
  25.             public void onItemClick(RViewHolder holder, int position) {  
  26.                 RecentContactBean contactBean = mContactList.get(position);  
  27.                 Intent intent;  
  28.                 if (contactBean.getRecentContact().getSessionType() == SessionTypeEnum.P2P){  
  29.                     intent = new Intent(getContext(), P2PChatActivity.class);  
  30.                     intent.putExtra("NimUserInfo",contactBean.getUserInfo());  
  31.                     startActivity(intent);  
  32.                 }  
  33.             }  
  34.         });  
  35.   
  36.         mRecyclerView.setAdapter(mViewAdapter);  
  37.     }  
3.初始化监听器
[java]  view plain  copy
  1. private void initListener(){  
  2.         mObserver = new Observer<List<RecentContact>>() {  
  3.             @Override  
  4.             public void onEvent(List<RecentContact> recentContacts) {  
  5.                 Log.e(TAG,"Observer RecentContact size = " + recentContacts.size());  
  6.                 if (mContactList.isEmpty()){  
  7.                     List<RecentContactBean> contactBeans = createContactBeans(recentContacts);  
  8.                     mContactList.addAll(contactBeans);  
  9.                     mViewAdapter.notifyDataSetChanged();  
  10.                     return;  
  11.                 }  
  12.                 for (RecentContact contact : recentContacts){  
  13.                     refreshRecentList(contact);  
  14.                 }  
  15.             }  
  16.         };  
  17.     }  

4. 刷新最近通话列表

[java]  view plain  copy
  1. private void refreshRecentList(RecentContact contact){  
  2.         for (int i=0;i<mContactList.size();i++){  
  3.             RecentContactBean bean = mContactList.get(i);  
  4.             if (bean.getRecentContact().getContactId().equals(contact.getContactId())){  
  5.                 bean.setRecentContact(contact);  
  6.                 mViewAdapter.notifyItemChanged(i);  
  7.                 break;  
  8.             }else if (i == mContactList.size()-1){  
  9.                 // 否则为新的最近会话  
  10.                 RecentContactBean newBean = new RecentContactBean();  
  11.                 newBean.setRecentContact(contact);  
  12.                 newBean.setUserInfo(getUserInfoByAccount(contact.getContactId()));  
  13.                 mContactList.add(0,newBean);  
  14.             }  
  15.         }  
  16.     }  

5.加载最近联系人列表

通过NimClient的getService接口获取到MsgService(云信消息服务接口)服务实例,调用queryRecentContacts方法查询最近联系人列表数据

若有错则返回,否则加载最近联系人列表

[java]  view plain  copy
  1. private void loadRecentList(){  
  2.         NIMClient.getService(MsgService.class).queryRecentContacts()  
  3.                 .setCallback(new RequestCallbackWrapper<List<RecentContact>>() {  
  4.             @Override  
  5.             public void onResult(int code, List<RecentContact> result, Throwable exception) {  
  6.                 if (exception != null){  
  7.                     Log.e(TAG,"loadRecentList exception = " + exception.getMessage());  
  8.                     return;  
  9.                 }  
  10.                 if (code != 200){  
  11.                     Log.e(TAG,"loadRecentList error code = " + code);  
  12.                     return;  
  13.                 }  
  14.                 Log.e(TAG,"loadRecentList size = " + result.size());  
  15.                 List<RecentContactBean> contactBeans = createContactBeans(result);  
  16.                 mContactList.clear();  
  17.                 mContactList.addAll(contactBeans);  
  18.                 mViewAdapter.notifyDataSetChanged();  
  19.             }  
  20.         });  
  21.     }  



附上完整代码:

RecentMsgFragment.java

[java]  view plain  copy
  1. package com.ezreal.ezchat.fragment;  
  2.   
  3.   
  4. import android.content.Intent;  
  5. import android.support.v7.widget.LinearLayoutManager;  
  6. import android.support.v7.widget.RecyclerView;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import com.ezreal.ezchat.R;  
  10. import com.ezreal.ezchat.activity.P2PChatActivity;  
  11. import com.ezreal.ezchat.bean.RecentContactBean;  
  12. import com.netease.nimlib.sdk.NIMClient;  
  13. import com.netease.nimlib.sdk.Observer;  
  14. import com.netease.nimlib.sdk.RequestCallbackWrapper;  
  15. import com.netease.nimlib.sdk.msg.MsgService;  
  16. import com.netease.nimlib.sdk.msg.MsgServiceObserve;  
  17. import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;  
  18. import com.netease.nimlib.sdk.msg.model.RecentContact;  
  19. import com.netease.nimlib.sdk.uinfo.UserService;  
  20. import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;  
  21. import com.suntek.commonlibrary.adapter.OnItemClickListener;  
  22. import com.suntek.commonlibrary.adapter.RViewHolder;  
  23. import com.suntek.commonlibrary.adapter.RecycleViewAdapter;  
  24.   
  25. import java.text.SimpleDateFormat;  
  26. import java.util.ArrayList;  
  27. import java.util.Date;  
  28. import java.util.List;  
  29.   
  30.   
  31. /** 
  32.  * Created by 张静. 
  33.  */  
  34.   
  35. public class RecentMsgFragment extends BaseFragment {  
  36.   
  37.     private static final String TAG = RecentMsgFragment.class.getSimpleName();  
  38.     private RecyclerView mRecyclerView;  
  39.     private LinearLayoutManager mLayoutManager;  
  40.     private List<RecentContactBean> mContactList;//最近联系人  
  41.     private RecycleViewAdapter<RecentContactBean> mViewAdapter;  
  42.     private Observer<List<RecentContact>> mObserver;  
  43.     private SimpleDateFormat mDateFormat;  
  44.   
  45.   
  46.     @Override  
  47.     public int setLayoutID() {  
  48.         return R.layout.fragment_message;  
  49.     }  
  50.   
  51.     @Override  
  52.     public void initView(View rootView) {  
  53.         mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rcv_message_list);  
  54.         mDateFormat = new SimpleDateFormat("HH:mm");  
  55.         initRecyclerView();  
  56.         initListener();  
  57.         loadRecentList();  
  58.         NIMClient.getService(MsgServiceObserve.class).observeRecentContact(mObserver,true);  
  59.     }  
  60.   
  61.     private void initRecyclerView(){  
  62.         mLayoutManager = new LinearLayoutManager(getContext());  
  63.         mContactList = new ArrayList<>();  
  64.         mRecyclerView.setLayoutManager(mLayoutManager);  
  65.         mViewAdapter = new RecycleViewAdapter<RecentContactBean>(getContext(),mContactList) {  
  66.             @Override  
  67.             public int setItemLayoutId(int position) {  
  68.                 return R.layout.item_recent_msg;  
  69.             }  
  70.   
  71.             @Override  
  72.             public void bindView(RViewHolder holder, int position) {  
  73.                 RecentContactBean contactBean= mContactList.get(position);  
  74.                 holder.setImageByUrl(getContext(),R.id.iv_head_picture,  
  75.                         contactBean.getUserInfo().getAvatar(),R.mipmap.bg_img_defalut);  
  76.                 holder.setText(R.id.tv_recent_name,contactBean.getUserInfo().getName());  
  77.                 holder.setText(R.id.tv_recent_content,contactBean.getRecentContact().getContent());  
  78.                 String time = mDateFormat.format(new Date(contactBean.getRecentContact().getTime()));  
  79.                 holder.setText(R.id.tv_recent_time,time);  
  80.             }  
  81.         };  
  82.   
  83.         mViewAdapter.setItemClickListener(new OnItemClickListener() {  
  84.             @Override  
  85.             public void onItemClick(RViewHolder holder, int position) {  
  86.                 RecentContactBean contactBean = mContactList.get(position);  
  87.                 Intent intent;  
  88.                 if (contactBean.getRecentContact().getSessionType() == SessionTypeEnum.P2P){  
  89.                     intent = new Intent(getContext(), P2PChatActivity.class);  
  90.                     intent.putExtra("NimUserInfo",contactBean.getUserInfo());  
  91.                     startActivity(intent);  
  92.                 }  
  93.             }  
  94.         });  
  95.   
  96.         mRecyclerView.setAdapter(mViewAdapter);  
  97.     }  
  98.   
  99.     private void initListener(){  
  100.         mObserver = new Observer<List<RecentContact>>() {  
  101.             @Override  
  102.             public void onEvent(List<RecentContact> recentContacts) {  
  103.                 Log.e(TAG,"Observer RecentContact size = " + recentContacts.size());  
  104.                 if (mContactList.isEmpty()){  
  105.                     List<RecentContactBean> contactBeans = createContactBeans(recentContacts);  
  106.                     mContactList.addAll(contactBeans);  
  107.                     mViewAdapter.notifyDataSetChanged();  
  108.                     return;  
  109.                 }  
  110.                 for (RecentContact contact : recentContacts){  
  111.                     refreshRecentList(contact);  
  112.                 }  
  113.             }  
  114.         };  
  115.     }  
  116.   
  117.     private void refreshRecentList(RecentContact contact){  
  118.         for (int i=0;i<mContactList.size();i++){  
  119.             RecentContactBean bean = mContactList.get(i);  
  120.             if (bean.getRecentContact().getContactId().equals(contact.getContactId())){  
  121.                 bean.setRecentContact(contact);  
  122.                 mViewAdapter.notifyItemChanged(i);  
  123.                 break;  
  124.             }else if (i == mContactList.size()-1){  
  125.                 // 否则为新的最近会话  
  126.                 RecentContactBean newBean = new RecentContactBean();  
  127.                 newBean.setRecentContact(contact);  
  128.                 newBean.setUserInfo(getUserInfoByAccount(contact.getContactId()));  
  129.                 mContactList.add(0,newBean);  
  130.             }  
  131.         }  
  132.     }  
  133.   
  134.     @Override  
  135.     public void onResume() {  
  136.         super.onResume();  
  137.         Log.e(TAG,"onResume");  
  138.     }  
  139.   
  140.     @Override  
  141.     public void onPause() {  
  142.         super.onPause();  
  143.         Log.e(TAG,"onPause");  
  144.   
  145.     }  
  146.   
  147.     private void loadRecentList(){  
  148.         NIMClient.getService(MsgService.class).queryRecentContacts()  
  149.                 .setCallback(new RequestCallbackWrapper<List<RecentContact>>() {  
  150.             @Override  
  151.             public void onResult(int code, List<RecentContact> result, Throwable exception) {  
  152.                 if (exception != null){  
  153.                     Log.e(TAG,"loadRecentList exception = " + exception.getMessage());  
  154.                     return;  
  155.                 }  
  156.                 if (code != 200){  
  157.                     Log.e(TAG,"loadRecentList error code = " + code);  
  158.                     return;  
  159.                 }  
  160.                 Log.e(TAG,"loadRecentList size = " + result.size());  
  161.                 List<RecentContactBean> contactBeans = createContactBeans(result);  
  162.                 mContactList.clear();  
  163.                 mContactList.addAll(contactBeans);  
  164.                 mViewAdapter.notifyDataSetChanged();  
  165.             }  
  166.         });  
  167.     }  
  168.   
  169.     private List<RecentContactBean> createContactBeans(List<RecentContact> recentContacts){  
  170.         List<RecentContactBean> beanList = new ArrayList<>();  
  171.         RecentContactBean bean;  
  172.         for (RecentContact contact : recentContacts){  
  173.             bean = new RecentContactBean();  
  174.             bean.setRecentContact(contact);  
  175.             bean.setUserInfo(getUserInfoByAccount(contact.getContactId()));  
  176.             beanList.add(bean);  
  177.         }  
  178.         return beanList;  
  179.     }  
  180.   
  181.     private NimUserInfo getUserInfoByAccount(String account){  
  182.         return NIMClient.getService(UserService.class).getUserInfo(account);  
  183.     }  
  184. }  

item_recent_msg.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  6.     android:layout_width="match_parent"  
  7.     android:background="@color/white_color"  
  8.     android:layout_height="61dp"  
  9.     android:gravity="center_vertical">  
  10.   
  11.     <LinearLayout  
  12.         android:id="@+id/layout_recent_contact"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="60dp"  
  15.         android:orientation="horizontal"  
  16.         android:layout_marginLeft="10dp"  
  17.         android:layout_marginRight="10dp"  
  18.         android:layout_alignParentTop="true"  
  19.         android:layout_centerHorizontal="true">  
  20.   
  21.         <com.joooonho.SelectableRoundedImageView  
  22.             android:layout_marginTop="5dp"  
  23.             android:padding="5dp"  
  24.             android:id="@+id/iv_head_picture"  
  25.             android:layout_width="50dp"  
  26.             android:layout_height="50dp"  
  27.             android:scaleType="fitXY"  
  28.             app:sriv_oval="true"/>  
  29.   
  30.         <LinearLayout  
  31.             android:id="@+id/msg_detail"  
  32.             android:layout_width="0dp"  
  33.             android:layout_height="match_parent"  
  34.             android:layout_marginLeft="5dp"  
  35.             android:layout_weight="1"  
  36.             android:gravity="center_vertical"  
  37.             android:orientation="vertical">  
  38.   
  39.             <TextView  
  40.                 android:id="@+id/tv_recent_name"  
  41.                 android:layout_width="wrap_content"  
  42.                 android:layout_height="wrap_content"  
  43.                 android:maxLines="1"  
  44.                 android:text="@string/app_name"  
  45.                 android:textColor="@color/app_black_color"/>  
  46.   
  47.             <TextView  
  48.                 android:id="@+id/tv_recent_content"  
  49.                 android:layout_width="wrap_content"  
  50.                 android:layout_height="wrap_content"  
  51.                 android:layout_marginTop="10dp"  
  52.                 android:maxLines="1"  
  53.                 android:text="@string/test_msg"  
  54.                 android:textColor="@color/default_text_color"/>  
  55.         </LinearLayout>  
  56.   
  57.   
  58.         <TextView  
  59.             android:id="@+id/tv_recent_time"  
  60.             android:layout_marginLeft="5dp"  
  61.             android:layout_width="50dp"  
  62.             android:layout_height="30dp"  
  63.             android:layout_marginTop="5dp"  
  64.             android:gravity="center"  
  65.             android:text="@string/test_time"  
  66.             android:textColor="@color/default_text_color"  
  67.             android:visibility="visible"/>  
  68.   
  69.     </LinearLayout>  
  70.   
  71.     <View  
  72.         android:layout_marginTop="5dp"  
  73.         android:layout_below="@+id/layout_notify"  
  74.         android:layout_width="match_parent"  
  75.         android:layout_height="0.8dp"  
  76.         android:background="@color/interval_color"  
  77.         android:layout_alignParentBottom="true"/>  
  78.   
  79. </RelativeLayout>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值