java - 如何从另一个类(class)的 Activity/类(class)中获取 View ?

标签 java android android-studio

所以我有一个表格布局,在每一行中我都有一个 setOnTouchListener ,它的类允许我检测行中的滑动,它已经检测到了这一点,但我想做的是当人交换行时,该行被删除,我已经尝试了几件事,但我认为我需要获取其他类中的 View 才能获取行索引。 我的 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

最佳答案

创建监听器:

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() {}
        }
);

关于java - 如何从另一个类(class)的 Activity/类(class)中获取 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195454/

相关文章:

java - Eclipse git 插件和获取当前分支的方法 - 将 id 提交到 Java 代码版本信息字符串

android - Application 实例是否总是在任何 Activity 之前创建?

android - 数据库更新后刷新 Listfragment 中的 ListView

android-studio - 如何在 Android Studio 上为 KMM 调试 swift 代码?

Java:从扩展它的通用列表调用接口(interface)的函数

java - 连接到服务器中的 ms sql 数据库

sql - 如何在 Android 中使用列表适配器显示矩阵光标?

java - Android Studio 2.1.3 中构建错误

java - 使用蓝牙服务时如何避免出现 "Are you missing a call to unregisterReceiver()?"错误?

java - 坚持使用 java8 lambda 表达式