RecyclerView is in the middle instead of the top after changing orientation from landscape to portrait

I used recyclerview in fragment. If configChanges activity includes orientation, viewing the recycler appears in the middle of the screen after changing the orientation of the phone from landscape to portrait. However, it should always be on top. If configChanges does not include orientation, it is always displayed at the top when orientation changes.

Do you have any idea what is the reason?

This is an action layout.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.butterfly.LoginActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <FrameLayout app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/butterflypi_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top|start" /> </android.support.design.widget.CoordinatorLayout> 

This fragment layout replaces the frame above.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" xmlns:card_view="http://schemas.android.com/apk/res-auto" tools:context="com.butterfly.fragment.ButterflyPIsFragment"> <android.support.v7.widget.RecyclerView android:id="@+id/my_pi_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" /> </RelativeLayout> 
+1
android android-recyclerview
source share
2 answers

I found a solution here .

You basically create your own behavior class:

 public class PatchedScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior { public PatchedScrollingViewBehavior() { super(); } public PatchedScrollingViewBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) { if (child.getLayoutParams().height == -1) { List dependencies = parent.getDependencies(child); if (dependencies.isEmpty()) { return false; } AppBarLayout appBar = findFirstAppBarLayout(dependencies); if (appBar != null && ViewCompat.isLaidOut(appBar)) { if (ViewCompat.getFitsSystemWindows(appBar)) { ViewCompat.setFitsSystemWindows(child, true); } int scrollRange = appBar.getTotalScrollRange(); // int height = parent.getHeight() - appBar.getMeasuredHeight() + scrollRange; int parentHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec); int height = parentHeight - appBar.getMeasuredHeight() + scrollRange; int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST); parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed); return true; } } return false; } private static AppBarLayout findFirstAppBarLayout(List<View> views) { int i = 0; for (int z = views.size(); i < z; ++i) { View view = (View) views.get(i); if (view instanceof AppBarLayout) { return (AppBarLayout) view; } } return null; } } 

and application change: layout_behavior in your FrameLayout:

 <FrameLayout app:layout_behavior="your_package.PatchedScrollingViewBehavior" android:id="@+id/butterflypi_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top|start" /> 

UPDATE: As I just checked - this problem has been fixed in library 22.2.1, so please update the library if you are still using 22.2.0

+4
source share

Perhaps you can achieve the desired effect by creating a PORTRAIT and LANDSCAPE layout, as in here . Take a look at the attached clip if this is the behavior you desire.

0
source share

All Articles