1
我正在聊天。收到併發送郵件時,我無法顯示郵件。只有當我滾動此表時,纔會顯示新消息。如果您使用scrollToPosition (0)
方法,則一切正常。但在這種情況下,頁面將滾動,並在收到對話者的消息時,這是不可接受的。我一直在處理這個問題大概一個星期,我無法修復它。我會很樂意提供任何幫助。我括的代碼:直到您開始滾動,Recycler視圖纔會更新
適配器初始化:
MessagesListAdapter<IMessage> adapter;
...
adapter = new MessagesListAdapter<IMessage>(user.getUser_id(), imageLoader);
WebSocketListener(用於消息recieving):
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);
}
}
ХML與郵件列表:
<?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
你能告訴我一個例子嗎?我不確定是否理解你)謝謝! –
你應該知道https://developer.android.com/training/multiple-threads/communicate-ui.html –
我有更多的小問題。我必須在處理程序中插入什麼?只有adapter.notifyDataSetChanged或onFrame方法的所有代碼?謝謝! –