Android 开发即时聊天工具 YQ :(六) 最近会话列表
实现的效果如图:
RecentActivity:
1 <span style="font-size: 14px;">public class RecentActivity extends Activity{ 2 ListView listView; 3 List<RecentEntity> chatEntityList=new ArrayList<RecentEntity>(); 4 String[] mes; 5 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 requestWindowFeature(Window.FEATURE_NO_TITLE); 9 setContentView(R.layout.activity_recent); 10 //注册广播 11 IntentFilter myIntentFilter = new IntentFilter(); 12 myIntentFilter.addAction("org.yhn.yq.mes"); 13 registerReceiver(new MyBroadcastReceiver(), myIntentFilter); 14 15 listView = (ListView) findViewById(R.id.lv_recent); 16 listView.setOnItemClickListener(new OnItemClickListener(){ 17 public void onItemClick(AdapterView<?> arg0, View arg1, int position, 18 long arg3) { 19 //打开聊天页面 20 Intent intent=new Intent(RecentActivity.this,ChatActivity.class); 21 intent.putExtra("account", Integer.parseInt(mes[0])); 22 intent.putExtra("nick", ""); 23 startActivity(intent); 24 } 25 }); 26 } 27 28 //广播接收器 29 public class MyBroadcastReceiver extends BroadcastReceiver { 30 @Override 31 public void onReceive(Context context, Intent intent) { 32 mes = intent.getStringArrayExtra("message"); 33 Toast.makeText(context, "收到["+Integer.parseInt(mes[0])+"]的消息:"+mes[1], Toast.LENGTH_SHORT).show(); 34 //更新最近会话列表, 检测chatEntityList,防止同一个好友的消息出现多个会话实体 35 Iterator it=chatEntityList.iterator(); 36 if(chatEntityList!=null && chatEntityList.size()!=0){ 37 while(it.hasNext()){ 38 RecentEntity re=(RecentEntity) it.next(); 39 if(re.getAccount()==Integer.parseInt(mes[0])){ 40 chatEntityList.remove(re); 41 } 42 } 43 } 44 chatEntityList.add(new RecentEntity(5, Integer.parseInt(mes[0]), mes[0]+"", mes[1],mes[2], false)); 45 listView.setAdapter(new RecentAdapter(RecentActivity.this, chatEntityList)); 46 unregisterReceiver(this); 47 } 48 } 49 } 50 </span>
RecentActivity中listview的Adapter:
1 <span style="font-size: 14px;">public class RecentAdapter extends BaseAdapter{ 2 private Context context; 3 private List<RecentEntity> list; 4 LayoutInflater inflater; 5 6 public RecentAdapter(Context context,List<RecentEntity> list){ 7 this.context = context; 8 this.list = list; 9 inflater = LayoutInflater.from(context); 10 } 11 12 public View getView(int position, View convertView, ViewGroup root) { 13 convertView = inflater.inflate(R.layout.recent_listview_item, null); 14 15 ImageView avatar=(ImageView) convertView.findViewById(R.id.iv_avatar_r); 16 TextView nick=(TextView) convertView.findViewById(R.id.tv_nick_r); 17 TextView content=(TextView) convertView.findViewById(R.id.tv_chat_content_r); 18 ImageView isRead=(ImageView) convertView.findViewById(R.id.iv_tip_mes_r); 19 TextView time=(TextView) convertView.findViewById(R.id.tv_time_r); 20 21 RecentEntity re=list.get(position); 22 nick.setText(re.getNick()); 23 content.setText(re.getContent()); 24 isRead.setImageResource(R.drawable.tips_message); 25 time.setText(re.getTime()); 26 27 return convertView; 28 } 29 public int getCount() { 30 return list.size(); 31 } 32 33 public Object getItem(int position) { 34 return list.get(position); 35 } 36 37 public long getItemId(int position) { 38 return position; 39 } 40 } 41 </span>
消息实体类,就不贴代码了,很简单,你懂的
--------------------------------------------------分割线----------------------------------------------------------
完善了部分界面,以及一些细节,
后台运行,
增加了用户头像(简易方式,暂不能自定义头像),
用户个人信息
。。。
具体就不说了看图吧:
谢谢大家支持!欢迎一起学习交流!