问题描述:

I'm using viewpager for showing some introduction to the user, there are two screens, when both the screen are finished I have to pass the intent. Right now, I have implemented viewpager and passed the intent.

Here is what I have done so far:

Edited my source code:

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);

}

}

Through these I'm able to implement the viewpager, but as you can see that I have called intent on onTouchListener when the user is on second screen.

My problem is that user can't see the first screen again if he wants to, because of the touchListener(), as soon as the user touches the screen the intent is called.

I have tried using GestureListener for fling action, but that is also not helping me.

How can I overcome this problem?

Any kind of help will be appreciated.

网友答案:

First of all I advise you rethink your logic. The actions of going to another Activity and also being able to swipe to the previous page seems a bit contradictory, a notification presented to the user when he gets to the last page(inviting him to "close" this introduction) I think it's a better choice.

Anyway you could do what you want. Set a OnPageChangeListener on the ViewPager to listen for interactions:

        @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);
                }
            }
        }

You'll need:

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);
        }
    }
};
网友答案:

You will get postion of the view page by

private static class PageListener extends SimpleOnPageChangeListener{
    public void onPageSelected(int position) {
        Log.i(TAG, "page selected " + position);
           currentPage = position;
          } 


}

Call the pageListner

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

Now you can get the postion of the view

相关阅读:
Top