Android在带有滚动条的文本视图上未接收到触摸事件
我有一个带有嵌套线性布局、ImageView和TextView的线性布局。其中一个TextView具有滚动条。我在LinearLayout类中重写了onTouchEvent()方法,但是当您用滚动条触摸TextView时,什么都没有注册 这是我的xml文件(讨论中的TextView是此布局中的最后一项):Android在带有滚动条的文本视图上未接收到触摸事件,android,events,touch,textview,Android,Events,Touch,Textview,我有一个带有嵌套线性布局、ImageView和TextView的线性布局。其中一个TextView具有滚动条。我在LinearLayout类中重写了onTouchEvent()方法,但是当您用滚动条触摸TextView时,什么都没有注册 这是我的xml文件(讨论中的TextView是此布局中的最后一项): 对此有何想法?如果有,是否有人知道如何克服此问题,以便我可以在此TextView上实现触摸事件?我不想回答我自己的问题,但我终于找到了答案。基本上,您必须截获活动发送的触摸事件。在inte
对此有何想法?如果有,是否有人知道如何克服此问题,以便我可以在此TextView上实现触摸事件?我不想回答我自己的问题,但我终于找到了答案。基本上,您必须截获活动发送的触摸事件。在intercept函数中,您可以确定要处理的触摸事件以及要让哪些事件传递到活动和其他子项 这就是我所拥有的,使我能够捕获“滑动”或“投掷”事件,同时允许所有其他触摸事件通过(例如,它允许上下滚动、长按、按钮按下事件通过)
MotionEvent\u downMotionEvent;
/**
*此功能拦截所有触摸事件。
*在这里,我们决定将什么传递给子项以及自己处理什么。
*
*@param motionEvent-发生的触摸事件。
*/
@凌驾
公共布尔dispatchTouchEvent(MotionEvent MotionEvent){
if(_debug)Log.v(“NotificationActivity.dispatchTouchEvent()”);
NotificationViewFlipper NotificationViewFlipper=getNotificationViewFlipper();
开关(motionEvent.getAction()){
case MotionEvent.ACTION\u DOWN:{
//跟踪开始向下的事件。
_downMotionEvent=MotionEvent.get(MotionEvent);
打破
}
case MotionEvent.ACTION\u UP:{
//如有必要,消费并执行投掷/滑动动作
//如果已经确定是一次放纵/打击
float deltaX=motionEvent.getX()-_downMotionEvent.getX();
final ViewConfiguration ViewConfiguration=ViewConfiguration.get(_context);
if(Math.abs(deltaX)>viewConfiguration.getScaledTouchSlop()*2){
如果(deltaX<0){
//在这里做正确的方向刷工作。
返回true;
}否则如果(deltaX>0){
//在这里进行向左滑动操作。
返回true;
}
}
打破
}
}
返回super.dispatchTouchEvent(motionEvent);
}
我希望这能帮助其他遇到类似问题的人。如果您将
android:focusable
和android:clickable
添加到LinearLayout
,会发生什么?您不需要“扩展”LinearLayout来覆盖onTouchEvent吗?如果确实扩展了该类,则需要将LinearLayout放入XML中,而不是一般的LinearLayout。使用什么来代替不推荐的ViewConfiguration构造函数?@Towpse-您希望执行以下操作(我更新了答案以反映这一点):final ViewConfiguration ViewConfiguration=ViewConfiguration.get(_context);
<?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>
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);
}