2015-09-06 74 views
2

我想要滑动上/下手势。问题不在于如何实施向上/向下刷卡。我无法赶上onTouch电话。 Fragment布局中,有LinearLayout已设置onTouchListener。但不知何故,它无法正常工作 - 无法进行onTouch调用。Android - onTouchListener不工作的孩子LinearLayout

这是LinearLayout中:

<LinearLayout 
     android:id="@+id/llContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:id="@+id/btn_back" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" /> 

     <Button 
      android:id="@+id/btn_forward" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" /> 

    </LinearLayout> 

这是怎样的片段布局组件树enter image description here

内部片段的onCreateView,我设置onTouchListener到的LinearLayout:

LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent); 
    llContent.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      new MyLog("Touch"); 
      return true; 
     } 

    }); 

我想,这是因为孩子的按钮和c lickListeners。所以我禁用它们:

//  view.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() { 
//   @Override 
//   public void onClick(View v) { 
//    mCurrentIndex = (mCurrentIndex > 0) ? mCurrentIndex - 1 : 0; 
//    update(); 
//   } 
//  }); 
//  view.findViewById(R.id.btn_forward).setOnClickListener(new View.OnClickListener() { 
//   @Override 
//   public void onClick(View v) { 
//    mCurrentIndex = (mCurrentIndex < mPagination.size() - 1) ? mCurrentIndex + 1 : mPagination.size() - 1; 
//    update(); 
//   } 
//  }); 

在StackOverflow,有很多关于这个问题,并有解决方案。

尝试1: 这个answer说我应该从onTouch方法返回true。我的onTouch方法已经返回true。

@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     new MyLog("Touch"); 
     return true; 
    } 

尝试2: 这个回答说,我应该重写onTouchListener的方法的onTouchEvent。但是当我实现onTouchListener接口时,我无法重写onTouchEvent。 Android Studio的智能感知无法找到。当我手动编写它时,Android Studio显示onTouchEvent方法为错误。 错误:方法不会覆盖其超类中的方法。同样的答案也是here

问题:如何获得onTouch调用子LinearLayout?

这是整个片段布局XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FEFFFF"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="41dp" 
      android:background="#EFF0F1" 
      android:scrollbars="none"> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:gravity="center_vertical" 
       android:orientation="horizontal" 
       android:paddingLeft="57dp" 
       android:paddingRight="57dp"> 

       <TextView 
        android:id="@+id/tvTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#2E2F30" 
        android:textSize="17sp" /> 

       <TextView 
        android:id="@+id/tvAuthor" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:textColor="#5E5F60" 
        android:textSize="17dp" /> 

      </LinearLayout> 

     </HorizontalScrollView> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:paddingBottom="8dp" 
       android:paddingLeft="25dp" 
       android:paddingRight="25dp" 
       android:paddingTop="8dp"> 

       <TextView 
        android:id="@+id/tv" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:textColor="#1C1D1D" 
        android:textSize="18sp" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="24dp" 
        android:orientation="vertical"> 

        <View 
         android:layout_width="match_parent" 
         android:layout_height="1dp" 
         android:background="#050505" /> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="8dp" 
         android:orientation="horizontal"> 

         <TextView 
          android:id="@+id/tvPercent" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginRight="10dp" 
          android:textSize="15sp" /> 

         <TextView 
          android:id="@+id/tvProgress" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textColor="#444546" 
          android:textSize="15sp" /> 

        </LinearLayout> 

       </LinearLayout> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/llContent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <Button 
        android:id="@+id/btn_back" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" /> 

       <Button 
        android:id="@+id/btn_forward" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@android:color/transparent" /> 

      </LinearLayout> 


     </RelativeLayout> 

    </LinearLayout> 

</RelativeLayout> 
+0

你能解决这个问题吗?我有类似的问题,但在我的情况下,应用程序在通过按钮启动时没有检测到任何手势。 – tomDev

+0

[试试这个答案](http://stackoverflow.com/a/34845219/4819445)希望这可以帮助 – Leenah

+0

[试试这个答案](http://stackoverflow.com/a/13084649/4819445)希望这会有所帮助! – Leenah

回答

-1

试试这个:

LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent); 
     llContent.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       Log.d(TAG, "onTouch"); 
       return true; 
      } 

     }); 

     Button ff = (Button)view.findViewById(R.id.btn_forward); 
     ff.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       Log.i(TAG, "onTouch"); 
       return false; 

      } 
     }); 

     ff.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.i(TAG, "onClick"); 
      } 
     }); 

btn_forward高于llContent,因此你需要在onTouch()btn_forward)返回false。然后事件将从底部控件调用(llContent)。

在您的按钮上设置android:clickable="false"android:focusable="false"

+0

得到:09-06 13:37:20.752 21508-21508/net.joerichard.reader D/ReaderLog:bForward:onTouch 09-06 13:37:20.872 21508-21508/net.joerichard.reader D/ReaderLog: bForward:onTouch 09-06 13:37:20.872 21508-21508/net。joerichard.reader D/ReaderLog:bForward:onClick。 –

+0

仍然没有onTouch的llContent –

+0

编辑我的答案 –