I would like to split this example into two seperate files in eclipse to prevent multiple classes in one file.

public class ViewFlipperSampleActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper;  
private Context mContext;

private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mContext = this;
    mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
    mViewFlipper.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }
    });
}

class SwipeGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));                 
                mViewFlipper.showNext();
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_out));
                mViewFlipper.showPrevious();
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}

}

The error that i get is that mViewFlipper is unknown because it is defined in the other class. What would be the best way to solve this issue?

有帮助吗?

解决方案

private ViewFlipper mViewFlipper;
Public SwipeGestureDetector(ViewFlipper mViewFlipper){
    this.mViewFlipper = mViewFlipper;
}

private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mContext = this;
    mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
    detector = new GestureDetector(new SwipeGestureDetector(mViewFlipper));
    mViewFlipper.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }
    });
}

Create a constructor with an argument as ViewFlipper for your SwipeGestureDetector class. Create a variable inside that class like I shown in the code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top