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

混江龙づ霸主 提交于 2019-12-11 12:19:29

问题


I have used recyclerview in a fragment. If activity's configChanges includes orientation, recycler view appears in the middle of the screen after phone orientation changes from landscape to portrait. However it should be on top always. If configChanges does not include orientation, it always appear on top on orientation changes.

Do you have any idea what it is the reason?

This is the activity 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 is the fragment layout replaces framelayout 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:


I found solution here.

Basically you are creating 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 changing app: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 issue was fixed in 22.2.1 library, so please update library, if you are still using 22.2.0




回答2:


You may be able to achieve the desired effect by creating PORTRAIT and LANDSCAPE layout much like in the demo here. Take a look at attached clip if this is the behavior you desire.



来源:https://stackoverflow.com/questions/31355264/recyclerview-is-in-the-middle-instead-of-top-after-orientation-changes-from-land

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!