如何从另一个班级的活动/班级获取视图?

时间:2018-11-07 18:17:49

标签: java android android-studio

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

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

1 个答案:

答案 0 :(得分:1)

创建一个侦听器:

#ifndef QCANBUS_H
#define QCANBUS_H

#include <QtCore/qobject.h>
#include "qserialbusglobal.h"
#include "qcanbusdevice.h"
#include "qcanbusdeviceinfo.h"

QT_BEGIN_NAMESPACE

class Q_SERIALBUS_EXPORT QCanBus : public QObject
{
    Q_OBJECT

public:
    static QCanBus *instance();
    QStringList plugins() const;

    QList<QCanBusDeviceInfo> availableDevices(const QString &plugin, QString *errorMessage = nullptr) const;

    QCanBusDevice *createDevice(const QString &plugin,
                                const QString &interfaceName,
                                QString *errorMessage = nullptr) const;

private:
    QCanBus(QObject *parent = nullptr);

    Q_DISABLE_COPY(QCanBus)
};

QT_END_NAMESPACE

#endif // QSERIALBUS_H

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

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