2017-10-28 49 views
0

我有一個佈局由CoordinatorLayout母體的用含有ViewPager,其中包含一個NestedScrollView一個RecyclerViewCoordinatorLayout:如何防止滾動事件到達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滾動事件直到達到某個閾值條件。其中,RecyclerView將開始正常攔截滾動事件。

我該如何做到這一點?


注:我試圖尋找到Behavior.onNestedScroll(),但似乎並沒有這樣的伎倆。

回答

0

終於找到了答案 -

通過消費他們像這樣(在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]是一個存儲x和y滾動消耗的未消耗值的輸出變量。

0

您可以嘗試使用此方法來截取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; 
    } 

視圖參數,您可以檢查哪些觀點是感動或滾動,讓屏幕上的UI元素的觸摸的相對位置,然後回報
真正
告訴系統我想要消費此觸摸事件,並且返回
讓其他人處理觸摸事件。

+0

嗯,這是一般的。有沒有過濾滾動事件的解決方案? –

+0

好吧,我知道,它可能通過這個,但我想節省時間,如果有一個內置/較少涉及的方法可用,我懷疑可能有一個與CoordinatorLayout的所有流行 –

+0

我想我只知道這種方法,你能告訴我確切的條件或要求你想攔截觸摸recyclerview我會嘗試通過我的方法並排。 –

相關問題