From ada5a962d78146c226cc2dd08c17e9d711997813 Mon Sep 17 00:00:00 2001 From: imndx Date: Fri, 20 Dec 2024 11:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E5=88=97=E8=A1=A8=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=EF=BC=8C=E6=94=AF=E6=8C=81=E5=8F=8C=E5=87=BB=E5=BA=95?= =?UTF-8?q?=E9=83=A8=20item=20=E6=BB=91=E5=8A=A8=E5=88=B0=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9C=AA=E8=AF=BB=E4=BC=9A=E8=AF=9D=20fix=20?= =?UTF-8?q?#IBCGUU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wildfire/chat/app/main/MainActivity.java | 16 +++++++++++-- .../ConversationListAdapter.java | 10 ++++++++ .../ConversationListFragment.java | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java b/chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java index ab9381a0d..12adf0f4b 100644 --- a/chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java +++ b/chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java @@ -105,9 +105,11 @@ public class MainActivity extends WfcBaseActivity { private boolean isInitialized = false; private ContactListFragment contactListFragment; + private ConversationListFragment conversationListFragment; private ContactViewModel contactViewModel; private ConversationListViewModel conversationListViewModel; + private long lastSelectConversatonListItemTimestamp = 0; protected void bindViews() { super.bindViews(); @@ -331,7 +333,7 @@ private void initView() { //设置ViewPager的最大缓存页面 contentViewPager.setOffscreenPageLimit(4); - ConversationListFragment conversationListFragment = new ConversationListFragment(); + conversationListFragment = new ConversationListFragment(); contactListFragment = new ContactListFragment(); DiscoveryFragment discoveryFragment = new DiscoveryFragment(); MeFragment meFragment = new MeFragment(); @@ -347,7 +349,17 @@ private void initView() { contentViewPager.registerOnPageChangeCallback(this.onPageChangeCallback); bottomNavigationView.setOnItemReselectedListener(item -> { - // do nothing + switch (item.getItemId()) { + case R.id.conversation_list: + long now = System.currentTimeMillis(); + if(now - lastSelectConversatonListItemTimestamp < 200){ + conversationListFragment.scrollToNextUnreadConversation(); + } + lastSelectConversatonListItemTimestamp = now; + break; + default: + break; + } }); bottomNavigationView.setOnItemSelectedListener(item -> { switch (item.getItemId()) { diff --git a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListAdapter.java b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListAdapter.java index c2fc8fa7d..1544f6e57 100644 --- a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListAdapter.java +++ b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListAdapter.java @@ -63,6 +63,16 @@ public void setConversationInfos(List conversationInfos) { submit(this.statusNotifications, conversationInfos); } + public int getNextUnreadConversationPosition(int position) { + for (int i = position + 1; i < getItemCount(); i++) { + ConversationInfo conversationInfo = conversationInfos.get(i - headerCount()); + if (!conversationInfo.isSilent && (conversationInfo.unreadCount.unread + conversationInfo.unreadCount.unreadMention + conversationInfo.unreadCount.unreadMentionAll) > 0) { + return i; + } + } + return -1; + } + public void setOnClickConversationItemListener(OnClickConversationItemListener onClickConversationItemListener) { this.onClickConversationItemListener = onClickConversationItemListener; } diff --git a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListFragment.java b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListFragment.java index 73467f4ef..6475a4ed6 100644 --- a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListFragment.java +++ b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/ConversationListFragment.java @@ -12,6 +12,7 @@ import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.SimpleItemAnimator; @@ -49,6 +50,7 @@ public class ConversationListFragment extends ProgressFragment { private StatusNotificationViewModel statusNotificationViewModel; private LinearLayoutManager layoutManager; private OnClickConversationItemListener onClickConversationItemListener; + private RecyclerView.SmoothScroller smoothScroller; @Override protected int contentLayout() { @@ -90,6 +92,13 @@ private void init() { recyclerView.setAdapter(adapter); ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false); + smoothScroller = new LinearSmoothScroller(getContext()) { + @Override + protected int getVerticalSnapPreference() { + return LinearSmoothScroller.SNAP_TO_START; + } + }; + UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class); userViewModel.userInfoLiveData().observe(this, new Observer>() { @Override @@ -164,6 +173,20 @@ public void onChanged(Object o) { reloadConversations(); } + public void scrollToNextUnreadConversation() { + int start = layoutManager.findFirstVisibleItemPosition(); + int end = layoutManager.findLastVisibleItemPosition(); + int nextUnreadConversationPosition = adapter.getNextUnreadConversationPosition(start); + if (nextUnreadConversationPosition > 0) { + if (nextUnreadConversationPosition >= start && nextUnreadConversationPosition <= end) { + smoothScroller.setTargetPosition(nextUnreadConversationPosition); + layoutManager.startSmoothScroll(smoothScroller); + } else { + recyclerView.smoothScrollToPosition(nextUnreadConversationPosition); + } + } + } + private void loadAndShowPCOnlineNotification() { List pcOnlineInfos = ChatManager.Instance().getPCOnlineInfos(); statusNotificationViewModel.clearStatusNotificationByType(PCOnlineStatusNotification.class);