android - Multiple ViewPagers in on Activity -
i trying implement more 1 viewpager in 1 activity , not working. what's best way implement that. stuck!!
this activity
import android.app.activity; import android.os.bundle; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.widget.button; import com.viewpagerindicator.circlepageindicator; public class newworkoutactivity extends activity{ viewpager corepager; viewpager timepager; viewpager equipmentpager; circlepageindicator coreindicator; circlepageindicator timeindicator; circlepageindicator equipmentindicator; button startworkoutbutton; pageradapter coreadapter; pageradapter timeadapter; pageradapter equipmentadapter; string[] timesets; string[] coretargets; string[] equipments; int[] timeimages; int[] coreimages; int[] equipmentimages; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.new_workout); coretargets = new string[] { "full body", "core", "legs", "upper body"}; coreimages = new int[] { r.drawable.button_target_core, r.drawable.button_target_core2, r.drawable.button_target_core3, r.drawable.button_target_core4}; timesets = new string[] { "15 minutes", "20 minutes", "30 minutes", "45 minutes"}; timeimages = new int[] { r.drawable.button_target_core, r.drawable.button_target_core2, r.drawable.button_target_core3, r.drawable.button_target_core4}; equipments = new string[] { "rope", "kette bell", "weight", "hat"}; equipmentimages = new int[] { r.drawable.button_target_core, r.drawable.button_target_core2, r.drawable.button_target_core3, r.drawable.button_target_core4}; startworkoutbutton=(button)findviewbyid(r.id.startworkoutbutton); corepager=(viewpager)findviewbyid(r.id.corepager); timepager=(viewpager)findviewbyid(r.id.timepager); equipmentpager=(viewpager)findviewbyid(r.id.equipmentpager); coreindicator=(circlepageindicator)findviewbyid(r.id.coreindicator); timeindicator=(circlepageindicator)findviewbyid(r.id.timeindicator); equipmentindicator=(circlepageindicator)findviewbyid(r.id.equipmentindicator); coreadapter = new myviewpageradapter(newworkoutactivity.this, coretargets, coreimages); // binds adapter viewpager corepager.setadapter(coreadapter); timeadapter = new myviewpageradapter(newworkoutactivity.this, timesets, timeimages); // binds adapter viewpager timepager.setadapter(timeadapter); equipmentadapter = new myviewpageradapter(newworkoutactivity.this, equipments, equipmentimages); // binds adapter viewpager corepager.setadapter(coreadapter); // viewpager indicators coreindicator.setviewpager(corepager); equipmentindicator.setviewpager(equipmentpager); timeindicator.setviewpager(timepager); } } , adapter import android.content.context; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.relativelayout; import android.widget.textview; public class myviewpageradapter extends pageradapter { // declare variables context context; string[] desc; int[] image; layoutinflater inflater; public myviewpageradapter(context context, string[] desc, int[] image) { this.context = context; this.desc = desc; this.image = image; } @override public int getcount() { return desc.length; } @override public boolean isviewfromobject(view view, object object) { return view == ((relativelayout) object); } @override public object instantiateitem(viewgroup container, int position) { // declare variables textview desctext; imageview vpimage; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.body_page, container, false); // locate textview in viewpager_item.xml desctext = (textview) itemview.findviewbyid(r.id.vptext); // capture position , set textviews desctext.settext(desc[position]); // locate imageview in viewpager_item.xml vpimage = (imageview) itemview.findviewbyid(r.id.vpimage); // capture position , set imageview vpimage.setimageresource(image[position]); // add viewpager_item.xml viewpager ((viewpager) container).addview(itemview); return itemview; } @override public void destroyitem(viewgroup container, int position, object object) { // remove viewpager_item.xml viewpager ((viewpager) container).removeview((relativelayout) object); } }
the logcat telling me viewpagers don't have adapter instances.
this works me:
try code. not changes it.
note: images used random images.. can use own.
public class mainactivity extends actionbaractivity { context context; string[] timesets; string[] coretargets; string[] equipments; int[] timeimages; int[] coreimages; int[] equipmentimages; myadapter adapter1, adapter2, adapter3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); context = this; coretargets = new string[]{"full body", "core", "legs", "upper body"}; coreimages = new int[]{r.drawable.ic_launcher, r.drawable.abc_ab_share_pack_holo_dark, r.drawable.abc_ab_stacked_solid_dark_holo, r.drawable.abc_ab_bottom_solid_dark_holo}; timesets = new string[]{"15 minutes", "20 minutes", "30 minutes", "45 minutes"}; timeimages = new int[]{r.drawable.abc_ab_share_pack_holo_light, r.drawable.abc_ab_transparent_light_holo, r.drawable.abc_ic_ab_back_holo_light, r.drawable.abc_spinner_ab_pressed_holo_light}; equipments = new string[]{"rope", "kette bell", "weight", "hat"}; equipmentimages = new int[]{r.drawable.abc_ic_clear, r.drawable.abc_textfield_searchview_holo_dark, r.drawable.abc_spinner_ab_focused_holo_light, r.drawable.abc_ab_stacked_solid_dark_holo}; viewpager view1 = (viewpager) findviewbyid(r.id.viewpager1); viewpager view2 = (viewpager) findviewbyid(r.id.viewpager2); viewpager view3 = (viewpager) findviewbyid(r.id.viewpager3); adapter1 = new myadapter(coretargets, coreimages); adapter2 = new myadapter(timesets, timeimages); adapter3 = new myadapter(equipments, equipmentimages); view1.setadapter(adapter1); view2.setadapter(adapter2); view3.setadapter(adapter3); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
the adapter
private class myadapter extends pageradapter { string[] desc; int[] image; public myadapter(string[] desc, int[] image) { super(); this.desc = desc; this.image = image; } @suppresslint("newapi") @override public void finishupdate(viewgroup container) { // todo auto-generated method stub super.finishupdate(container); } @override public int getcount() { return desc.length; } @override public boolean isviewfromobject(view collection, object object) { return collection == ((view) object); } @override public object instantiateitem(view collection, int position) { // inflating layout layoutinflater inflater = (layoutinflater) collection.getcontext() .getsystemservice(context.layout_inflater_service); // setting view want display row element view view = inflater.inflate(r.layout.items, null); textview itemtext = (textview) view.findviewbyid(r.id.textviewmain); imageview imageview = (imageview) view.findviewbyid(r.id.imageviewmain); try { itemtext.settext(desc[position]); imageview.setimageresource(image[position]); } catch (exception e1) { // todo auto-generated catch block e1.printstacktrace(); } ((viewpager) collection).addview(view, 0); return view; } @override public void destroyitem(view collection, int position, object view) { ((viewpager) collection).removeview((view) view); } } }
the layout looks :
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.mike.app.mainactivity$placeholderfragment" android:background="#ffc42823"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.viewpager android:layout_width="fill_parent" android:layout_height="200dp" android:id="@+id/viewpager1" android:background="#333333" /> <view android:layout_width="fill_parent" android:layout_height="3dp" android:background="#ffffff"></view> <android.support.v4.view.viewpager android:layout_width="fill_parent" android:layout_height="200dp" android:id="@+id/viewpager2" android:background="#333333" /> <view android:layout_width="fill_parent" android:layout_height="3dp" android:background="#ffffff">></view> <android.support.v4.view.viewpager android:layout_width="fill_parent" android:layout_height="200dp" android:id="@+id/viewpager3" android:background="#333333" /> </linearlayout> </scrollview>
the items layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.mike.app.mainactivity$placeholderfragment" android:background="#ff10c7c6" android:orientation="horizontal" android:gravity="center"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:id="@+id/imageviewmain" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:text="some text" android:textsize="25sp" android:id="@+id/textviewmain" android:gravity="center" /> </linearlayout>
is looking ? if yes, works me.. lemme know if works.
Comments
Post a Comment