Вопрос

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