滚动 NestedScrollView 后,onClick 方法无法正常工作 [英] onClick method not working properly after NestedScrollView scrolled

查看:36
本文介绍了滚动 NestedScrollView 后,onClick 方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NestedScrollView 和 CoordinatorLayout 为工具栏启用滚动动画(通过 app:layout_scrollFlags="scroll|enterAlways").

I used NestedScrollView with CoordinatorLayout to enable scroll animation for Toolbar (by app:layout_scrollFlags="scroll|enterAlways").

NestedScrollView 包含 LinearLayout 作为根子级,我将 2 个 TextViews 放入 LinearLayout 以启用展开/折叠动画.一个被设置为可见另一个设置为 Gone.并通过 LinearLayout 的 onClick 事件切换可见性

NestedScrollView contain the LinearLayout as the root child, I put the 2 TextViews into LinearLayout to enable expand/collapse animation. The one was set Visible and other one was set to Gone. And switching visibility by onClick event of LinearLayout

通常,一切都按预期工作,但是当我滚动 NestedScrollView 时onClick 事件无法正常工作.我需要在滚动后双击才能获得展开/折叠动画

Normally, everything work as expected but when I scrolled the NestedScrollView the onClick event not working properly. I need double click after scroll to get expand/collapse animation

有人和我有同样的问题吗?请帮帮我

Does anyone have same problem with me ? Please help me

<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.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="98dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/detail_expense_reason_trim"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:textColor="@color/add_new_expense_text_color" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/detail_expense_reason"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:textColor="@color/add_new_expense_text_color"
            android:visibility="gone" />
    </LinearLayout>

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/detail_expense_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>

 @InjectView(R.id.detail_expense_reason)
AppCompatTextView originalReason;

@InjectView(R.id.detail_expense_reason_trim)
AppCompatTextView trimReason;

@InjectView(R.id.detail_expense_container)
LinearLayout expenseContainer;

//处理事件

public void onClick() {
    if (originalReason.getVisibility() == View.VISIBLE) {
        originalReason.setVisibility(View.GONE);
        trimReason.setVisibility(View.VISIBLE);
    } else {
        originalReason.setVisibility(View.VISIBLE);
        trimReason.setVisibility(View.GONE);
    }

}

推荐答案

这是 NestedScrollView 的一个 bug,bug 的详细信息可以在这里找到:问题.问题是 onInterceptTouchEvent(MotionEvent ev) 中的 mScroller.isFinished() 在投掷操作后不会返回 true(即使投掷是停止).因此拦截了触摸事件.

It is a bug of the NestedScrollView, the detail of the bug can be found in here: issue. The problem is that mScroller.isFinished() in onInterceptTouchEvent(MotionEvent ev) will not return true after a fling operation (even if the fling is stopped). Therefore the touch event is intercepted.

这个错误已经被报告了一段时间,但仍然没有被修复.所以我为这个问题创建了自己的错误修复版本.我实现了自己的 NestedScrollView,从 NestedScrollView 复制了所有代码,并进行了以下修改:

This bug have been reported for a while, but still have not been fixed. So I have created by own version of bug fix for this problem. I implemented my own NestedScrollView, copied all the code from NestedScrollView and having the with the following amendments:

public class NestedScrollView extends FrameLayout implements NestedScrollingParent, NestedScrollingChild {
    ...
    private void initScrollView() {
        ...
        // replace this line:
        // mScroller = new ScrollerCompat(getContext(), null);
        mScroller = ScrollerCompat.create(getContext(), null);
        ...
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        ...
        switch (action & MotionEventCompat.ACTION_MASK) {
            ...
            case MotionEvent.ACTION_DOWN: {
                ...
                // replace this line:
                // mIsBeingDragged = !mScroller.isFinished();
                mIsBeingDragged = false;
                ...
            }
        }
    }   
}

而且这个 NestedScrollView 应该和原来的有一样的行为.

And this NestedScrollView should have the same behaviour as the original one.

这篇关于滚动 NestedScrollView 后,onClick 方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆