方法未被调用

时间:2017-07-25 18:53:41

标签: java android

方法未被调用

//view flipper code 
......................................
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ViewFlipper mViewFlipper;
private float initialX;
private Context mContext;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mContext = this;
    mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);

    //play and stop button for image slideshow(working fine)

    findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mViewFlipper.setAutoStart(true);
            mViewFlipper.setFlipInterval(1000);
            mViewFlipper.startFlipping();
        }
    });

    findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mViewFlipper.stopFlipping();

        }
    });

}
//The below method is not getting invoked

public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            initialX = touchevent.getX();
            System.out.println(initialX); 
            //not displaying value in android 
            monitor
            break;
        case MotionEvent.ACTION_UP:
            float finalX = touchevent.getX();
            System.out.println(finalX);
            //not displaying value in android monitor
            if (initialX > finalX) {
                if (mViewFlipper.getDisplayedChild() == 0)
                 //image flipper has 3 images
                    break;
                mViewFlipper.showNext();
            } else {
                if (mViewFlipper.getDisplayedChild() == 2)
                    break;
                mViewFlipper.showPrevious();
            }
            break;
    }
    return false;
}
}

2 个答案:

答案 0 :(得分:1)

好吧,看来你正试图在触摸某些视图时执行一些操作。为此,您应该覆盖要为其接收触摸事件的视图的OnTouchListener。它可以按如下方式完成:

findViewById(R.id.your_view_id).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (touchevent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = touchevent.getX();
                    System.out.println(initialX);
                    break;
                case MotionEvent.ACTION_UP:
                    float finalX = touchevent.getX();
                    System.out.println(finalX);
                    if (initialX > finalX) {
                        if (mViewFlipper.getDisplayedChild() == 0)
                            //image flipper has 3 images
                            break;
                        mViewFlipper.showNext();
                    } else {
                        if (mViewFlipper.getDisplayedChild() == 2)
                            break;
                        mViewFlipper.showPrevious();
                    }
                    break;
            }
            return false;
        }
    });

答案 1 :(得分:1)

onTouchEvent仅适用于Views。活动本身不是意见。相反,您需要设置onTouchListener。