2011-05-06 90 views
0

我有一个带有一些嵌套LinearLayouts,ImageView和TextViews的LinearLayout。其中一个TextView有滚动条。我在我的LinearLayout类中覆盖了onTouchEvent()方法,但是当您使用滚动条触摸TextView时,什么也没有注册。Android没有在滚动条的TextView上接收触摸事件

这里是我的xml文件(有关TextView的是此布局中的最后一项):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:minWidth="310dp" 
    android:layout_width="fill_parent"> 
    <LinearLayout 
     android:id="@+id/from_linear_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left"> 
     <ImageView 
      android:src="@drawable/ic_dialog_info" 
      android:id="@+id/notification_type_icon_image_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:scaleType="center" 
      android:layout_margin="4dp"/> 
      <TextView 
       android:id="@+id/time_text_view" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical" 
       android:text="Timestamp" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:padding="1dp" 
       android:textColorLink="?android:attr/textColorPrimaryDisableOnly"/> 
    </LinearLayout> 
    <ImageView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/under_contact_image_view" 
     android:src="@drawable/divider_horizontal_dark" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingBottom="2dp" 
     android:paddingTop="2dp" /> 
    <TextView 
     android:text="Notification Text" 
     android:id="@+id/notification_text_view" 
     android:autoLink="all" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="?android:attr/textColorPrimaryDisableOnly" 
     android:layout_width="fill_parent" 
     android:gravity="left" 
     android:paddingRight="10dp" 
     android:paddingLeft="10dp" 
     android:textColorLink="?android:attr/textColorPrimaryDisableOnly" 
     android:layout_gravity="left" 
     android:layout_height="70dip" 
     android:scrollbars="vertical"/> 
</LinearLayout> 

关于这一点,如果是的话有什么想法,有没有人知道如何克服这让我可以实现在这个TextView触摸事件?

+0

如果您向'LinearLayout'添加'android:focusable'和'android:clickable',会发生什么? – 2011-05-10 14:26:48

+0

你不需要“扩展”LinearLayout来覆盖onTouchEvent吗?如果您确实扩展了该类,则需要将LinearLayout放入XML中,而不是通用的LinearLayout。 – 2011-05-10 14:45:11

回答

1

我讨厌回答我自己的问题,但我终于明白了这一点。基本上,你必须拦截活动发送的触摸事件。在拦截功能中,您可以确定要处理的触摸事件以及要让哪些事件通过活动和其他子项目。

这就是我让我在捕捉所有其他触摸事件时(例如,可以上下滚动,长按,按下按钮事件)捕捉“滑动”或“一甩”事件的功能。

MotionEvent _downMotionEvent; 

/** 
* This function intercepts all the touch events. 
* In here we decide what to pass on to child items and what to handle ourselves. 
* 
* @param motionEvent - The touch event that occured. 
*/ 
@Override 
public boolean dispatchTouchEvent(MotionEvent motionEvent){ 
    if (_debug) Log.v("NotificationActivity.dispatchTouchEvent()"); 
    NotificationViewFlipper notificationViewFlipper = getNotificationViewFlipper(); 
    switch (motionEvent.getAction()){ 
     case MotionEvent.ACTION_DOWN:{ 
      //Keep track of the starting down-event. 
      _downMotionEvent = MotionEvent.obtain(motionEvent); 
      break; 
     } 
     case MotionEvent.ACTION_UP:{ 
      //Consume if necessary and perform the fling/swipe action 
      //if it has been determined to be a fling/swipe 
      float deltaX = motionEvent.getX() - _downMotionEvent.getX(); 
      final ViewConfiguration viewConfiguration = ViewConfiguration.get(_context); 
      if(Math.abs(deltaX) > viewConfiguration.getScaledTouchSlop()*2){ 
       if (deltaX < 0){ 
        //Do work here for right direction swipes. 
        return true; 
       }else if (deltaX > 0){ 
        //Do work here for left direction swipes. 
        return true; 
       } 
      } 
      break; 
     } 
    } 
    return super.dispatchTouchEvent(motionEvent); 
} 

我希望这可以帮助任何遇到类似问题的人。

+0

使用什么来代替不推荐的ViewConfiguration构造函数? – topwik 2012-08-30 16:03:45

+1

@Towpse - 你想做以下事情(我更新了答案以反映这一点):final ViewConfiguration viewConfiguration = ViewConfiguration.get(_context); – 2012-08-31 16:08:10