2016-11-13 6 views
0

NestedScrollViewの上にRelativeLayoutがあります。私はRelativeLayout上のクリックの伝播を防ぎたいが、すべてのスクロールイベントを許可する。 android:clickable="true"追加 それはビュー上でクリックイベントとバイパススクロールイベントを回避するにはどうすればよいですか?

<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:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     ... content ... 

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_anchor="@id/scrollView" 
     app:layout_anchorGravity="bottom|left|end"> 

     <RelativeLayout 
      android:id="@+id/controllers_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/activity_vertical_margin" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_above="@+id/bottom_bar"> 

      ... content ... 

     </RelativeLayout> 

     ... content ... 

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

「controllers_container」IDとRelativeLayoutに適用すべきは、スクロールを含む任意のタッチイベントを、防止します。私はscrollViewにタッチリスナーを設定しようとしましたが、望みどおりに管理できませんでした。何か案は?

+0

親イベントへのタッチイベントを排除して、onTouchIntercept – Vicky

答えて

0

私はそれを行う方法を考え出しました。 基本的には、NestedScrollViewがタッチイベントを処理するビューを持っていました(targetView)。その後

public boolean handleTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_UP) { 

      ... Handle the action up event ... 

     } 
     return true; 
    } 

、私は(その場合には、これらすべての要素を含む断片)親にそのビューのタッチリスナーに設定します。私はそのようなタッチイベントを処理するパブリックメソッドを作成しました。操作が完了した場合はコントローラーコンテナの場合はhandleTouchメソッドを呼び出します。

targetView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_UP) { 
        int scrollY = nestedScrollView.getScrollY(); 
        int top = controllersContainer.getTop(); 
        float y = event.getY() + targetView.getTop(); 
        if (y - scrollY > top) { 
         return false; 
        } 
       } 
       return targetView.handleTouch(v, event); 
      } 
     }); 
関連する問題