用户从右向左滑动时的Viewpager调用意图

时间:2013-04-20 17:50:41

标签: android android-intent android-viewpager

我正在使用viewpager向用户展示一些介绍,有两个屏幕,当屏幕都完成时我必须通过intent。现在,我已经实施了viewpager并通过了intent

这是我到目前为止所做的:

编辑我的源代码:

IntroActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    final ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);

    OnPageChangeListener mListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            selectedIndex = arg0;
            System.out.println("Selecte Page: " + selectedIndex);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
            if (mPageEnd && arg0 == selectedIndex) {
                Log.d(getClass().getName(), "Okay");
                mPageEnd = false;// To avoid multiple calls.
            } else {
                mPageEnd = false;
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            if (selectedIndex == adapter.getCount() - 1) {
                mPageEnd = true;
            }
        }
    };
    viewPager.setOnPageChangeListener(mListener);

}

private class ImagePagerAdapter extends PagerAdapter {
    private int[] mImages = new int[] { R.drawable.libin1,
            R.drawable.libin2 };

    @Override
    public int getCount() {
        return mImages.length;

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = MainActivity.this;
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(
                R.dimen.padding_medium);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageResource(mImages[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
}

通过这些,我可以实现viewpager,但正如您所看到的,当用户在第二个屏幕上时,我已在onTouchListener调用了意图。

我的问题是用户无法再次看到第一个屏幕,因为touchListener(),只要用户触摸屏幕就会调用意图。

我尝试使用GestureListener进行fling操作,但这对我没有帮助。

我该如何克服这个问题?

任何形式的帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:4)

首先,我建议你重新考虑一下你的逻辑。转到另一个Activity并且能够滑动到上一页的行为似乎有点矛盾,当用户到达最后一页时向用户发出通知(邀请他“关闭”此介绍)我认为这是一个更好的选择。

无论如何,你可以做你想做的事。在OnPageChangeListener上设置ViewPager以收听互动:

        @Override
        public void onPageScrollStateChanged(int state) {
            mCurrentState = state;
            // if we are at the second page and the user touched the
            // ViewPager post a Runnable with a decent time to schedule our
            // Intent
            if (mViewPager.getCurrentItem() == 1) {
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                    mHandler.postDelayed(mRun, INTERVAL);
                }
            }
        }

你需要:

private ViewPager mViewPager;
private int mCurrentState = -1;
private static int INTERVAL = ViewConfiguration.getLongPressTimeout();
private Handler mHandler = new Handler();
private Runnable mRun = new Runnable() {

    @Override
    public void run() {
        // we got the Runnable to be executed. If we are on the second page
        // and the user let go of the ViewPager in our time frame then start
        // the Activity(also cancel the dozen Runnables that were posted)
        if (mCurrentState == ViewPager.SCROLL_STATE_IDLE
                && mViewPager.getCurrentItem() == 1) {
            mHandler.removeCallbacks(mRun);// or always set it to run
            Intent i = new Intent(PagerTextTouch.this, MainActivity.class);
            startActivity(i);
        }
    }
};

答案 1 :(得分:0)

您将通过

获取视图页面的位置
private static class PageListener extends SimpleOnPageChangeListener{
    public void onPageSelected(int position) {
        Log.i(TAG, "page selected " + position);
           currentPage = position;
          } 


}

调用pageListner

pageListener = new PageListener(); ViewPager.setOnPageChangeListener(pageListener);

现在你可以得到视图的位置