Commit 1e2a248d authored by Yusuke Iwaki's avatar Yusuke Iwaki

call "readMessage" when the latest item is shown.

parent 3983b95f
......@@ -28,6 +28,7 @@ import chat.rocket.android.helper.LoadMoreScrollListener;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.OnBackPressListener;
import chat.rocket.android.helper.RecyclerViewAutoScrollManager;
import chat.rocket.android.helper.RecyclerViewScrolledToBottomListener;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.MessageFormManager;
import chat.rocket.android.layouthelper.chatroom.MessageListAdapter;
......@@ -157,6 +158,8 @@ public class RoomFragment extends AbstractChatRoomFragment
}
};
listView.addOnScrollListener(scrollListener);
listView.addOnScrollListener(
new RecyclerViewScrolledToBottomListener(layoutManager, 1, this::markAsReadIfNeeded));
setupSideMenu();
setupMessageComposer();
......
package chat.rocket.android.helper;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* ScrollListener for detecting scrolled on the bottom of the RecyclerView.
*/
public class RecyclerViewScrolledToBottomListener extends RecyclerView.OnScrollListener {
/**
* callback.
*/
public interface Callback {
void onScrolledToBottom();
}
private final LinearLayoutManager layoutManager;
private final int thresholdPosition;
private final Handler handler;
private final Callback callback;
/**
* Trigger callback if the bottom item position > thresholdPosition.
*/
public RecyclerViewScrolledToBottomListener(LinearLayoutManager layoutManager,
int thresholdPosition, Callback callback) {
this.layoutManager = layoutManager;
this.thresholdPosition = thresholdPosition;
this.callback = callback;
this.handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
onScrollEnd();
}
};
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
handler.removeMessages(0);
handler.sendEmptyMessageDelayed(0, 120);
}
private void onScrollEnd() {
if (layoutManager.getReverseLayout()) {
if (layoutManager.findFirstVisibleItemPosition() <= thresholdPosition) {
doCallback();
}
} else {
if (layoutManager.findLastVisibleItemPosition() >= thresholdPosition) {
doCallback();
}
}
}
private void doCallback() {
if (callback != null) {
callback.onScrolledToBottom();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment