2014-11-04 59 views
0

我在列表视图上首次使用了GestureDetector。它工作正常,直到我有多个ListView项onclick。我想要GestureDetector整个列表,但它不工作,我在ListView中的项目的某些部分onclick事件。带有多个onclick监听器和手势检测器的Android ListView

考虑我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lineraMain" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="100" 
    android:background="#FFFFFF"> 


    <TextView android:id="@+id/meet_time" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:textColor="#FFFFFF" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:background="#ef3d4a" 
     android:textStyle="bold" 
     android:layout_weight="30" 
     android:text="@string/note"/> 

    <TextView android:id="@+id/textid" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:textColor="#FFFFFF" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:background="#ef3d4a" 
     android:textStyle="bold" 
     android:layout_weight="30" 
     android:text="@string/note"/> 

    <LinearLayout android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:orientation="horizontal" 
      android:layout_weight="10"> 

     <ImageView 
      android:id="@+id/arrow_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/down"/> 

     <ImageView 
      android:id="@+id/delete_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" 
      android:src="@drawable/icon_delete_red"/> 

    </LinearLayout> 

</LinearLayout> 

对于TextView中的文本ID和meet_time我设置onclicklistener,当我尝试刷一下这段文字这是行不通的。对于GestureDetector,我扩展了SimpleOnGestureListener。

请给我建议。谢谢。

回答

0

您首先需要了解OnTouch事件如何工作。
首先,OnClickListenerOnTouchListenter的私人案例,它的功能是在特定视图上监听downup触摸事件。如果触摸事件由某个听众“处理”,那么该听众返回,其停止将事件传播到涵盖/包含它的更“普通”视图。
在你的情况下,发生什么事是OnClickListener处理事件,并且不让ListView获得触摸事件。 您可能可以实现您自己的OnClickListener,它只与标准的OnClickListener不同,因为它会针对检测到的点击返回false
您应该多大范围内ListView和实施onInterceptTouchEvent这将使ListView“看到”触摸事件(你可以通过你的GestureDetector运行,并进行相应处理)

public class MyListView extends ListView { 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     //run through your GestureDetector 
     if (ev.getActionMasked() != ACTION_DOWN && yourGestureDetector.onTouchEvent(ev)) 
     { 
      //you have detected your gesture 
      //.... 
     } 
     return false; //return false so that the child TextViews will continue to get touch events 
    } 
} 

希望这有助于。

+0

你能举个例子吗? – 2014-11-05 03:59:58

+0

查看我编辑的答案,我想你现在会有答案 – 2014-11-05 15:24:11