From 236b6b66f1bc60bdcec1322367f0239174e2d802 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Jul 2021 21:04:02 +0800 Subject: Scroll to bottom if new comments arrive in ViewRPANBroadcastFragment. --- .../fragments/ViewRPANBroadcastFragment.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java index 4eed627c..5fa26888 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java @@ -16,6 +16,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.exoplayer2.ExoPlaybackException; @@ -267,7 +268,21 @@ public class ViewRPANBroadcastFragment extends Fragment { payload.getString(JSONUtils.BODY_KEY), payload.getLong(JSONUtils.CREATED_UTC_KEY)); - handler.post(() -> adapter.addRPANComment(rpanComment)); + handler.post(() -> { + LinearLayoutManager manager = ((LinearLayoutManager) recyclerView.getLayoutManager()); + boolean shouldScrollToBottom = false; + if (manager != null) { + int lastPosition = manager.findLastCompletelyVisibleItemPosition(); + int currentItemCount = adapter.getItemCount(); + if (currentItemCount > 0 && lastPosition == currentItemCount - 1) { + shouldScrollToBottom = true; + } + } + adapter.addRPANComment(rpanComment); + if (shouldScrollToBottom) { + recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1); + } + }); } } catch (JSONException e) { e.printStackTrace(); -- cgit v1.2.3