0

我一直在努力让我的观点响应用户的滑动。我让它在我的主要活动上运行良好,我想在多个活动上重用这段代码,而不必每次都复制和粘贴它。

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

有谁知道一种方法来隔离和使用 Listener 作为自定义类,这样我就不必一遍又一遍地复制它?

View_Main 包 us.ciple.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
}
}

谢谢

4

1 回答 1

0

Replace ListActivity by Activity. you will need to use a listview inside activity this. now go ahead with the way you mention . i.e. extend all by some XActivity class .

however you can try more patches like create on BaseListActivity and on BaseActivity and use this listener in both of them, not necessarily need repetition of code, can be like implantation in separate class .

于 2012-09-23T17:21:06.723 回答