所謂朋友,不過是我在路上走著,遇到了你,大家點頭微笑,結伴一程。緣深緣淺,緣聚緣散,該分手時分手,該重逢時重逢。惜緣而已,不比攀緣。同路人而已,能不遠不近的彼此陪伴著,不是已經很好了么? <p>——大冰《阿彌陀佛么么噠》
一段很喜歡的文字~~
好!步入正題,這一篇文章主要是記錄一下自己使用鴻洋哥baseAdapter(https://github.com/hongyangAndroid/baseAdapter )使用筆記,做了一些小的更改,用來適配自己的小項目!
小酋踏入行業不久,歡迎大神吐槽~~,小酋跪謝了
<p>主要效果:
- 模仿微信、qq聊天列表的效果
- 包含倒序、實時的發送消息底部插入
- 撤回消息同時更改布局
- 滾動到底部
實現上述效果主要用于設置RecyclerView.LayoutManager
RecyclerView.LayoutManager
ALayoutManageris responsible for measuring and positioning item views within aRecyclerViewas well as determining the policy for when to recycle item views that are no longer visible to the user. By changing theLayoutManageraRecyclerViewcan be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use
翻譯:LayoutManager負責測量和定位RecyclerView中的項目視圖,以及確定何時回收用戶不再可見的項目視圖的策略。通過更改LayoutManager,RecyclerView可用于實現標準的垂直滾動列表,統一網格,交錯網格,水平滾動視圖等。提供了一些庫存布局管理器用于一般使用。
<p>常規設置LayoutManager:
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
llm.setReverseLayout(true);//是數據從底部堆棧(倒序插入數據)
llm.setStackFromEnd(true);//視圖滾動到底部
llm.scrollToPosition(0);
converRecylist.setLayoutManager(llm);
同學可以直接下載鴻洋哥的baseAdapter,封裝的不同type的類型布局設置adapter為ChatAdapterForRv類,我將其更改為:
public class ChatAdapterForRv extends MultiItemTypeAdapter<Message>
{
public static final int CHAT_ITEM_SEND=0;
public static final int CHAT_ITEM_FROM=1;
public static final int REMOVE_ITEM=4;
public ChatAdapterForRv(Context context, List<Message> datas,TransferImage transferImage)
{
super(context, datas);
addItemViewDelegate(CHAT_ITEM_SEND,new MsgSendItemDelagate(context));//用戶自己發送的文本布局(右側顯示)
addItemViewDelegate(CHAT_ITEM_FROM,new MsgComingItemDelagate(context));//收到的消息文本布局(左側顯示)
addItemViewDelegate(REMOVE_ITEM,new MsgRemoveItemDelagate(context));(//撤回的消息布局)
}
}
將MsgSendItemDelagate進行修改,返回的isForViewType()方法進行修改,直接返回類型代表值,以MsgSendItemDelagate類為例,其他的delagate的isForViewType()一個樣
@Override
public int isForViewType(Message item, int position)
{
if (item.getContent() instanceof TextMessage){
//101是我自己的user_id,用來判斷是不是用戶本人發送的消息,true 0,false 1
if (item.getSenderUserId().equals("101")){
return 0;
}else {
return 1;
}
}
return 4;//是撤回消息的布局類型
}
最后再次修改類ItemViewDelegateManager ,將getItemViewType(T item, int position)方法改為:
public int getItemViewType(T item, int position)
{
int delegatesCount = delegates.size();
for (int i = delegatesCount - 1; i >= 0; i--)
{
//主要修改的位置
ItemViewDelegate<T> delegate = delegates.valueAt(i);
if (delegate.isForViewType(item,position)==delegates.keyAt(i))
return delegates.keyAt(i);
//------------
}
throw new IllegalArgumentException(
"No ItemViewDelegate added that matches position=" + position + " in data source");
}
到此位置就可以修改不同的布局了,根據返回的ViewType,還是建議少年看看鴻洋哥的baseadapter就可以看得懂了,封裝的很容易理解。
<p>當用戶發送一條消息后,列表會自動滾到向上滾動到最新消息處,同時列表插入數據:
插入數據代碼:
//接受文本消息
if (!ParamsCheckUtils.isEmpty(presenter))
datas.add(0, message);
adapter.notifyDataSetChanged();
代碼可知:數據直接添加到position為0處即可,然后刷新數據adapter.
<p>設置列表滾動到最底部:
/**
* 設置輸入完直接滾動到底部
*/
private void scrollToBottom() {
converRecylist.requestLayout();
converRecylist.post(new Runnable() {
@Override
public void run() {
converRecylist.scrollToPosition(0);
}
});
}
<p>項目用的資源:
baseAdapter (https://github.com/hongyangAndroid/baseAdapter )
chat聊天的鍵盤輸入以及處理表情(開源表情鍵盤解決方案):
(https://github.com/w446108264/XhsEmoticonsKeyboard )
好!整體內容比較簡單,也是我第一次寫文章,還有很多很多需要學習的東西,我是一個喜歡民謠的少年碼農,也希望前輩們能夠提意見~~小酋不勝感激呀!