修复聊天室消息大于99消息会混乱的问题

This commit is contained in:
zlzw 2022-12-23 14:57:59 +08:00
parent 1bde99f6c9
commit 99938f6ed6
2 changed files with 46 additions and 9 deletions

View File

@ -47,6 +47,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Created by cxf on 2018/10/10. * Created by cxf on 2018/10/10.
@ -127,7 +128,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy); super.onScrolled(recyclerView, dx, dy);
if (isSlideToBottom(recyclerView)) return; if (isSlideToBottom(recyclerView) || dy == 0) return;
mRecyclerViewScrolledDy = dy; mRecyclerViewScrolledDy = dy;
} }
}); });
@ -379,16 +380,22 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
public void insertItem(LiveChatBean bean) { public void insertItem(LiveChatBean bean) {
if (bean == null) { if (bean == null) {
return; return;
} }
int size = mList.size(); int size = mList.size();
//设置最大展示99条消息 //设置最大展示99条消息
if (size == 100) { if (size == 100) {
mList.remove(0); mList.subList(0, 50).clear();
notifyItemRangeRemoved(0, 50);
} }
mList.add(bean); mList.add(bean);
if (getItemCount() == 1 ) {
notifyDataSetChanged();
} else {
notifyItemInserted(getItemCount());
}
size = mList.size();
if (isSlideToBottom(mRecyclerView) || mRecyclerViewScrolledDy == 0) { if (isSlideToBottom(mRecyclerView) || mRecyclerViewScrolledDy == 0) {
scrollToBottom(); scrollToBottom();
} else { } else {
@ -401,11 +408,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
mPosition = size; mPosition = size;
} }
} }
if (getItemCount() == 1) {
notifyDataSetChanged();
} else {
notifyItemInserted(getItemCount());
}
} }
@ -447,6 +450,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
} }
public void scrollToBottom() { public void scrollToBottom() {
mRecyclerViewScrolledDy = 0;
if (mList.size() > 0) { if (mList.size() > 0) {
mRecyclerView.smoothScrollToPosition(getItemCount() - 1); mRecyclerView.smoothScrollToPosition(getItemCount() - 1);
} }

View File

@ -16,6 +16,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewOutlineProvider; import android.view.ViewOutlineProvider;
@ -157,6 +158,8 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -743,7 +746,7 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
msgNumber = (TextView) findViewById(R.id.room_msg_num); msgNumber = (TextView) findViewById(R.id.room_msg_num);
msgUserIcon = (RoundedImageView) findViewById(R.id.room_msg_user_icon); msgUserIcon = (RoundedImageView) findViewById(R.id.room_msg_user_icon);
msgLayout = (ConstraintLayout) findViewById(R.id.room_msg_layout); msgLayout = (ConstraintLayout) findViewById(R.id.room_msg_layout);
ViewClicksAntiShake.clicksAntiShake(msg, this::openChatWindow); // ViewClicksAntiShake.clicksAntiShake(msg, this::openChatWindow);
ViewClicksAntiShake.clicksAntiShake(msg2, this::openChatWindow); ViewClicksAntiShake.clicksAntiShake(msg2, this::openChatWindow);
lt_trickery = (LinearLayout) findViewById(R.id.lt_trickery); lt_trickery = (LinearLayout) findViewById(R.id.lt_trickery);
@ -925,6 +928,35 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
mViewMedal.setOnClickListener(this); mViewMedal.setOnClickListener(this);
findViewById(R.id.btn_close).setOnClickListener(this); findViewById(R.id.btn_close).setOnClickListener(this);
mAvatar.setOnClickListener(this); mAvatar.setOnClickListener(this);
//点击侧边消息按钮自动发消息测试用的
/*msg.setOnClickListener(new View.OnClickListener() {
long index=0;
public TimerTask createTask() {
return new TimerTask() {
@Override
public void run() {
LiveChatBean bean = new LiveChatBean();
bean.setContent("test msg = "+index++);
bean.setUserNiceName("auto");
handler.post(()->mLiveChatAdapter.insertItem(bean));
}
};
}
Handler handler = new Handler(Looper.getMainLooper());
private TimerTask task;
@Override
public void onClick(View v) {
if(task==null){
task=createTask();
new Timer().schedule(task, 0, 300);
}else{
task.cancel();
task=null;
}
}
});
*/
findViewById(R.id.btn_votes).setOnClickListener(this); findViewById(R.id.btn_votes).setOnClickListener(this);
findViewById(R.id.btn_medal_rank).setOnClickListener(this); findViewById(R.id.btn_medal_rank).setOnClickListener(this);
findViewById(R.id.btn_noble).setOnClickListener(this); findViewById(R.id.btn_noble).setOnClickListener(this);
@ -3718,6 +3750,7 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
* 展示新消息提示 * 展示新消息提示
*/ */
public void showNewMessage() { public void showNewMessage() {
if (newMessage.getVisibility() == View.VISIBLE) return;
Locale locale = mContext.getResources().getConfiguration().locale; Locale locale = mContext.getResources().getConfiguration().locale;
String language = locale.getLanguage(); String language = locale.getLanguage();
ImgLoader.display(mContext, TextUtils.equals(language, "zh") ? R.mipmap.new_message_cn : R.mipmap.new_message_en, newMessage); ImgLoader.display(mContext, TextUtils.equals(language, "zh") ? R.mipmap.new_message_cn : R.mipmap.new_message_en, newMessage);