当从具有点击事件的视图滚动时,NestedScrollView中的滚动不起作用。

33

我在布局中使用了NestedScrollView,并且尝试使用来自设计支持库的新CoordinatorLayout来实现CollapsingToolbarLayout。

我的布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        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="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"

            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <!--            app:expandedTitleMarginEnd="64dp"-->
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@drawable/image_load_default_big" />

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollVw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="false"
            android:fitsSystemWindows="true">

            <LinearLayout
                android:id="@+id/changePasswordButtonContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:id="@+id/changePasswordExpand"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@drawable/back_img"
                    android:text="Change Your Password"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>


            <LinearLayout
                android:id="@+id/changePasswordContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/changePasswordButtonContainer"
                android:layout_centerInParent="true"
                android:orientation="vertical"
                android:padding="10dp">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Edit Your Password"
                    android:textColor="@color/orange" />

                <EditText
                    android:id="@+id/etUserName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/edittext_default_bg"
                    android:drawableLeft="@drawable/password_icon"
                    android:drawableRight="@drawable/tick"
                    android:hint=" Old Password"
                    android:padding="12dp"
                    android:password="true"
                    android:textColorHint="#b5b5b5" />

                <EditText
                    android:id="@+id/etPass"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/edittext_default_bg"
                    android:drawableLeft="@drawable/password_icon"
                    android:drawableRight="@drawable/cross"
                    android:hint=" New Password"
                    android:padding="12dp"
                    android:password="true"
                    android:textColorHint="#b5b5b5" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingBottom="20dp"
                    android:paddingTop="20dp"
                    android:text="show password" />

                <Button
                    android:id="@+id/btnSingIn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:background="@drawable/login_button_background"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:text="Done"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/dealerToDealerContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/changePasswordContainer"
                android:orientation="vertical">

                <Button
                    android:id="@+id/dealerToDealerExpand"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="@drawable/back_img"
                    android:text="Dealer To Dealer Platform No"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/dealerToDealerContainer"
                android:layout_centerInParent="true"
                android:orientation="vertical"
                android:padding="10dp">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Edit Number"
                    android:textColor="@color/orange" />

                <EditText
                    android:id="@+id/dealerToDealerNo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/edittext_default_bg"
                    android:drawableLeft="@drawable/password_icon"
                    android:drawableRight="@drawable/tick"
                    android:hint=" 56546789"
                    android:padding="12dp"
                    android:password="true"
                    android:textColorHint="#b5b5b5" />

                <Button
                    android:id="@+id/dealerToDealerNoDone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:background="@drawable/login_button_background"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:text="Done"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

有时候我尝试滚动页面,但是它不起作用。 造成此问题的原因是,布局中其他具有点击事件的元素正在消耗触摸事件。 基本上,EditText、RadioButton和Button都会消耗触摸事件。 有什么建议可以解决这个问题吗?


你解决了在可点击视图内滚动的问题吗?你使用了fill_vertical标记,但对我来说它只是填充了空白区域。它仍然不能让可点击的视图滚动。 - Davideas
22.2.1版本似乎已经解决了,您能确认一下吗? - Davideas
@Davidea 对我没用,你那边可以用吗? - Arun
这个问题解决了吗?我也遇到了同样的问题。 - goldsmit409
2
我也有这个问题,有趣的是,我在ViewPager中遇到了它..但只有一个Fragment受到影响,另一个Fragment可以正确地处理从touchables开始的滚动事件... - Graeme
显示剩余3条评论
4个回答

22

我有同样的问题。当NestedScrollView的内容高度小于设备屏幕的高度时才会出现这种情况。因此解决方法是对你的NestedScrollView中的视图使用setMinimumHeight(..)方法,使其调整为屏幕高度:

DisplayMetrics displaymetrics = new DisplayMetrics();
getBaseActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;

int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getBaseActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

view.setMinimumHeight(screenHeight - actionBarHeight);

其中view是您的RelativeLayout

对我来说运行良好。希望能对您有所帮助。


1
将视图的最小高度设置为screenHeight - actionBarHeght并没有解决我的问题。我尝试将其设置得更大一些(例如:screenHeight - actionBarHeight + 5),这样就可以了。有什么想法吗? - Sandra
这还有必要吗?这似乎是 Android / Material Design 的基础。怎么可能被忽视了呢? - Luke Allison
太好了!运行得非常顺畅。但我想知道,作为Material设计中如@LukeAllison所正确提到的重要组件,它怎么可能被忽视了呢! - sud007
我思考了一下,意识到在scrollView中放置另一个scrollView是不被支持的,因为这不是好的实践。它会使父级scrollView变得无用。相反,我选择通过编程方式调整视图大小以适应更新的内容,从而增加了父级scrollView的高度。然而,这也不容易,需要使用hacky代码! - Luke Allison

2

在您的AndroidManifest.xml中,请确保将'windowSoftInputMode'属性设置为'adjustResize'。

<activity android:name=".activities.YourActivity"  android:windowSoftInputMode="adjustResize">

2

你的一个ScrollView会将所有事件发送给第一个在dispatchMotionEvent上返回true的View。

你可以避免在你的应用中使用这种情况,或者覆盖所有dispatchMotionEvent方法(从滚动和视图中)以不消耗ACTION_DOWN。


你能否请再详细解释一下你的答案,因为我是初学者 :) - aya salama

1
这里的解决方案(通过覆盖嵌套滚动视图来解决Google问题的变通方法)https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2非常有效!
定义FixAppBarLayoutBehavior.java。
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package your.package;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

/**
 * Workaround AppBarLayout.Behavior for https://issuetracker.google.com/66996774
 *
 * See https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 for
 * example usage.
 *
 * Change the package name as you wish.
 */
public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {

    public FixAppBarLayoutBehavior() {
        super();
    }

    public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
            int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
                dxUnconsumed, dyUnconsumed, type);
        stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
            View target, int dx, int dy, int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        stopNestedScrollIfNeeded(dy, child, target, type);
    }

    private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
        if (type == ViewCompat.TYPE_NON_TOUCH) {
            final int currOffset = getTopAndBottomOffset();
            if ((dy < 0 && currOffset == 0)
                    || (dy > 0 && currOffset == -child.getTotalScrollRange())) {
                ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
            }
        }
    }
}

在Java中的用法:

AppBarLayout abl = findViewById(R.id.app_bar);
((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).setBehavior(new FixAppBarLayoutBehavior());

在 XML 中的使用方式:
<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.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_height="..."
            android:layout_width="..."
            app:layout_behavior="your.package.FixAppBarLayoutBehavior">

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

    <!-- Content -->

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

这是在帖子当滚动时,CoordinatorLayout中RecyclerView上的点击无法工作中提供的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接