使用CollapsingToolbarLayout的CoordinatorLayout在Dialog片段中使用Keyboard打破

时间:2015-06-12 10:57:21

标签: android android-support-library android-dialogfragment android-keypad android-coordinatorlayout

我最近决定将我的应用程序移到使用新的支持设计库,最近发现了一个非常讨厌的错误。

假设我有一个CoordinatorLayout托管AppBarLayout和任何可滚动视图,无论是ViewPager,NestedScrollView,还是具有所需滚动行为的RecyclerView;选择显示一个显示键盘的对话框片段会导致AppBarLayout与滚动视图断开连接,并且它们不再滚动在一起。

此外,滚动视图被强制调整到底部屏幕的一半,而AppBar布局占据了上半部分,尽管它不需要它。

该错误的视频在这里: Youtube Link

编辑:

将Activity中键盘的softInputMode设置为" adjustPan"解决了这个问题。显然,CoordinatorLayout不喜欢通过键盘动态调整大小。

有问题的XML看起来像这样,但我有多个XML变体表现出这种行为,其中的共同元素都是他们使用的是CollapsingToolbarLayout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="@color/transparent">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="144dp"
        android:elevation="4dp"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="@dimen/quadruple_margin"
        app:layout_collapseParallaxMultiplier="0.7"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:background="@color/primary"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ranking_background" />

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_gravity="bottom"
                android:background="@color/black_40" />

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_picture"
                android:layout_width="@dimen/double_margin"
                android:layout_height="@dimen/double_margin"
                android:layout_alignBottom="@+id/text"
                android:layout_marginLeft="@dimen/double_margin"
                android:layout_marginStart="@dimen/double_margin"
                android:src="@drawable/image_placeholder"
                android:visibility="gone" />

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignWithParentIfMissing="true"
                android:layout_marginLeft="@dimen/single_margin"
                android:layout_marginStart="@dimen/single_margin"
                android:layout_marginTop="@dimen/quadruple_margin"
                android:layout_toRightOf="@+id/profile_picture"
                android:text="@string/my_places_for"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/sub_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/text"
                android:layout_marginLeft="@dimen/single_margin"
                android:layout_marginStart="@dimen/single_margin"
                android:layout_marginTop="@dimen/single_margin"
                android:text="@string/pick_category_or_business"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:visibility="gone" />
        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            app:contentInsetLeft="@dimen/triple_margin"
            app:contentInsetStart="@dimen/triple_margin"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/Theme.AppCompat.NoActionBar"
            app:theme="@style/Theme.AppCompat.NoActionBar" />
    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        app:layout_scrollFlags="scroll">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentBottom="true"
            android:background="@color/primary"
            android:elevation="4dp"
            app:layout_scrollFlags="enterAlways"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/grey_400" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

3 个答案:

答案 0 :(得分:9)

Tunji_D提供的答案将解决问题,但不一定是潜在的问题。

协调器布局上的滚动行为现在在其子视图的无效步骤中出现问题:https://code.google.com/p/android/issues/detail?id=176373#c5

因此,为了修复它,请使用junya提供的此解决方法,并使用

设置视图的行为
app:layout_behavior="com.app.common.PatchedScrollingViewBehavior"

并使用:

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

答案 1 :(得分:6)

转到Android清单并将“窗口软输入模式”更改为:

android:windowSoftInputMode="adjustPan|adjustNothing"

答案 2 :(得分:0)

Try updating to the latest the com.android.support:appcompat - Worked for me.