diff --git a/.idea/misc.xml b/.idea/misc.xml index 503aca7..33952c6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -55,7 +55,7 @@ - + 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 new file mode 100644 index 0000000..2a3aa46 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnScrollChangeDetectListener.java @@ -0,0 +1,19 @@ +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} + * + * @author Pierre HUBERT + * Created by pierre on 4/28/18. + */ + +public interface OnScrollChangeDetectListener { + + /** + * This method is triggered when the user reach the top (first item) of the list view + */ + void onReachTop(); + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollListView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollListView.java new file mode 100644 index 0000000..87d5abc --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ScrollListView.java @@ -0,0 +1,102 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.widget.AbsListView; + +import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; + +/** + * Custom ListView with extended functions + * + * This class use some concepts from maXp StackOverFlow answer + * on question "Detect Scroll Up & Scroll down in ListView" + * + * @author Pierre HUBERT + * Created by pierre on 4/28/18. + */ + +public class ScrollListView extends android.widget.ListView { + + /** + * Debug tag + */ + private static final String TAG = "ScrollListView"; + + /** + * Optional additional scroll listener + */ + private OnScrollListener onScrollListener; + + /** + * Scroll change detection listener + */ + private OnScrollChangeDetectListener onScrollChangeDetectListener; + + public ScrollListView(Context context) { + super(context); + init(); + } + + public ScrollListView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(); + } + + /** + * Initialize this view + */ + private void init(){ + + super.setOnScrollListener(new OnScrollListener() { + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + + //Call additional listener (if any) + if(onScrollListener != null) + onScrollListener.onScrollStateChanged(view, scrollState); + + } + + @Override + public void onScroll(AbsListView view, int firstVisibleItem, + int visibleItemCount, int totalItemCount) { + + //Call additional listener (if any) + if(onScrollListener != null) + onScrollListener.onScroll(view, firstVisibleItem, + visibleItemCount, totalItemCount); + + + if(onScrollChangeDetectListener != null && firstVisibleItem == 0 + && visibleItemCount > 0) + onScrollChangeDetectListener.onReachTop(); + } + }); + + } + + @Override + public void setOnScrollListener(OnScrollListener onScrollListener) { + this.onScrollListener = onScrollListener; + } + + public void setOnScrollChangeDetectListener(OnScrollChangeDetectListener onScrollChangeDetectListener) { + this.onScrollChangeDetectListener = onScrollChangeDetectListener; + } + + public OnScrollListener getOnScrollListener() { + return onScrollListener; + } +} diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml index e1e323f..1dc0435 100644 --- a/app/src/main/res/layout/fragment_conversation.xml +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -21,7 +21,7 @@ android:layout_marginTop="10dp"/> -