CoordinatorLayout:如何防止滚动事件到达 RecyclerView?
CoordinatorLayout: How can I prevent scroll events from reaching RecyclerView?
我有一个布局,其中包含一个 CoordinatorLayout
父级和一个包含 ViewPager
的 NestedScrollView
,其中包含一个 RecyclerView
:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.snap.MainActivity"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:layout_gravity="top"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager_photosvideos_albums"
android:layout_width="match_parent"
android:layout_height="1000dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout1">
<android.support.design.widget.TabLayout
android:id="@+id/id_tab_photosvideos_albums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我想实现以下目标:
我想拦截(阻止)滚动事件达到RecyclerView
,直到达到某个阈值条件。 post其中,RecyclerView
应该开始正常拦截滚动事件了
我怎样才能做到这一点?
注意:我已经尝试调查 Behavior.onNestedScroll()
,但这似乎没有用。
你可以试试这个方法拦截UI个元素的触摸
findViewById(R.id.recyclerView).setOnTouchListener(this)
然后覆盖 onTouch 方法。
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
initialX = event.getX();
initialY = event.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_UP:
//
break;
case MotionEvent.ACTION_DOWN:
//do something here
break;
}
return true;
}
从 view 参数,您可以检查哪个视图被 touched 或滚动并获得 [=32= 触摸的相对位置] 元素,然后 return
true 告诉系统我要消费这个触摸事件和 return
false 让其他人处理触摸事件。
终于找到答案了 -
像这样食用它们(在 CoordinatorLayout.Behavior
中):
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View directTargetChild, View target, int nestedScrollAxes) {
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View target, int dx, int dy, int[] consumed) {
int mCurrentScrollOffset = target.getScrollY();
if(mCurrentScrollOffset <= thresholdScrollDistance){
consumed[1] = dy;
child.scrollBy(0, dy);
}
}
为什么?
因为,consumed[i]
是一个out变量,存储x和y滚动未消费的值以供消费。
我有一个布局,其中包含一个 CoordinatorLayout
父级和一个包含 ViewPager
的 NestedScrollView
,其中包含一个 RecyclerView
:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.snap.MainActivity"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:layout_gravity="top"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager_photosvideos_albums"
android:layout_width="match_parent"
android:layout_height="1000dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout1">
<android.support.design.widget.TabLayout
android:id="@+id/id_tab_photosvideos_albums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我想实现以下目标:
我想拦截(阻止)滚动事件达到RecyclerView
,直到达到某个阈值条件。 post其中,RecyclerView
应该开始正常拦截滚动事件了
我怎样才能做到这一点?
注意:我已经尝试调查 Behavior.onNestedScroll()
,但这似乎没有用。
你可以试试这个方法拦截UI个元素的触摸
findViewById(R.id.recyclerView).setOnTouchListener(this)
然后覆盖 onTouch 方法。
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
initialX = event.getX();
initialY = event.getY();
switch (motionEvent.getAction()){
case MotionEvent.ACTION_UP:
//
break;
case MotionEvent.ACTION_DOWN:
//do something here
break;
}
return true;
}
从 view 参数,您可以检查哪个视图被 touched 或滚动并获得 [=32= 触摸的相对位置] 元素,然后 return
true 告诉系统我要消费这个触摸事件和 return
false 让其他人处理触摸事件。
终于找到答案了 -
像这样食用它们(在 CoordinatorLayout.Behavior
中):
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View directTargetChild, View target, int nestedScrollAxes) {
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View target, int dx, int dy, int[] consumed) {
int mCurrentScrollOffset = target.getScrollY();
if(mCurrentScrollOffset <= thresholdScrollDistance){
consumed[1] = dy;
child.scrollBy(0, dy);
}
}
为什么?
因为,consumed[i]
是一个out变量,存储x和y滚动未消费的值以供消费。