From c4cfa0becd840351ae1ca67f8e6eea20356ce53f Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 24 Dec 2022 11:08:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=9B=B4=E6=92=AD=E9=97=B4?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=AE=A4=E5=8A=A8=E7=94=BB=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunbao/live/adapter/LiveChatAdapter.java | 3 +- .../LiveChatRecyclerViewLayoutManager.java | 40 ++++++++++ .../yunbao/live/views/LiveRoomViewHolder.java | 76 +++++++++++++++---- live/src/main/res/anim/live_chat_msg_in.xml | 16 ++++ 4 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 live/src/main/java/com/yunbao/live/adapter/LiveChatRecyclerViewLayoutManager.java create mode 100644 live/src/main/res/anim/live_chat_msg_in.xml diff --git a/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java b/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java index f8f993610..27341f9d0 100644 --- a/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java +++ b/live/src/main/java/com/yunbao/live/adapter/LiveChatAdapter.java @@ -383,6 +383,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter { if (bean == null) { return; } + // Log.i(TAG, "insertItem: " + bean.getContent()); int size = mList.size(); //设置最大展示99条消息 if (size == 100) { @@ -390,7 +391,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter { notifyItemRangeRemoved(0, 50); } mList.add(bean); - if (getItemCount() == 1 ) { + if (getItemCount() == 1) { notifyDataSetChanged(); } else { notifyItemInserted(getItemCount()); diff --git a/live/src/main/java/com/yunbao/live/adapter/LiveChatRecyclerViewLayoutManager.java b/live/src/main/java/com/yunbao/live/adapter/LiveChatRecyclerViewLayoutManager.java new file mode 100644 index 000000000..fdd546e65 --- /dev/null +++ b/live/src/main/java/com/yunbao/live/adapter/LiveChatRecyclerViewLayoutManager.java @@ -0,0 +1,40 @@ +package com.yunbao.live.adapter; + +import android.content.Context; +import android.graphics.PointF; +import android.util.DisplayMetrics; + +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.LinearSmoothScroller; +import androidx.recyclerview.widget.RecyclerView; + +public class LiveChatRecyclerViewLayoutManager extends LinearLayoutManager { + private static final float MILLISECONDS_PER_INCH = 200f; + public LiveChatRecyclerViewLayoutManager(Context context) { + super(context); + } + + @Override + public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { + // super.smoothScrollToPosition(recyclerView, state, position); + LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) + { + + @Nullable + @Override + public PointF computeScrollVectorForPosition(int targetPosition) { + return LiveChatRecyclerViewLayoutManager.this.computeScrollVectorForPosition(targetPosition); + } + + @Override + protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { + // System.out.println("----->自定义滑动 = "+MILLISECONDS_PER_INCH+" > "+( MILLISECONDS_PER_INCH / displayMetrics.densityDpi)); + return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; + } + + }; + linearSmoothScroller.setTargetPosition(position); + startSmoothScroll(linearSmoothScroller); + } +} diff --git a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java index e7e26e215..6fa9eaa4a 100644 --- a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java @@ -16,7 +16,6 @@ import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewOutlineProvider; @@ -37,6 +36,7 @@ import androidx.annotation.Nullable; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.SimpleItemAnimator; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -116,6 +116,7 @@ import com.yunbao.live.activity.LiveAudienceActivity; import com.yunbao.live.activity.LiveRyAnchorActivity; import com.yunbao.live.activity.ZhuangBanActivity; import com.yunbao.live.adapter.LiveChatAdapter; +import com.yunbao.live.adapter.LiveChatRecyclerViewLayoutManager; import com.yunbao.live.adapter.LiveRoomFastMessageRecyclerViewAdapter; import com.yunbao.live.adapter.LiveUserAdapter; import com.yunbao.live.bean.DrPkbean; @@ -158,8 +159,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; @@ -868,11 +867,54 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis mChatRecyclerView.setLayoutParams(params1); mChatRecyclerView.setHasFixedSize(true); - LinearLayoutManager layoutManager = new LinearLayoutManager(mContext); + LinearLayoutManager layoutManager = new LiveChatRecyclerViewLayoutManager(mContext); layoutManager.setStackFromEnd(true); mChatRecyclerView.setLayoutManager(layoutManager); mChatRecyclerView.addItemDecoration(new TopGradual()); mChatRecyclerView.setItemViewCacheSize(10); + mChatRecyclerView.setItemAnimator(new SimpleItemAnimator() { + @Override + public boolean animateRemove(RecyclerView.ViewHolder holder) { + return false; + } + + @Override + public boolean animateAdd(RecyclerView.ViewHolder holder) { + Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.live_chat_msg_in); + holder.itemView.startAnimation(animation); + return false; + } + + @Override + public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY) { + return false; + } + + @Override + public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop) { + return false; + } + + @Override + public void runPendingAnimations() { + + } + + @Override + public void endAnimation(@NonNull RecyclerView.ViewHolder item) { + + } + + @Override + public void endAnimations() { + + } + + @Override + public boolean isRunning() { + return false; + } + }); mLiveChatAdapter = new LiveChatAdapter(mContext); mChatRecyclerView.setAdapter(mLiveChatAdapter); mLiveChatAdapter.setOnItemClickListener(new OnItemClickListener() { @@ -928,35 +970,39 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis mViewMedal.setOnClickListener(this); findViewById(R.id.btn_close).setOnClickListener(this); mAvatar.setOnClickListener(this); - //点击侧边消息按钮,自动发消息,测试用的 - /*msg.setOnClickListener(new View.OnClickListener() { - long index=0; + /*//点击侧边消息按钮,自动发消息,测试用的 + 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.setContent("test msg = " + index++); bean.setUserNiceName("auto"); - handler.post(()->mLiveChatAdapter.insertItem(bean)); + 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{ + if (task == null) { + task = createTask(); + new Timer().schedule(task, 0, 1000); + } else { task.cancel(); - task=null; + task = null; } } }); -*/ + userMore.setOnClickListener(v -> { + mLiveChatAdapter.scrollToBottom(); + });*/ findViewById(R.id.btn_votes).setOnClickListener(this); findViewById(R.id.btn_medal_rank).setOnClickListener(this); findViewById(R.id.btn_noble).setOnClickListener(this); diff --git a/live/src/main/res/anim/live_chat_msg_in.xml b/live/src/main/res/anim/live_chat_msg_in.xml new file mode 100644 index 000000000..8042381e2 --- /dev/null +++ b/live/src/main/res/anim/live_chat_msg_in.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file