Previous data displayed on ViewPager Android

In my application, I have a ViewPager with CirclePageIndicator and it works. It shows two elements in one pager. When I sit down on another pager, it shows the previous one element. I do not want it. I want to do two elements at a time. does not show the previous one, which is already shown on the pager.

Here is my previous question related to this. Show two items in a ViewPager Android browser .

Currently works as
ItemA, ItemB | ItemB, ItemC | ItemC, ItemD |

I want to be like ItemA, ItemB | ItemC, ItemD | ItemE, ItemF |

This is the code that I used to display two elements in one pager.

 @Override public float getPageWidth(int position) { return(0.5f); } 

Adapter.class

 public class ViewPagerAdapter extends PagerAdapter { // Declare Variables Context context; String[] domain; String[] title; int[] flag; LayoutInflater inflater; public ViewPagerAdapter(Context context, String[] domain, String[] title, int[] flag) { this.context = context; this.domain = domain; this.title = title; this.flag = flag; } @Override public int getCount() { return (int) Math.ceil((double)title.length/2); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((RelativeLayout) object); } @Override public Object instantiateItem(ViewGroup container, int position) { inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View pageView = inflater.inflate(R.layout.related_article_item_page, container, false); // Locate the TextViews in viewpager_item.xml View itemView1 = pageView.findViewById(R.id.article_1); View itemView2 = pageView.findViewById(R.id.article_2); configureItemView(itemView1, calculateFirstItemDataPosition(position)); configureItemView(itemView2, calculateSecondItemDataPosition(position)); return pageView; } public int calculateFirstItemDataPosition(int position){ return position * 2; } public int calculateSecondItemDataPosition(int position){ return position * 2 + 1; } public void configureItemView(View view,int position){ inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View ConfigItemView = inflater.inflate(R.layout.related_article_item, null); // Locate the TextViews in viewpager_item.xml final Button btnSave = (Button) ConfigItemView.findViewById(R.id.btn_save); final Button btnLike = (Button) ConfigItemView.findViewById(R.id.btn_like); final TextView tv_domain = (TextView) ConfigItemView.findViewById(R.id.domain_text); TextView tv_title = (TextView) ConfigItemView.findViewById(R.id.title_text); ResizableImageView imageView = (ResizableImageView) ConfigItemView.findViewById(R.id.imageViewDynamic); final ProgressBar progressBar = (ProgressBar) ConfigItemView.findViewById(R.id.loading); // Capture position and set to the TextViews tv_domain.setText(domain[position]); tv_title.setText(title[position]); // Locate the ImageView in viewpager_item.xml imageView.setImageResource(flag[position]); // Add viewpager_item.xml to ViewPager } @Override public void destroyItem(ViewGroup container, int position, Object object) { // Remove viewpager_item.xml from ViewPager //((ViewPager) container).removeView((RelativeLayout) object); } } 

Activities

  public class TestingActivity extends ActionBarActivity { // Declare Variables ViewPager viewPager; PagerAdapter adapter; String[] title; String[] domain; int[] flag; CirclePageIndicator mIndicator; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from viewpager_main.xml setContentView(R.layout.viewpager_main); title = new String[]{"Canada", "Thailand", "United States United StatesUnited StatesUnited States United States United States United States United States United States United States United States United States United States United States United States United States ", "Indonesia", "Brazil", "Thailand", "Nigeria", "Singapore", "RussiaRussiaRussiaRussiaRussiaRussiaRussiaRussia", "Japan"}; domain = new String[]{"text1", "text1", "text1", "text1", "text1", "text1", "text1", "text1", "text1", "text1"}; flag = new int[]{R.drawable.arsenal, R.drawable.aston_villa, R.drawable.manchester_city, R.drawable.liverpool, R.drawable.chelsea, R.drawable.manchester_united, R.drawable.swansea, R.drawable.liverpool, R.drawable.west_brom, R.drawable.west_ham}; // Locate the ViewPager in viewpager_main.xml viewPagerRelated = (ViewPager) findViewById(R.id.pager2); // Pass results to ViewPagerAdapter Class adapter = new ViewPagerAdapter(ArticleViewActivityV2.this, domain, title, flag); // Binds the Adapter to the ViewPager viewPagerRelated.setAdapter(adapter); // viewPagerRelated.setPageMargin(3); // ViewPager Indicator mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager); mIndicator.setViewPager(viewPagerRelated); } } 

Log files

 java.lang.ArrayIndexOutOfBoundsException: length=9; index=9 at com.sample.news.adapters.ViewPagerAdapter.configureItemView(ViewPagerAdapter.java:94) at com.sample.news.adapters.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:64) at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869) at android.support.v4.view.ViewPager.populate(ViewPager.java:1085) at android.support.v4.view.ViewPager.populate(ViewPager.java:951) at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2041) at android.view.View.dispatchTouchEvent(View.java:7717) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2087) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1532) at android.app.Activity.dispatchTouchEvent(Activity.java:2467) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2035) at android.view.View.dispatchPointerEvent(View.java:7897) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4005) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3884) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3495) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3464) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3571) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3472) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3628) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3495) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3464) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3472) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5653) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5633) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5604) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5733) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:138) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:5050) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect 

thanks

+6
source share
1 answer

I looked at the implementation of ViewPager. The ViewPager.determineTargetPage(int, float, int, int) method determines the landing page when scrolling is completed, and, as I see, it cannot be changed.

To make this work, I offer you a workaround and creating one page that has 2 horizontal views. In this case, you must delete

 @Override public float getPageWidth(int position) { return(0.5f); } 

EDIT

To make this work, add a new xml related_article_item_page.xml layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/related_article_item" android:id="@+id/article_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <include layout="@layout/related_article_item" android:id="@+id/article_2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> 

Then change the ViewPagerAdapter.instatiateItem(ViewGroup, int) method:

 inflater = ... View pageView = infalter.inflate(R.layout.related_article_item_page, container, false); View itemView1 = pageView.findViewById(R.id.article_1); View itemView2 = pageView.findViewById(R.id.article_2); configureItemView(itemView1, calculateFirstItemDataPosition(position)); configureItemView(itemView2, calculateSecondItemDataPosition(position)); ((ViewPager) container).addView(pageView); return pageView; 

in configureItemView(View, int) you must set the item view data, and in calculateFirstItemDataPosition(int) calculateSecondItemDataPosition(int) you must calculate the data position for the current adapter position. Also do not forget to change the way getCount() - now it will be 2x less

+1
source

All Articles