Display multiple pages in ViewPager, wrong position

I am trying to implement a Dave Smith PagerContainer to show multiple pages in my ViewPager. I introduced a click listener to display page numbers, but when I click on the left page, the message "clicked on item 2" is displayed (position 0 will be correct). If I click on the middle and right pages, the correct messages are displayed, "paragraph 1" and "paragraph 2" respectively. I also added an image to describe the problem. How can i fix this? Thanks in advance.

enter image description here

The following is a snippet of code:

@Override public Object instantiateItem(ViewGroup container, final int position) { TextView view = new TextView(PagerActivity.this); view.setText("Item "+position); view.setGravity(Gravity.CENTER); view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50)); view.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(PagerActivity.this, "clicked on Item " + String.valueOf(position), 1000).show(); } }); container.addView(view); return view; } 



  pager.setOffscreenPageLimit(adapter.getCount()); pager.setPageMargin(15); pager.setClipChildren(false); 
+4
android android-viewpager
Oct 26 '12 at 12:33
source share
2 answers

Helo

Change this in the PagerContainer.java file. I think this will help:

 @Override public boolean onTouchEvent(MotionEvent ev) { // We capture any touches not already handled by the ViewPager // to implement scrolling from a touch outside the pager bounds. switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mInitialTouch.x = (int) ev.getX(); mInitialTouch.y = (int) ev.getY(); default: if (mInitialTouch.x < mCenter.x) { ev.offsetLocation(-(mCenter.x - mInitialTouch.x), mCenter.y - mInitialTouch.y); } else { ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y); } break; } return mPager.dispatchTouchEvent(ev); } 
+3
Apr 24 '14 at 7:40
source share

There are various sensory processing and hardware acceleration issues in the CommonsWare-associated workaround. This is most likely a negative field definition for the ViewPager:

 ViewPager.setPageMargin( getResources().getDimensionPixelOffset(R.dimen.viewpager_margin)); 

Then I specified this dimension in my dimens.xml :

 <dimen name="viewpager_margin">-64dp</dimen> 

To compensate for overlapping pages, each page view has the opposite edge:

 android:layout_marginLeft="@dimen/viewpager_margin_fix" android:layout_marginRight="@dimen/viewpager_margin_fix" 

Again in the dimens.xml :

 <dimen name="viewpager_margin_fix">32dp</dimen> 

(Note that the size of viewpager_margin_fix is half the size of the absolute viewpager_margin .)

We applied this in an application for the Dutch newspaper De Telegraaf Krant :

Phone example in De Telegraaf KrantTablet example

+1
Jan 06 '14 at 11:33
source share



All Articles