onTouchListener in ViewPager

最后都变了- 提交于 2020-01-15 03:18:52

问题


I have an ImageView with onTouchListener inside a ViewPager. Of course the ImageView's listener fires off when I try to change ViewPager's page. Is there a way to prevent it. I guess I need to recognize touch from swipe somehow.

This is the extract of my code :

public class ServerDetailFragment extends Fragment implements SimpleGestureListener {
GestureDetector detector;
SimpleOnGestureListener gestures = new SimpleOnGestureListener() {
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.e("TAG", "onFling");
        return true;
    };
};
....


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    detector = new GestureDetector(ServerDetailFragment.this.getActivity(), gestures);

}

....

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_server_detail, container, false);
    MyPagerAdapter adapter = new MyPagerAdapter();
    myPager = (ViewPager) rootView.findViewById(R.id.detail_pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
    myPager.setOffscreenPageLimit(4);

    ViewTreeObserver vto = myPager.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            pagerGraph = (ImageView) myPager.findViewWithTag(myPager.getCurrentItem()).findViewById(R.id.pagerView);
            pagerPb = (ProgressBar) myPager.findViewWithTag(myPager.getCurrentItem()).findViewById(R.id.pagerPb);
            if (init) {

                plotFakeData();

                // Following problematic part is also in ViewPager's onPageChangeListener

                        pagerGraph.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (detector.onTouchEvent(event)) {

                            // some more code here

                        }
                        return false;
                    }

                });

                // myPager.setCurrentItem(0);
            }
            init = false;
        }
    });

回答1:


I'd go with something like this:

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        onTouch(e);
        return true;
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                    } else {
                    }
                }
            } else {
               // onTouch(e);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}
public void onTouch(MotionEvent e) {
}
}

and call them this way:

pagerGraph.setOnTouchListener(new OnSwipeTouchListener() {          
public void onTouch(MotionEvent event) {
    // your code here
});


来源:https://stackoverflow.com/questions/13073465/ontouchlistener-in-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!