获取一个视图的触摸侦听器并在另一个视图上调用触摸事件

Take the touch listener of an view and call an touch event on another view

我有一个带有此侦听器的 viewpager。

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Float translation = position * viewpagerWidth *Constants.density;
            if(positionOffset != 0)
                image.setTranslationX(- translation -(positionOffsetPixels * Constants.density)/12);
        }

        @Override
        public void onPageSelected(int position) {
            page = position;
            mButtons.updateViewIntro(position + 1);
            switch (position){
                case 0:
                    title.setText(getString(R.string.passenger_name));
                    subtitle.setText(getString(R.string.intro_passenger_subtitle));
                    new FadeOutAnimation(arrowleft).animate();
                    break;
                case 1:
                    title.setText(getString(R.string.intro_mileage));
                    subtitle.setText(getString(R.string.intro_mileage_subtitle));
                    if(arrowleft.getVisibility() == View.GONE || arrowleft.getVisibility() == View.INVISIBLE)
                        new FadeInAnimation(arrowleft).animate();
                    if(arrowright.getVisibility() == View.GONE || arrowright.getVisibility() == View.INVISIBLE)
                        new FadeInAnimation(arrowright).animate();
                    break;
                case 2:
                    title.setText(getString(R.string.intro_analytics));
                    subtitle.setText(getString(R.string.intro_analytics_subtitle));
                    new FadeOutAnimation(arrowright).animate();
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

我的问题是:如果我有另一个视图,在底部,它与这个 viewpager 没有任何关系。是否可以在其上设置一个触摸侦听器,并使其也可以调用 viewpager 的 onPageChangeListener? 比如,我希望能够通过按下另一个对象来滚动 viewpager。 (我也需要在图像上添加它)

是的,只需为您的视图设置一个触摸侦听器,然后在寻呼机上调用 setCurrentItem。

private final View.OnClickListener mListener = new View.OnClickListener() {
    public void onClick(View view) {
        mPager.setCurrentItem(<page number here>);
    }
}

编辑:根据您的评论,试试这个:

yourView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mPager.onTouchEvent(event);
    }
});