我正在进行聊天功能的开发,但是我遇到了一个问题:当我发送或接收消息时,消息并不会立即出现,只有当我滚动页面时才会显示新消息。如果使用
Adapter 初始化:
我使用这个库ChatKit for Android。
scrollToPosition(0)
方法,一切都能正常工作。但是在这种情况下,每当收到对话者的消息时,页面都会滚动,这是无法接受的。我已经尝试解决这个问题差不多一周了,但是还没有找到解决方法。希望能得到任何帮助。以下是我的代码:Adapter 初始化:
MessagesListAdapter<IMessage> adapter;
...
adapter = new MessagesListAdapter<IMessage>(user.getUser_id(), imageLoader);
WebSocket监听器(用于接收消息):
webSocket.addListener(new WebSocketListener() {
...
@Override
public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.i("fg", "onFrame");
JSONObject data = new JSONObject(frame.getPayloadText());
Log.i("fg", "data: " + data.toString());
if (data.has("data")) {
switch (data.getString("type")) {
case "Text":
Log.i("fg", "type text");
MyMessage myMessage = new MyMessage(data.getString("user_id"), data.getString("data"),
data.getString("login"), data.getString("unix_time"), data.getString("user_id"), data.getString("avatar"));
Log.i("fg", "before " + Integer.toString(adapter.getItemCount()));
adapter.addToStart(myMessage, true);
adapter.notifyDataSetChanged();
break;
case "Location":
Log.i("fg", "type location");
MyLocationMessage myLocationMessage = new MyLocationMessage(data.getString("user_id"), data.getString("data"),
data.getString("login"), data.getString("unix_time"), data.getString("user_id"), data.getString("avatar"));
Log.i("fg", "before " + Integer.toString(adapter.getItemCount()));
adapter.addToStart(myLocationMessage, true);
adapter.notifyDataSetChanged();
break;
case "Photo":
Log.i("fg", "was here images");
MyImageMessage myImageMessage = new MyImageMessage(data.getString("user_id"), data.getString("data"),
data.getString("login"), data.getString("unix_time"), data.getString("user_id"), data.getString("avatar"));
Log.i("fg", "before " + Integer.toString(adapter.getItemCount()));
adapter.addToStart(myImageMessage, true);
adapter.notifyDataSetChanged();
break;
}
}
adapter.notifyDataSetChanged();
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}
适配器:
public MessagesListAdapter(String senderId, ImageLoader imageLoader) {
this(senderId, new MessageHolders(), imageLoader);
}
/**
* For default list item layout and view holder.
*
* @param senderId identifier of sender.
* @param holders custom layouts and view holders. See {@link MessageHolders} documentation for details
* @param imageLoader image loading method.
*/
public MessagesListAdapter(String senderId, MessageHolders holders,
ImageLoader imageLoader) {
this.senderId = senderId;
this.holders = holders;
this.imageLoader = imageLoader;
this.items = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return holders.getHolder(parent, viewType, messagesListStyle);
}
@SuppressWarnings("unchecked")
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Log.i("fg","onBindViewHolder position: " + Integer.toString(position));
Wrapper wrapper = items.get(position);
holders.bind(holder, wrapper.item, wrapper.isSelected, imageLoader,
getMessageClickListener(wrapper),
getMessageLongClickListener(wrapper),
dateHeadersFormatter);
}
public int getMessagesCount(){
int count = 0;
for(int i = 0; i < items.size(); i++){
Wrapper wrapper = items.get(i);
if (wrapper.item instanceof IMessage) {
count++;
}
}
return count;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position) {
// Log.i("fg", "position: " + Integer.toString(position));
return holders.getViewType(items.get(position).item, senderId);
}
public void addToStart(MESSAGE message, boolean scroll) {
boolean isNewMessageToday = !isPreviousSameDate(0, message.getCreatedAt());
if (isNewMessageToday) {
items.add(0, new Wrapper<>(message.getCreatedAt()));
}
Wrapper<MESSAGE> element = new Wrapper<>(message);
items.add(0, element);
notifyItemRangeInserted(0, isNewMessageToday ? 2 : 1);
if (layoutManager != null && scroll) {
layoutManager.scrollToPosition(0);
}
}
带消息列表的HTML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.uncolor.aroundme.Dialog">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/buttonLoadMore"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_gravity="center|top"
android:background="@color/transparent"
android:gravity="center"
android:text="@string/previous_messages"
android:textAllCaps="false"
android:textColor="@color/colorPrevMsgs"
android:textSize="18sp" />
<com.stfalcon.chatkit.messages.MessagesList
android:id="@+id/messagesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:paddingTop="30dp"
android:paddingBottom="15dp"
android:clipToPadding="false"
android:layout_marginBottom="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent">
</com.stfalcon.chatkit.messages.MessagesList>
</FrameLayout>
我使用这个库ChatKit for Android。