From 2a202f1308f2cbc2101fc538f9ecf27acbab4d52 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 16 Aug 2018 09:55:43 +0200 Subject: [PATCH] Can get older conversation message --- .../adapters/ConversationMessageAdapter.java | 2 +- .../ui/fragments/ConversationFragment.java | 5 +- .../OnScrollChangeDetectListener.java | 1 + .../client/ui/views/ScrollRecyclerView.java | 71 +++++++++++++++++++ .../main/res/layout/fragment_conversation.xml | 2 +- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollRecyclerView.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java index ea11eb9..4f744bd 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java @@ -182,7 +182,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { mUserName.setText(info.getDisplayFullName()); } - if(pos < 2) + if(pos < 1) setUserInfoVisibility(true); else setUserInfoVisibility( diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index b3a6f35..84ca59b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -38,6 +38,7 @@ import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAd import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; import org.communiquons.android.comunic.client.ui.utils.UiUtils; +import org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView; import java.io.FileNotFoundException; import java.util.ArrayList; @@ -110,7 +111,7 @@ public class ConversationFragment extends Fragment /** * Conversation message listView */ - private RecyclerView convMessRecyclerView; + private ScrollRecyclerView convMessRecyclerView; /** * Conversation messages layout manager @@ -291,7 +292,7 @@ public class ConversationFragment extends Fragment //Set a listener to detect when the user reaches the top of the conversation - //TODO : add reach top listener + convMessRecyclerView.setOnScrollChangeDetectListener(this); } @Override diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnScrollChangeDetectListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnScrollChangeDetectListener.java index d3d1a79..ec10c02 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnScrollChangeDetectListener.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnScrollChangeDetectListener.java @@ -4,6 +4,7 @@ package org.communiquons.android.comunic.client.ui.listeners; * On Scroll change listener * * This listener works with {@link org.communiquons.android.comunic.client.ui.views.ScrollListView} + * and {@link org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView} * * @author Pierre HUBERT * Created by pierre on 4/28/18. diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollRecyclerView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollRecyclerView.java new file mode 100644 index 0000000..410aa16 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollRecyclerView.java @@ -0,0 +1,71 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.util.AttributeSet; +import android.widget.AbsListView; + +import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; + +/** + * A view that extends RecyclerView in order to add a listener to detect when + * the user scrolll the view + * + * @author Pierre HUBERT + */ +public class ScrollRecyclerView extends RecyclerView { + + private OnScrollChangeDetectListener mOnScrollChangeDetectListener = null; + + public ScrollRecyclerView(@NonNull Context context) { + super(context); + init(); + } + + public ScrollRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + init(); + } + + public ScrollRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + public void setOnScrollChangeDetectListener(OnScrollChangeDetectListener onScrollChangeDetectListener) { + this.mOnScrollChangeDetectListener = onScrollChangeDetectListener; + } + + public OnScrollChangeDetectListener getOnScrollChangeDetectListener() { + return mOnScrollChangeDetectListener; + } + + /** + * Initialize the view + */ + private void init(){ + addOnScrollListener(new PrivateScrollListener()); + } + + /** + * Private class to handle scroll events + */ + private class PrivateScrollListener extends OnScrollListener { + + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + + if(mOnScrollChangeDetectListener == null) + return; + + if(!canScrollVertically(1)) + mOnScrollChangeDetectListener.onReachBottom(); + + else if(!canScrollVertically(-1)) + mOnScrollChangeDetectListener.onReachTop(); + } + } +} diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml index cd4bb01..c2274ff 100644 --- a/app/src/main/res/layout/fragment_conversation.xml +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -27,7 +27,7 @@ android:layout_height="match_parent"> -