的onClick方法不能正常工作后NestedScrollView滚动 [英] onClick method not working properly after NestedScrollView scrolled
问题描述
我用NestedScrollView与CoordinatorLayout,使滚动动画工具栏。(由应用程序:layout_scrollFlags =滚动| enterAlways)
NestedScrollView包含的LinearLayout作为根的孩子,我把2 TextViews到LinearLayout中,使展开/折叠动画。一个设置可见 而另一个被设置为不见了。而通过的LinearLayout的onClick事件切换的知名度。
通常情况下,一切都按预期方式工作,但是当我滚动的NestedScrollView onClick事件不能正常工作。我需要滚动后双击即可得到展开/折叠动画
有没有人有同样的问题我吗?请帮我
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< android.support.v4.widget.NestedScrollView
机器人:layout_width =match_parent
机器人:layout_height =match_parent
应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:paddingBottom会=98dp
机器人:以下属性来=24dp
机器人:paddingRight =24dp>
< android.support.v7.widget.AppCompatTextView
机器人:ID =@ + ID / detail_expense_reason_trim
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:单线=假
机器人:文字颜色=@色/ add_new_expense_text_color/>
< android.support.v7.widget.AppCompatTextView
机器人:ID =@ + ID / detail_expense_reason
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:单线=假
机器人:文字颜色=@色/ add_new_expense_text_color
机器人:能见度=水涨船高/>
< / LinearLayout中>
< /android.support.v4.widget.NestedScrollView>
< android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< android.support.v7.widget.Toolbar
机器人:ID =@ + ID / detail_expense_toolbar
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
应用程序:layout_scrollFlags =滚动| enterAlways
应用程序:popupTheme =@风格/ 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;
//处理事件
公共无效的onClick(){
如果(originalReason.getVisibility()== View.VISIBLE){
originalReason.setVisibility(View.GONE);
trimReason.setVisibility(View.VISIBLE);
} 其他 {
originalReason.setVisibility(View.VISIBLE);
trimReason.setVisibility(View.GONE);
}
}
这是NestedScrollView的一个bug,该bug的详细信息可以在这里找到:的问题。问题是, mScroller.isFinished()
在 onInterceptTouchEvent(MotionEvent EV)
将不会返回真正的
后一扔操作(即使一扔已停止)。因此,触摸事件截获。
这个错误已经报道了一段时间,但仍然没有得到解决。所以我通过自己的版本的bug修复这个问题产生。我实现了我自己的 NestedScrollView
,复制所有的code从 NestedScrollView
,并具有与如下修改:
公共类NestedScrollView扩展的FrameLayout实现NestedScrollingParent,NestedScrollingChild {
...
私人无效initScrollView(){
...
//替换这行:
// mScroller =新ScrollerCompat(的getContext(),NULL);
mScroller = ScrollerCompat.create(的getContext(),NULL);
...
}
@覆盖
公共布尔onInterceptTouchEvent(MotionEvent EV){
...
开关(行动及放大器; MotionEventCompat.ACTION_MASK){
...
案例MotionEvent.ACTION_DOWN:{
...
//替换这行:
// mIsBeingDragged = mScroller.isFinished()!;
mIsBeingDragged = FALSE;
...
}
}
}
}
和本 NestedScrollView
应具有相同的行为与原来的。
I used NestedScrollView with CoordinatorLayout to enable scroll animation for Toolbar (by app:layout_scrollFlags="scroll|enterAlways").
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
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;
// Handle event
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);
}
}
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.
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;
...
}
}
}
}
And this NestedScrollView
should have the same behaviour as the original one.
这篇关于的onClick方法不能正常工作后NestedScrollView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!