Android 折叠工具栏布局在EditText聚焦时不折叠

Android 折叠工具栏布局在EditText聚焦时不折叠,android,androiddesignsupport,android-coordinatorlayout,android-collapsingtoolbarlayout,Android,Androiddesignsupport,Android Coordinatorlayout,Android Collapsingtoolbarlayout,我正在使用collasingtoolbar内部CoordinatorLayout并且我已经将NestScrollView包含一些EditText作为子视图。我面临的是当edittext聚焦,键盘出现时,我的完整视图不会向上滚动 下面是作为该布局输出的代码和图像 <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent"

我正在使用
collasingtoolbar
内部
CoordinatorLayout
并且我已经将
NestScrollView
包含一些
EditText
作为子视图。我面临的是当edittext聚焦,键盘出现时,我的完整视图不会向上滚动

下面是作为该布局输出的代码和图像

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:id="@+id/ivProductImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/stub_image"
            app:layout_collapseParallaxMultiplier="0.7"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/containerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp">

            <EditText
                android:id="@+id/etProductTitle"
                style="@style/EditTextStyle"
                android:hint="@string/lbl_product_title"
                android:inputType="text"
                android:textSize="24dp" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

            <EditText
                android:id="@+id/etProductDescription"
                style="@style/EditTextStyle"
                android:hint="@string/lbl_description"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.design.widget.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="0.5">

                <EditText
                    android:id="@+id/etDeposit"
                    style="@style/EditTextStyle"
                    android:hint="@string/lbl_deposit"
                    android:inputType="number" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="0.5">

                <EditText
                    android:id="@+id/etPricePerDay"
                    style="@style/EditTextStyle"
                    android:hint="@string/lbl_price_per_day"
                    android:inputType="number" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

            <EditText
                android:id="@+id/etLocation"
                style="@style/EditTextStyle"
                android:hint="@string/lbl_add_location"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>

        <TextView
            style="@style/SubHeading"
            android:layout_margin="10dp"
            android:text="@string/lbl_categories"
            android:textColor="@color/color_primary" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="315dp"
            android:layout_marginLeft="10dp"
            android:isScrollContainer="false"
            android:layout_marginRight="10dp"
            android:scrollbars="none" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>


RecyclerView
是一个可滚动的小部件,这意味着这些可滚动的小部件彼此不工作。(但是,是的子部件)

检查此问题以了解更多说明:

但是,您可以将其与自定义
LinearLayoutManager一起使用


顺便说一下

您可以在
CoordinatorLayout
内部和
NestedScrollView
外部使用该
RecyclerView


希望这能有所帮助。

我已经解决了这个问题,我将
onfocuschangeelistener
添加到
EditText
中,如果它有焦点,只需使用
setExpanded
方法折叠视图即可:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(final View v, boolean hasFocus) {
        if (hasFocus) {
           mAppBarLayout.setExpanded(false, true);
        }
    }
});

如果你想让你的
折叠工具栏布局
在你的布局中的其他
编辑文本
聚焦时被折叠,那么你应该为它们每个设置相同的
聚焦更改侦听器

我认为经过所有研究,这是最好的解决方案

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, boolean hasFocus) {
    if (hasFocus) {
        mAppBarLayout.setExpanded(false, true); // second one for animation
    }
}
});

您好,我也遇到了同样的问题,您解决了吗?我在
NestedScrollView
中使用了
RecyclerView
,通过删除RecyclerView解决了我的问题。这似乎是最好的解决方案,但使用
mAppBarLayout.setExpanded(false)折叠
CollingToolbarLayout
应该更容易些
。我没有使用
setExpanded
方法的原因是该方法仅出现在v23版本的design library中,但我之前已经实现了它。