首页 > 解决方案 > 如何从另一个类的活动/类中获取视图?

问题描述

所以我有一个表格布局,在每一行中我都有一个 setOnTouchListener 类,它允许我检测行中的滑动,它已经检测到了,但我想要做的是当人交换行时,行已删除,我尝试了几件事,但我认为我需要获取其他类中的视图才能获取行索引。我的 Swipe Class 全局实例为:

ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);

每当我创建一行时,我只需添加: tr.setOnTouchListener(activitySwipeDetector); tr 是该行这就是 ActivitySwipeDetector,注释部分是我想要做的:

public class ActivitySwipeDetector implements View.OnTouchListener {

    static final String logTag = "ActivitySwipeDetector";
    private Activity activity;
    static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    Invoices invoices = new Invoices();
    NavManager nav = new NavManager();

    public ActivitySwipeDetector(Activity activity){
        this.activity = activity;
    }

    public void onRightSwipe(){
        System.out.println("Swiped Correctly from Right to Left");
        int index = (int)nav.getCurrentFocus().getTag();
        invoices.data.remove(index);
        System.out.println("Eliminado Correctamente");
    }

    public void onLeftSwipe(){
    }

    public void onDownSwipe(){
    }

    public void onUpSwipe(){
    }

    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                downY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_UP: {
                upX = event.getX();
                upY = event.getY();

                float deltaX = downX - upX;
                float deltaY = downY - upY;

                // swipe horizontal?
                if(Math.abs(deltaX) > Math.abs(deltaY))
                {
                    if(Math.abs(deltaX) > MIN_DISTANCE){
                        // left or right
                        if(deltaX > 0) { this.onRightSwipe(); return true; }
                        if(deltaX < 0) { this.onLeftSwipe(); return true; }
                    }
                    else {
                        Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                        return false; // We don't consume the event
                    }
                }
                // swipe vertical?
                else
                {
                    if(Math.abs(deltaY) > MIN_DISTANCE){
                        // top or down
                        if(deltaY < 0) { this.onDownSwipe(); return true; }
                        if(deltaY > 0) { this.onUpSwipe(); return true; }
                    }
                    else {
                        Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                        return false; // We don't consume the event
                    }
                }
                return true;
            }
        }
        return false;
    }

在此先感谢,我真的很感激至少有一些关于该做什么的想法!*编辑:表格由数据填充:一个 ArrayList

标签: javaandroidandroid-studio

解决方案


创建一个监听器:

public interface SwipeCallback {
    void onRightSwipe();
    void onLeftSwipe();
    void onDownSwipe();
    void onUpSwipe();
}

将该侦听器添加到您的构造函数并将其设置为全局变量:

private SwipeCallback swipeCallback;

public ActivitySwipeDetector(Activity activity, SwipeCallback callback) {
    this.activity = activity;
    swipeCallback = callback;
}

然后在对应的方法中调用监听器的方法:

public void onRightSwipe() {
    listener.onRightSwipe();
    //...
}

//etc

构建检测器时:

ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(
        this,
        new SwipeCallback() {
            //handle the swipes
            @Override
            public void onRightSwipe() {}

            @Override
            public void onLeftSwipe() {}

            @Override
            public void onUpSwipe() {}

            @Override
            public void onDownSwipe() {}
        }
);

推荐阅读