2

我在问这个相当复杂的问题,希望我能从你那里得到一个想法。

事情是这样的:我创建了一个自定义列表视图类,上面的那个:

public class FolderListView extends ListView {

    private float xDistance, yDistance, lastX, lastY;
    private int folder_index;
    private FolderViewInterface openFolder;

    // private boolean swiping = false;

    // If built programmatically
    public FolderListView(Context context) {
        super(context);
    }

    // This example uses this method since being built from XML
    public FolderListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Build from XML layout
    public FolderListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

        case MotionEvent.ACTION_UP:
            // if (swiping == false) {

            //if (xDistance > yDistance)
            //  return false;
        //  else {
                folder_index = pointToPosition((int) lastX, (int) lastY);
            //  Toast.makeText(getContext(),
            //          "You pressed a folder with index: " + folder_index,
            //          Toast.LENGTH_SHORT).show();
        //  }
            HomeScreenFragment f = new HomeScreenFragment();
            openFolder = (FolderViewInterface) f;
            openFolder.onFolderOpened(folder_index);
            // }
            // swiping = false;
            break;

        case MotionEvent.ACTION_DOWN:
            // swiping = false;
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            // swiping = true;
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance)
                return false;
        }

        return super.onInterceptTouchEvent(ev);

    }

}

事情就在这里,我正在寻找正确检测触摸事件。我想在触摸时打开另一个片段,但现在该片段在触摸和滑动时都打开了。

我使用了 onInterceptTouchEvent,因为我的列表中的元素是 viewpagers,并且在滚动时它们的行为不正确。

我需要某种逻辑来检测滑动而不执行这部分:

HomeScreenFragment f = new HomeScreenFragment();
            openFolder = (FolderViewInterface) f;
            openFolder.onFolderOpened(folder_index);

仅当事件是触摸事件而不是滑动事件时。

非常感谢任何形式的建议/帮助,如果您需要更多详细信息,请告诉我。

谢谢!

4

3 回答 3

2

time根据PRESSED_STATE_DURATION取 125

尝试这个

//Declare as instance variables
int oldX,oldY,newX,newY;
//State the minimum distance of swipe that you forgive the user for ;)
int distance = 10;
int time = 125;
boolean cancel = false;
case MotionEvent.ACTION_DOWN:
cancel = false;
//Get x and y of touch
oldX = ev.getX();
oldY = ev.getY();
startActionTimer();

case MotionEvent.ACTION_MOVE:
//Get x and y of touch
newX = ev.getX();
newY = ev.getY();

case MotionEvent.ACTION_UP:
cancel = true;

private void startActionTimer(){
        //time is pressed state duration
          new CountDownTimer(time, time) {
             public void onTick(long millisUntilFinished) {}

             public void onFinish() {
                //Time to do our action!!!
                // please consider
                if((Math.abs(newX - oldX )< distance) && (Math.abs(newY - oldY ) <distance)){
                    if(cancel == false){
                    // Now its a touch so do your action
                    }
                }
             }
          }.start();
     }

注意:请使用所有变量作为类变量(实例变量)以避免问题。因为有时我会遇到值未正确更新的奇怪情况

或者

你为什么不试试手势检测

并覆盖该onSingleTapUp方法

于 2013-08-05T12:41:38.913 回答
0

这是我的问题的答案,我最终想通了。我只需要使用一个布尔变量来检查何时执行刷卡并在不刷卡时执行适当的操作。它不是最好的解决方案,因为它非常敏感(即使是最轻微的滑动尝试也能检测到),但它仍然是一些东西。

谢谢大家帮助!

这是我更新的代码:

package ro.gebs.captoom.utils;

import ro.gebs.captoom.fragments.HomeScreenFragment;
import ro.gebs.captoom.interfaces.FolderViewInterface;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class FolderListView extends ListView {

    private float xDistance, yDistance, lastX, lastY;
    private int folder_index;
    private FolderViewInterface openFolder;

    private boolean swiping = false;

    // If built programmatically
    public FolderListView(Context context) {
        super(context);
    }

    // This example uses this method since being built from XML
    public FolderListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Build from XML layout
    public FolderListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

        case MotionEvent.ACTION_UP:
            folder_index = pointToPosition((int) lastX, (int) lastY);
            if (!swiping) {
                HomeScreenFragment f = new HomeScreenFragment();
                openFolder = (FolderViewInterface) f;
                openFolder.onFolderOpened(folder_index);
            }
            swiping = false;
            break;

        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();

            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance) {
                swiping = true;
                System.out.println("I AM HEREEEEEE");
                return false;
            }

        }

        return super.onInterceptTouchEvent(ev);

    }

}
于 2013-08-05T13:48:46.363 回答
0

如果你只需要在一个项目被触摸时做一些事情,你不需要自定义ListView,你只需要在 listView 的适配器上设置一个监听器,.setOnItemClickListener().

于 2013-08-05T12:38:44.273 回答