android - 方向从横向变为纵向后,RecyclerView 位于中间而不是顶部

标签 android android-recyclerview android-configchanges

我在 fragment 中使用了 recyclerview。如果 Activity 的 configChanges 包含方向,则在手机方向从横向变为纵向后,回收器 View 会出现在屏幕中间。但是,它应该始终位于最前面。如果 configChanges 不包括方向,它总是出现在方向更改的顶部。

你知道这是什么原因吗?

这是 Activity 布局

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

这是 fragment 布局取代了上面的框架布局。

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

最佳答案

我找到了解决方案 here .

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

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;
    }
}

并在您的 FrameLayout 中更改 app:layout_behavior:

<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,请更新库

关于android - 方向从横向变为纵向后,RecyclerView 位于中间而不是顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355264/

相关文章:

android - 在 WebView 中从相机或图库上传图像

android - SearchView 搜索项目时无法保留选中的复选框

android - 应用程序因配置更改而崩溃

java - 当 layoutManager 的 scrollToPositionWithOffset 时项目的 recyclerView 之间空白

android - 有没有办法用我的 Json 数据设置可绘制文件夹中的图像,该数据包括可绘制文件夹中图像的名称

android - 在不调用 onCreate 的情况下更改布局

android - 为什么 onLayout 和 onSizeChanged 在方向改变时被调用两次?

android - GCM onRegistered 未调用

Android:接受拍照后相机不关闭

android - 在flutter dart中的some方法之后调用该方法