ViewPager使用Button OnTouchListener事件滑动

时间:2013-11-23 07:21:43

标签: android android-layout android-viewpager

我有一个ViewPager,其中有2个项目,ButtonViewPager Animation。我想用按钮的TouchEvent滑动ViewPager而不用我的手指向上滑动,即从Button&滑动我的手指。获取ViewPager Swipe

目前,只有当我向上移动手指时,我才能进行滑动。不在TouchEvent

上滑动

以下是我的代码片段XML ...

<Button
    android:id="@+id/slide_btn_speak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@android:drawable/ic_btn_speak_now"
    android:contentDescription="@string/app_name" />

<TextView
    android:id="@+id/slide_txtView_inside"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/slide_btn_speak"
    android:text="@string/app_name" />

<android.support.v4.view.ViewPager
    android:id="@+id/ui_pager"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/slide_btn_speak"
    android:layout_toRightOf="@+id/slide_btn_speak"
    android:scrollbars="horizontal" >

</android.support.v4.view.ViewPager>

<Button
    android:id="@+id/slide_btn_drag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@android:drawable/ic_btn_speak_now"
    android:contentDescription="@string/app_name" />

我的活动片段......

public class SlidingActitvity extends Activity {

    ViewPager viewPager;

    Button slide_button_start ; 

    Vibrator vibrator;

    static int count ;

    int x_position, y_position;

    String hint[] ={" SWIPE TO CANCEL " , ""};
    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sliding_actitvity);

        viewPager = (ViewPager) findViewById(R.id.ui_pager);

        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        slide_button_start = (Button) findViewById(R.id.slide_btn_drag);



        slide_button_start.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                switch (event.getAction()) {

                case MotionEvent.ACTION_MOVE:

                    int viewPager_width = viewPager.getWidth();

                    x_position = (int) event.getX();
                    y_position = (int) event.getY();
                    if(x_position<= 0){

                        vibrator.cancel();
                        viewPager.bringToFront();

                        logIt("Touch..." +x_position +"..."+y_position);
                    }

                    break;

                case MotionEvent.ACTION_DOWN:

                    vibrator.vibrate(100);

                    final Animation animation = AnimationUtils.loadAnimation(SlidingActitvity.this, R.anim.left);

                    ViewPagerAdapter adapter = new ViewPagerAdapter(SlidingActitvity.this , hint);

                    viewPager.setAdapter(adapter);

                    viewPager.setVisibility(ViewPager.VISIBLE);

                    viewPager.setAnimation(animation);


                    logIt("Touch...Down");

                    break;

                case MotionEvent.ACTION_UP:

                     logIt("Touch...Up");
                     vibrator.cancel();

                     break;
                case MotionEvent.ACTION_OUTSIDE:
                    logIt("Your are out...");
                    vibrator.cancel();
                break ;
                default:
                    break;
                }

                return true;
            }
        });

    }

任何答案都得到赞赏......

1 个答案:

答案 0 :(得分:0)

在按钮的OnTouchListener中返回true,you're consuming the touch event。具体而言,需要向ViewPager通知MotionEvent.ACTION_DOWN,以便在检测到正在刷卡时拦截触摸事件。

您需要仔细考虑触摸事件在哪些情况下传递到视图层次结构,以便ViewPager可以继续发挥其魔力。