日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

聊天ListView使用ViewHolder

發布時間:2023/12/2 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聊天ListView使用ViewHolder 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????聊天界面會展示至少兩種布局,即收到消息和發送消息。這樣一個ListView與平時使用的ListView的最大不同,在于它有兩個不同的布局:收到的布局和發送的布局。需要利用Adapter實現這樣的效果。即需要在獲取布局的時候判斷該獲取哪個布局。

public abstract class

BaseAdapter

extends Object
implements ListAdapter SpinnerAdapter
java.lang.Object
????android.widget.BaseAdapter

BaseAdapter這個類中提供了兩個方法:

public intgetItemViewType (int position)

Added in API level 1

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

Parameters
position
The position of the item within the adapter's data set whose view type we want.
Returns? 返回第position個Item是何種類型
  • An integer representing the type of View. Two views should share the same type if one can be converted to the other ingetView(int, View, ViewGroup). Note: Integers must be in the range 0 togetViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

?

public intgetViewTypeCount ()

Added in API level 1

Returns the number of types of Views that will be created by getView(int, View, ViewGroup). Each type represents a set of views that can be converted ingetView(int, View, ViewGroup). If the adapter always returns the same type of View for all items, this method should return 1.

This method will only be called when when the adapter is set on the the AdapterView.

Returns 返回不同布局的數目
  • The number of types of Views that will be created by this adapter

首先需要實現兩個布局——發送和接收

chat_item_itemin.xml

?

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:gravity="center_vertical"android:padding="10dp"android:layout_height="match_parent"android:layout_width="match_parent"><ImageViewandroid:id ="@+id/icon_in"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic"/><TextViewandroid:id="@+id/text_in"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp"/>"</LinearLayout>


chat_item_itemout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical|right"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:id="@+id/text_out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp" /><ImageViewandroid:id="@+id/icon_out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" /></LinearLayout>


?

?

為了封裝聊天內容,便于在Adapter中獲取數據信息,封裝一個Bean類來保存聊天信息。編寫javabean就是編寫一個java的類,所以只要會寫類就能編寫一個bean,這個類創建的一個對象稱做一個bean。為了能讓使用這個bean的應用程序構建工具(比如JSP引擎)知道這個bean的屬性和方法,只需在類的方法命名上遵守以下規則:

1. 如果類的成員變量的名字是xxx,那么為了更改或獲取成員變量的值,即更改或獲取屬性,在類中可以使用兩個方法:

getXxx(),用來獲取屬性xxx。

setXxx(),用來修改屬性xxx。

2. 對于boolean類型的成員變量,即布爾邏輯類型的屬性,允許使用"is"代替上面的"get"和"set"。

3. 類中方法的訪問屬性都必須是public的。

4. 類中如果有構造方法,那么這個構造方法也是public的并且是無參數的。

?

ChatItemListViewBean.java

package sunny.example.ahfourlistviewchat;//封裝一個Bean來保存聊天信息 import android.graphics.Bitmap; public class ChatItemListViewBean {private int type;private String text;private Bitmap icon;public ChatItemListViewBean(){}public int getType(){return type;}//在使用時setType 如bean1.setType(0);public void setType(int type){this.type = type;}public String getText(){return text;}public void setText(String text){this.text = text;}public Bitmap getIcon(){return icon;}public void setIcon(Bitmap icon){this.icon = icon;} }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><ListViewandroid:id="@+id/listView_chat"android:layout_width="match_parent"android:layout_height="match_parent"android:divider="@null"android:listSelector="@android:color/transparent" /></LinearLayout>

?

ChatItemListViewTest.java

package sunny.example.ahfourlistviewchat;import android.support.v7.app.ActionBarActivity; import android.widget.ListView; import android.graphics.BitmapFactory; import android.os.Bundle; import java.util.List; import java.util.ArrayList; public class ChatItemListViewTest extends ActionBarActivity{private ListView mListView;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mListView = (ListView)findViewById(R.id.listView_chat);ChatItemListViewBean bean1 = new ChatItemListViewBean();bean1.setType(0);bean1.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));bean1.setText("Hello. How are you?");ChatItemListViewBean bean2 = new ChatItemListViewBean();bean2.setType(1);bean2.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));bean2.setText("Fine. Thank you");List<ChatItemListViewBean> data = new ArrayList<ChatItemListViewBean>();data.add(bean1);data.add(bean2);mListView.setAdapter(new ChatItemListViewAdapter(this,data));} }



?

總結

以上是生活随笔為你收集整理的聊天ListView使用ViewHolder的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。