A sign of calling the Viewpager when the user clicks from right to left

I use the viewpager to familiarize viewpager with the user, there are two screens, when both screens are finished, I must pass intent . Right now I have implemented viewpager and passed intent .

Here is what I have done so far:

Edited 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 them I can implement the viewpager , but as you can see, I called the intent on onTouchListener when the user is on the second screen.

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

I tried using the GestureListener for the fling action, but that also does not help me.

How can I solve this problem?

Any help would be appreciated.

0
source share
2 answers

First of all, I advise you to rethink your logic. The actions of moving to another Activity , as well as the ability to scroll to the previous page, seem a bit contradictory, the notification presented to the user when he gets to the last page (inviting him to “close” this introduction) I think this is the best choice.

In any case, you can do what you want. Set OnPageChangeListener to 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 will 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); } } }; 
+4
source

You will receive a message about the watch page.

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

Call pageListner

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

Now you can get an idea of ​​the presentation

0
source

All Articles