working in android have followed online tutorial create view pager want programmatically add text to. have created bulk of adding 4 views, when scroll between views getting same view. code follows. there way initial view page1 when scrolled page2 , forth.
edit
just clarify, have rather simplified want here, end goal each "view" different. example page 1 contains textview, page 2 contains image view, page 3 contains larger text.
page 1 = "this page 1" page 2 = image of sheep page 3 = big red text saying "bang!"
xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="300dp" /> <imageview android:layout_width="match_parent" android:layout_height="wrap_content" app:srccompat="@drawable/myimage" android:id="@+id/imageview13" /> </linearlayout> </relativelayout>
java
import android.content.context; import android.graphics.color; import android.os.bundle; import android.os.parcelable; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.textview; public class testing extends appcompatactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_testing); viewpager vp = (viewpager) findviewbyid(r.id.viewpager); vp.setadapter(new mypageradapter(this)); } } class mypageradapter extends pageradapter { private context ctx; public mypageradapter(context context) { ctx = context; } // set number of slides @override public int getcount() { return 4; } @override public object instantiateitem(view collection, int position) { textview tv = new textview(ctx); tv.settextsize(50); tv.settextcolor(color.white); tv.settext("page1"); ((viewpager) collection).addview(tv); return tv; } // destroy @override public void destroyitem(view collection, int position, object view) { ((viewpager) collection).removeview((view) view); } // view object @override public boolean isviewfromobject(view view, object object) { return view == object; } // parceable @override public parcelable savestate() { return null; } // restore state @override public void restorestate(parcelable arg0, classloader arg1) { } //start update @override public void startupdate(view arg0) { } // finish update @override public void finishupdate(view arg0) { } }
position change when scroll page...
@override public object instantiateitem(view collection, int position) { if(position==0){ textview tv = new textview(ctx); tv.settextsize(50); tv.settextcolor(color.white); tv.settext("page "+position); collection.addview(tv); return tv; }else if(position==1){ imageview iv = new imageview(ctx); iv.setimageresource(r.mipmap.ic_launcher); collection.addview(iv); return iv; }else if(position==2){ textview tv = new textview(ctx); tv.settextsize(50); tv.settextcolor(color.white); tv.settext("bang"); collection.addview(tv); return tv; }else if(position==3){ imageview iv = new imageview(ctx); iv.setimageresource(r.mipmap.ic_launcher); collection.addview(tv); return iv; }
Comments
Post a Comment