android 获取 任何屏幕 touch坐标,整个屏幕的onTouchListener

首先将事件返回触摸到子视图。如果为它们定义onClick或onTouch侦听器,则parnt视图(例如片段)将不会收到任何触摸侦听器。因此,如果要在这种情况下为片段定义滑动监听器,则必须在新类中实现它:

package com.neganet.QRelations.fragments;

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.FrameLayout;

public class SwipeListenerFragment extends FrameLayout {

private float x1,x2;

static final int MIN_DISTANCE=150;

private onSwipeEventDetected mSwipeDetectedListener;

public SwipeListenerFragment(Context context) {

super(context);

}

public SwipeListenerFragment(Context context, AttributeSet attrs) {

super(context, attrs);

}

public SwipeListenerFragment(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

boolean result=false;

switch(ev.getAction())

{

case MotionEvent.ACTION_DOWN:

x1 = ev.getX();

break;

case MotionEvent.ACTION_UP:

x2 = ev.getX();

float deltaX = x2 - x1;

if (Math.abs(deltaX) > MIN_DISTANCE)

{

if(deltaX<0)

{

result=true;

if(mSwipeDetectedListener!=null)

mSwipeDetectedListener.swipeLeftDetected();

}else if(deltaX>0){

result=true;

if(mSwipeDetectedListener!=null)

mSwipeDetectedListener.swipeRightDetected();

}

}

break;

}

return result;

}

public interface onSwipeEventDetected

{

public void swipeLeftDetected();

public void swipeRightDetected();

}

public void registerToSwipeEvents(onSwipeEventDetected listener)

{

this.mSwipeDetectedListener=listener;

}

}

您可以完全像这样为其他类型的布局制作工具。此类可以检测到左右滑动,特别是它在检测到后返回onInterceptTouchEvent true。这很重要,因为如果某些时候我们不这样做,子视图可能会接收到事件,并且Swipe(用于片段)和onClick(用于子视图)都会运行,并引起一些问题。

制作完此类后,您必须更改片段xml文件:

xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\"

android:id=\"@+id/main_list_layout\"

android:clickable=\"true\"

android:focusable=\"true\"

android:focusableInTouchMode=\"true\"

android:layout_height=\"match_parent\" tools:context=\"com.neganet.QRelations.fragments.mainList\"

android:background=\"@color/main_frag_back\">

android:id=\"@+id/farazList\"

android:scrollbars=\"horizontal\"

android:layout_width=\"match_parent\"

android:layout_height=\"match_parent\"

android:layout_gravity=\"left|center_vertical\" />

您会看到begin标记是我们制作的类。现在在片段类中:

View view=inflater.inflate(R.layout.fragment_main_list, container, false);

SwipeListenerFragment tdView=(SwipeListenerFragment) view;

tdView.registerToSwipeEvents(this);

and then Implement SwipeListenerFragment.onSwipeEventDetected in it:

@Override

public void swipeLeftDetected() {

Toast.makeText(getActivity(), \"left\", Toast.LENGTH_SHORT).show();

}

@Override

public void swipeRightDetected() {

Toast.makeText(getActivity(), \"right\", Toast.LENGTH_SHORT).show();

}

这有点复杂,但是效果完美:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值