From 65b16beb61546c0190834da3b742dcb5f917ad3c Mon Sep 17 00:00:00 2001
From: mvp76 <mikael.von.pfaler@gmail.com>
Date: Tue, 6 Dec 2016 09:44:46 +0100
Subject: [PATCH] Make sure right view is selected as anchor on
 "more"-expansion

---
 .../views/AppDetailsRecyclerViewAdapter.java    | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/app/src/main/java/org/fdroid/fdroid/views/AppDetailsRecyclerViewAdapter.java b/app/src/main/java/org/fdroid/fdroid/views/AppDetailsRecyclerViewAdapter.java
index addd0d61b..8825c733a 100644
--- a/app/src/main/java/org/fdroid/fdroid/views/AppDetailsRecyclerViewAdapter.java
+++ b/app/src/main/java/org/fdroid/fdroid/views/AppDetailsRecyclerViewAdapter.java
@@ -317,14 +317,11 @@ public class AppDetailsRecyclerViewAdapter
             descriptionMoreView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    // Remember current scroll position so that we can restore it
-                    LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
-                    int pos = lm.findFirstVisibleItemPosition();
-                    int posOffset = 0;
-                    if (pos != RecyclerView.NO_POSITION) {
-                        View firstChild = recyclerView.getChildAt(0);
-                        posOffset = (firstChild == null) ? 0 : (firstChild.getTop()); // - recyclerView.getPaddingTop());
-                    }
+                    // Make this "header section" the focused child, so that RecyclerView will use
+                    // it as the anchor in the layout process. Otherwise the RV might select another
+                    // view as the anchor, resulting in that the top of this view is instead scrolled
+                    // off the screen. Refer to LinearLayoutManager.updateAnchorFromChildren(...).
+                    recyclerView.requestChildFocus(itemView, itemView);
                     if (TextViewCompat.getMaxLines(descriptionView) != MAX_LINES) {
                         descriptionView.setMaxLines(MAX_LINES);
                         descriptionMoreView.setText(R.string.more);
@@ -332,10 +329,6 @@ public class AppDetailsRecyclerViewAdapter
                         descriptionView.setMaxLines(Integer.MAX_VALUE);
                         descriptionMoreView.setText(R.string.less);
                     }
-                    if (pos != RecyclerView.NO_POSITION) {
-                        // Restore scroll position
-                        lm.scrollToPositionWithOffset(pos, posOffset);
-                    }
                 }
             });
             // Set ALL caps (in a way compatible with SDK 10)