Android 在从风景到肖像的方向改变之后,RealdVIEW处于中间而不是顶部。

Android 在从风景到肖像的方向改变之后,RealdVIEW处于中间而不是顶部。,android,android-recyclerview,android-configchanges,Android,Android Recyclerview,Android Configchanges,我在片段中使用了recyclerview。如果Activity的配置包括定位,则在电话方向从风景到肖像改变后,在屏幕中间出现回收器视图。但是,它应该始终处于顶部。如果configChanges不包括方向,则它始终显示在方向更改的顶部 你知道原因是什么吗 这是活动布局 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

我在片段中使用了recyclerview。如果Activity的配置包括定位,则在电话方向从风景到肖像改变后,在屏幕中间出现回收器视图。但是,它应该始终处于顶部。如果configChanges不包括方向,则它始终显示在方向更改的顶部

你知道原因是什么吗

这是活动布局

 <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>
这是上面的片段布局替换框架布局

<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>
我找到了解决办法

基本上,您正在创建自己的行为类:

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;
    }
}
并更改框架布局中的app:layout\u行为:

<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" />
更新:正如我刚才检查的-此问题已在22.2.1库中修复,因此,如果您仍在使用22.2.0,请更新库,我找到了解决方案

基本上,您正在创建自己的行为类:

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;
    }
}
并更改框架布局中的app:layout\u行为:

<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" />

更新:正如我刚才检查的-此问题已在22.2.1库中修复,因此请更新库,如果您仍在使用22.2.0,您可以通过创建与中类似的纵向和横向布局来实现所需效果。如果这是您想要的行为,请查看附加的剪辑。

您可以通过创建肖像和横向布局来实现所需的效果,就像在中一样。如果这是您想要的行为,请查看附加的剪辑