Android viewPager image slide right to left -
i want add image slide. cannot make slide right left. (for languages arabic or hebrew)
i checked replies in stackoverflow, can not find clear solution.
i write here whole code.
please write me clearly, not professional
mainactivity;
package com.manishkpr.viewpagerimagegallery; import android.app.activity; import android.os.bundle; import android.support.v4.view.viewpager; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); imageadapter adapter = new imageadapter(this); viewpager.setadapter(adapter); } }
here imageadapter.java;
package com.manishkpr.viewpagerimagegallery; import android.content.context; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.view.view; import android.view.viewgroup; import android.widget.imageview; public class imageadapter extends pageradapter { context context; private int[] galimages = new int[] { r.drawable.one, r.drawable.two, r.drawable.three }; imageadapter(context context){ this.context=context; } @override public int getcount() { return galimages.length; } @override public boolean isviewfromobject(view view, object object) { return view == ((imageview) object); } @override public object instantiateitem(viewgroup container, int position) { imageview imageview = new imageview(context); int padding = context.getresources().getdimensionpixelsize(r.dimen.padding_medium); imageview.setpadding(padding, padding, padding, padding); imageview.setscaletype(imageview.scaletype.center_inside); imageview.setimageresource(galimages[position]); ((viewpager) container).addview(imageview, 0); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { ((viewpager) container).removeview((imageview) object); } }
and here layout file;
<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/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
create layout file pager_item.xml
:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageview" /> </linearlayout>
change pageradapter this:
public class imageadapter extends pageradapter { context context; private int[] galimages = new int[] { r.drawable.one, r.drawable.two, r.drawable.three }; layoutinflater mlayoutinflater; imageadapter(context context){ this.context=context; mlayoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return galimages.length; } @override public boolean isviewfromobject(view view, object object) { return view == ((linearlayout) object); } @override public object instantiateitem(viewgroup container, int position) { view itemview = mlayoutinflater.inflate(r.layout.pager_item, container, false); imageview imageview = (imageview) itemview.findviewbyid(r.id.imageview); imageview.setimageresource(galimages[position]); container.addview(itemview); return itemview; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((linearlayout)object); } }
edit 1:
a trick :
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); imageadapter adapter = new imageadapter(this); viewpager.setadapter(adapter); viewpager.setcurrentitem(adapter.getcount()-1); }
i hope helps :)
Comments
Post a Comment