Align the child views in center of the ViewPager android -
i need set child view center of viewpager , show part of next , previous views current view sides(like current screen below 1). current view starting @ left side of viewpager(like expected screen below 2). how can achieve that?
here code..
myviewpageradapter
public class myviewpageradapter extends pageradapter { private activity mactivity; private int mpagecount; public myviewpageradapter(activity activity,int pagecount) { mactivity = activity; mpagecount = pagecount; } @override public int getcount() { return mpagecount; } @override public boolean isviewfromobject(view view, object obj) { return (view ==(view)obj); } @override public object instantiateitem(viewgroup container,final int position) { viewgroup viewgroup = (viewgroup)mactivity.getlayoutinflater().inflate( r.layout.item_view, null); viewgroup.setbackgroundcolor(randomcolor()); textview textview = (textview)viewgroup.findviewbyid(r.id.textview1); textview.settext("page: "+(position+1)); button button = (button) viewgroup.findviewbyid(r.id.button1); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(mactivity, "hey, clicked!!! @ page "+(position+1), toast.length_long).show(); } }); container.addview(viewgroup); return viewgroup; } random rnd = new random(); private int randomcolor(){ return color.argb(255, rnd.nextint(256), rnd.nextint(256), rnd.nextint(256)); } @override public void destroyitem(viewgroup collection, int position, object view) { //must overridden else throws exception not overridden. log.d("tag", collection.getchildcount()+""); collection.removeview((view) view); } @override public float getpagewidth(int position) { return 0.8f; } }
mainactivity
public class mainactivity extends activity { private viewpager viewpager; linearlayout linearlayout; private int id = 100; private final int count = 8; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager = (viewpager) findviewbyid(r.id.viewpager); linearlayout = (linearlayout) findviewbyid(r.id.indicator_layout); generateindicators(count); myviewpageradapter adapter = new myviewpageradapter(this, count); viewpager.setadapter(adapter); viewpager.setonpagechangelistener(new onpagechangelistener() { int oldposition = 0; @override public void onpageselected(int position) { //this changes old position's view state image ((textview)linearlayout.getchildat(oldposition)).settext(""); oldposition = position; //this changes current position's view state image ((textview)linearlayout.getchildat(position)).settext((position+1)+""); } //this method called repeatedly upto item comes front one(active one) @override public void onpagescrolled(int arg0, float arg1, int arg2) { } //this called per scroll state @override public void onpagescrollstatechanged(int arg0) { } }); viewpager.setoffscreenpagelimit(4); } private void generateindicators(int count) { /// converts 14 dip equivalent px int padd = (int)typedvalue.applydimension(typedvalue.complex_unit_dip, 3, getresources().getdisplaymetrics()); for(int i=0;i<count;i++){ textview textview = new textview(this); textview.setid(id+i); final int currentitem = i; textview.setbackgroundresource(r.drawable.white_cell); textview.setpadding(padd,padd,padd,padd); /// converts 14 dip equivalent px int size = (int)typedvalue.applydimension(typedvalue.complex_unit_dip, 10, getresources().getdisplaymetrics()); textview.settextsize(size); textview.setgravity(gravity.center); /// converts 14 dip equivalent px int px = (int)typedvalue.applydimension(typedvalue.complex_unit_dip, 30, getresources().getdisplaymetrics()); linearlayout.layoutparams params = new linearlayout.layoutparams(px, px); linearlayout.addview(textview,params); } ((textview)linearlayout.getchildat(0)).settext("1"); } }
activity_main.xml
<relativelayout 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" tools:context=".mainactivity" > <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > </android.support.v4.view.viewpager> <linearlayout android:id="@+id/indicator_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="19dp" > </linearlayout> </relativelayout>
item_view.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="match_parent" android:id="@+id/root_view" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="text" android:textappearance="?android:attr/textappearancelarge" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="click me" /> </linearlayout>
current screen
expected screen
for 1 app implemented similar following way, standard viewpager
:
make pages full-screen actual content in inner layout. example, make full-screen layout
relativelayout
transparent background , actual contentrelativelayout
centered in parent. if remember right, reason inner layout page,viewpager
not have taken screen width on devices such galaxy nexus.use
viewpager.setpagemargin()
set negative page margin i.e. how of next/previous page want show. make sure overlaps transparent region of parent full-screen layout.call
viewpager.setoffscreenpagelimit()
adjust off-screen page count @ least2
default1
ensure smooth paging creating pages off-screen. otherwise see next/previous pages being drawn while partially showing on screen.