5

我想访问在方法GestureListener本身内部使用gestureListener 的视图。这是我的听众:

public class GestureSwipeListener extends SimpleOnGestureListener {

    final Context myContext;

    // Costanti per lo swipe
    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    public GestureSwipeListener(Context context) {
        myContext = context;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD
                        && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD
                        && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

    public boolean onSwipeRight() {
        Log.d("FLING", "right");
        return true;
    }

    public boolean onSwipeLeft() {
        Log.d("FLING", "left");
        return true;
    }

    public boolean onSwipeTop() {
        Log.d("FLING", "top");
        return true;
    }

    public boolean onSwipeBottom() {
        Log.d("FLING", "bottom");
        return true;
    }

} // end class OnSwipeTouchListener

然后在我的活动中我这样做:

GestureSwipeListener gestureListener = new GestureSwipeListener(this);

GestireDetector gestureDetector = new GestureDetector(getApplicationContext(), gestureListener);


mainView = findViewById(R.id.main_view);
mainView.setClickable(true);
mainView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if(gestureDetector.onTouchEvent(event)) {

                return true;
            }
            return false;
        }
    });

现在我想直接在内部获取视图资源(在本例中为 mainView),GestureSwipeListener以在该视图上执行一些动画。
我想这样做是因为我必须在我的应用程序中的每个 Activity 上重复相同的动画。因此,直接在GestureListener(在onSwipeRight,onSwipeLeft 方法中)使用代码可以更好地避免一次又一次地重复相同的代码。我怎样才能做到这一点?

4

1 回答 1

6

向 SwipeGestureListener 构造函数添加参数,例如:

在你的手势监听器中:

View myView;

public GestureSwipeListener(Context context, View view) {
   myContext = context;
   myView = view;
}

public doSomethingWithView() {
  this.myView.setText("Foobar");
}

在您的活动中:

GestureSwipeListener gestureListener = new GestureSwipeListener(this.appContext, mainView);
...

更新1:

View mainView = findViewById(R.id.main_view);
GestureSwipeListener gestureListener = new GestureSwipeListener(this.appContext, mainView);

然后不要忘记在您的 GestureSwipeListener 构造函数中:

View myView;

public GestureSwipeListener(Context context, View view) {
   myContext = context;
   myView = view;
}
于 2013-04-22T09:33:26.300 回答