Previous data is showing on ViewPager Android -
in app, have viewpager
circlepageindicator
, it's working. it's show 2 items in 1 pager. when swipe pager it's show previous 1 item. dont want it. want swipe 2 item in time. doesn't show previous 1 shown on pager.
here previous question, it's related this. show 2 item in viewpager android.
currently working like
itema, itemb | itemb, itemc | itemc, itemd |i want like
itema, itemb | itemc, itemd | iteme, itemf |
this code used show 2 items in 1 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 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 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 , set textviews tv_domain.settext(domain[position]); tv_title.settext(title[position]); // locate imageview in viewpager_item.xml imageview.setimageresource(flag[position]); // add viewpager_item.xml viewpager } @override public void destroyitem(viewgroup container, int position, object object) { // remove viewpager_item.xml viewpager //((viewpager) container).removeview((relativelayout) object); } }
activity
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); // view 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 viewpager in viewpager_main.xml viewpagerrelated = (viewpager) findviewbyid(r.id.pager2); // pass results viewpageradapter class adapter = new viewpageradapter(articleviewactivityv2.this, domain, title, flag); // binds adapter 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 @ com.sample.news.adapters.viewpageradapter.configureitemview(viewpageradapter.java:94) @ com.sample.news.adapters.viewpageradapter.instantiateitem(viewpageradapter.java:64) @ android.support.v4.view.viewpager.addnewitem(viewpager.java:869) @ android.support.v4.view.viewpager.populate(viewpager.java:1085) @ android.support.v4.view.viewpager.populate(viewpager.java:951) @ android.support.v4.view.viewpager.ontouchevent(viewpager.java:2041) @ android.view.view.dispatchtouchevent(view.java:7717) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1979) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2250) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1951) @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:2087) @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1532) @ android.app.activity.dispatchtouchevent(activity.java:2467) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:2035) @ android.view.view.dispatchpointerevent(view.java:7897) @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:4005) @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:3884) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3445) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3495) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3464) @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:3571) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3472) @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:3628) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3445) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3495) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3464) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3472) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3445) @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:5653) @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:5633) @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:5604) @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:5733) @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:185) @ android.os.messagequeue.nativepollonce(native method) @ android.os.messagequeue.next(messagequeue.java:138) @ android.os.looper.loop(looper.java:123) @ android.app.activitythread.main(activitythread.java:5050) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect
thanks
i've looked viewpager implementation. method viewpager.determinetargetpage(int, float, int, int)
determine target page when scroll ended , see not possible change it.
to make work suggest workaround , creating 1 page, have 2 horizontal views inside. in case should remove
@override public float getpagewidth(int position) { return(0.5f); }
edit
to make work, add new layout xml related_article_item_page.xml:
<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 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)
should set itemview views data , in calculatefirstitemdataposition(int)
calculateseconditemdataposition(int)
should calculate data position current adapter position. remember change getcount()
method - 2x smaller
Comments
Post a Comment