如何在android中重用一个监听器

时间:2012-09-23 17:10:48

标签: java android listener

我一直在努力让我的观点回应用户滑动。我在主Activity上工作得很好,我希望在多个活动中重复使用这些代码,而不必每次都复制粘贴。

我正在尝试找到一种方法将此侦听器移动到单独的类(和文件),然后调用可在每个活动中唯一定义的不同响应。我能够想到的唯一方法是将Activity扩展为自定义类(XActivity)。但这样做只有在我使用自定义类XActivity而不是ListActivity等时才有效。

有没有人知道如何隔离并将Listener用作自定义类,这样我就不必一遍又一遍地复制它?

View_Main     包us.cimple.testswipe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ViewConfiguration;
import android.widget.Toast;

public class View_Main extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Implement Gesture detection
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };
    View v = (View) getWindow().getDecorView().getRootView();
    v.setOnTouchListener(gestureListener);
}

private void openSecond(){
    Intent i = new Intent(this, View_Second.class);
    startActivity(i);
}

public final void toast(String message) {
    Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
}


/**
 * Gestures
 */
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

private class MyGestureDetector extends SimpleOnGestureListener {
    final ViewConfiguration vc = ViewConfiguration.get(getBaseContext());
    final int swipeMinDistance = vc.getScaledTouchSlop() * 2;
    final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity();

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try { 
            /**
            if (Math.abs(e1.getY() - e2.getY()) > swipeMinDistance){
               return false;
            }
            */

            float deltaX = 0;
            float deltaY = 0;

            deltaX = e1.getX() - e2.getX();
            deltaX = Math.abs(deltaX);

            deltaY = e1.getY() - e2.getY();
            deltaY = Math.abs(deltaY);

            Log.i("View_Main - deltaX", Float.toString(deltaX));
            Log.i("View_Main - deltaY", Float.toString(deltaY));

            Log.i("View_Main - swipe e1", Float.toString(e1.getX()) +" , " + Float.toString(e1.getY()));
            Log.i("View_Main - swipe e2", Float.toString(e2.getX()) +" , " + Float.toString(e2.getY()));

            //Vertical Swipe
            if (deltaY > deltaX){
                if(e1.getY() - e2.getY() > swipeMinDistance*2 && Math.abs(velocityY) > swipeThresholdVelocity) {
                onBottomToTopSwipe();
            }  else if (e2.getY() - e1.getY() > swipeMinDistance *2 && Math.abs(velocityY) > swipeThresholdVelocity) {
                onTopToBottomSwipe();
                }
            }

            //Horizontal Swipe
            else {
                if(e1.getX() - e2.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
                    onRightToLeftSwipe();
                }  else if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
                    onLeftToRightSwipe();
                }
            }    


        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}


private void onRightToLeftSwipe() {
    //do something      
    toast("Right to Left");
    openSecond();
}

private void onLeftToRightSwipe() {
    toast("Left to Right");
    onBackPressed();
}

private void onBottomToTopSwipe() {
    toast("Bottom to Top");
    //do something
}

private void onTopToBottomSwipe(){
    toast("Top to Bottom");
    //do something
}
}

由于

1 个答案:

答案 0 :(得分:0)

按活动替换ListActivity。你需要在活动里面使用listview。 现在继续你提到的方式。即通过某些XActivity类扩展所有内容。

但是你可以尝试更多的补丁,比如在BaseListActivity和BaseActivity上创建并在这两个补丁中使用这个监听器,不一定需要重复代码,可以像植入单独的类一样。