欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ListView实现聊天列表之处理不同数据项

程序员文章站 2023-11-04 12:36:52
    通常我们用惯的listview每一项的布局都是相同的,只是控件所绑定的数据不同。但单单只是如此并不能满足我们某些特殊需求,比如我们常见...

    通常我们用惯的listview每一项的布局都是相同的,只是控件所绑定的数据不同。但单单只是如此并不能满足我们某些特殊需求,比如我们常见的qq、微信的聊天列表,除了有左右之分外,内容更是有很大区别,有文字、语音、图片、视频等等,他们真的是listview可以实现的吗?答案是肯定的,只要我们做一下类型区别即可。

实现效果如下所示:

ListView实现聊天列表之处理不同数据项

    大家不要在意布局,这里为了方便就随意了。大家可以看到,这里有两种布局,一种头像在左,一种头像在右,虽然这是一种简单的情况,但我们只需要了解其中的原理,再复杂的情况都可以迎刃而解。
我们只要将每一种布局划为一种类型进行区分,根据我们所区分的类型在我们自定义的adapter中加载不同布局即可,代码如下所示:

if (bean.gettype() == 1) { 
      convertview = minflater.inflate(r.layout.item_chat_left, null); 
      holder.mivavatar = (imageview) convertview.findviewbyid(r.id.iv_chatleftitem_avatar); 
      holder.mtvcontent = (textview) convertview.findviewbyid(r.id.tv_chatleftitem_content); 
  } else if (bean.gettype() == 2) { 
      convertview = minflater.inflate(r.layout.item_chat_right, null); 
      holder.mivavatar = (imageview) convertview.findviewbyid(r.id.iv_chatrightitem_avatar); 
      holder.mtvcontent = (textview) convertview.findviewbyid(r.id.tv_chatrightitem_content); 
  } 

是不是发现很简单,最后,我再将所有代码一同贴出来。

item_chat_left.xml

<?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" 
  android:padding="6dp" > 
 
  <imageview 
    android:id="@+id/iv_chatleftitem_avatar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#dd00f0" 
    android:src="@drawable/ic_launcher" /> 
 
  <textview 
    android:id="@+id/tv_chatleftitem_content" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textsize="16sp" 
    android:padding="16dp" 
    android:layout_torightof="@id/iv_chatleftitem_avatar" 
    android:background="@drawable/qfav_list_bubble_nor" 
    android:layout_marginright="64dp" 
    android:gravity="center_vertical" /> 
 
</relativelayout> 

item_chat_right.xml

<?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" 
  android:padding="6dp" > 
   
  <imageview 
    android:id="@+id/iv_chatrightitem_avatar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignparentright="true" 
    android:background="#ddf000" 
    android:src="@drawable/ic_launcher" /> 
   
  <textview 
    android:id="@+id/tv_chatrightitem_content" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textsize="16sp" 
    android:padding="16dp" 
    android:layout_toleftof="@id/iv_chatrightitem_avatar" 
    android:background="@drawable/skin_aio_user_bubble_pressed" 
    android:layout_marginleft="64dp" 
    android:gravity="center_vertical" /> 
   
</relativelayout> 

activity_chat.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context="${relativepackage}.${activityclass}" > 
 
  <listview 
    android:id="@+id/lv_content" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" 
    android:divider="@null" 
    android:listselector="@android:color/transparent" > 
  </listview> 
 
</relativelayout> 

chatitembean.java

import android.graphics.bitmap; 
 
public class chatitembean { 
  private int type; 
  private string content; 
  private bitmap avatar; 
  public int gettype() { 
    return type; 
  } 
  public void settype(int type) { 
    this.type = type; 
  } 
  public string getcontent() { 
    return content; 
  } 
  public void setcontent(string content) { 
    this.content = content; 
  } 
  public bitmap getavatar() { 
    return avatar; 
  } 
  public void setavatar(bitmap avatar) { 
    this.avatar = avatar; 
  } 
} 

chatlvadapter.java

import java.util.list; 
 
import android.content.context; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.baseadapter; 
import android.widget.imageview; 
import android.widget.textview; 
 
public class chatlvadapter extends baseadapter { 
 
  private list<chatitembean> mdatas; 
  private layoutinflater minflater; 
   
  public chatlvadapter(context context, list<chatitembean> datas) { 
    this.minflater = layoutinflater.from(context); 
    this.mdatas = datas; 
  } 
   
  @override 
  public int getcount() { 
    return mdatas.size(); 
  } 
 
  @override 
  public object getitem(int position) { 
    return mdatas.get(position); 
  } 
 
  @override 
  public long getitemid(int position) { 
    return position; 
  } 
 
  @override 
  public view getview(int position, view convertview, viewgroup parent) { 
    viewholder holder = null; 
    chatitembean bean = mdatas.get(position); 
    if (convertview == null) { 
      holder = new viewholder(); 
      if (bean.gettype() == 1) { 
        convertview = minflater.inflate(r.layout.item_chat_left, null); 
        holder.mivavatar = (imageview) convertview.findviewbyid(r.id.iv_chatleftitem_avatar); 
        holder.mtvcontent = (textview) convertview.findviewbyid(r.id.tv_chatleftitem_content); 
      } else if (bean.gettype() == 2) { 
        convertview = minflater.inflate(r.layout.item_chat_right, null); 
        holder.mivavatar = (imageview) convertview.findviewbyid(r.id.iv_chatrightitem_avatar); 
        holder.mtvcontent = (textview) convertview.findviewbyid(r.id.tv_chatrightitem_content); 
      } 
      convertview.settag(holder); 
    } else { // 通过tag找到缓存的布局 
      holder = (viewholder) convertview.gettag(); 
    } 
    holder.mivavatar.setimagebitmap(bean.getavatar()); 
    holder.mtvcontent.settext(bean.getcontent()); 
     
    return convertview; 
  } 
   
  public final class viewholder { 
    public imageview mivavatar; 
    public textview mtvcontent; 
  } 

chatactivity.java

import java.util.arraylist; 
import java.util.list; 
 
import android.app.activity; 
import android.graphics.bitmapfactory; 
import android.os.bundle; 
import android.widget.listview; 
 
public class chatactivity extends activity { 
  private listview mlv; 
  private list<chatitembean> mdatas ; 
  private chatlvadapter madapter; 
   
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_chat); 
     
    mlv = (listview) findviewbyid(r.id.lv_content); 
 
    initdata(); 
    madapter = new chatlvadapter(this, mdatas); 
    mlv.setadapter(madapter); 
  } 
 
  private void initdata() { 
    mdatas = new arraylist<chatitembean>(); 
    chatitembean chat1 = new chatitembean(); 
    chat1.settype(1); 
    chat1.setcontent("早啊!"); 
    chat1.setavatar(bitmapfactory.decoderesource(getresources(), r.drawable.ic_item1)); 
    mdatas.add(chat1); 
    chatitembean chat2 = new chatitembean(); 
    chat2.settype(2); 
    chat2.setcontent("早!一大早找我有啥事?"); 
    chat2.setavatar(bitmapfactory.decoderesource(getresources(), r.drawable.ic_item2)); 
    mdatas.add(chat2); 
    chatitembean chat3 = new chatitembean(); 
    chat3.settype(1); 
    chat3.setcontent("没事就不能找你谈情说爱吗?也没什么事,看你有没有在撸代码。"); 
    chat3.setavatar(bitmapfactory.decoderesource(getresources(), r.drawable.ic_item1)); 
    mdatas.add(chat3); 
    chatitembean chat4 = new chatitembean(); 
    chat4.settype(2); 
    chat4.setcontent("算了吧,别找我!我害怕。"); 
    chat4.setavatar(bitmapfactory.decoderesource(getresources(), r.drawable.ic_item2)); 
    mdatas.add(chat4); 
    chatitembean chat5 = new chatitembean(); 
    chat5.settype(2); 
    chat5.setcontent("都被代码撸惨了,我哪敢一早起来找虐。"); 
    chat5.setavatar(bitmapfactory.decoderesource(getresources(), r.drawable.ic_item2)); 
    mdatas.add(chat5); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。