android - Circlepage indicator to imageview using sliding -
i want add circle indicator images when images change 1 another. circle indicator has change @ same time.
you can use viewpager
, fragment
this. activity layout should contains viewpager
inside. , fragment
layout needs nothing imageview
.
in java code, fragment needs adapter, should this:
private class mypageradapter extends fragmentpageradapter { private arraylist<string> imagelist; private int imageposition; public mypageradapter(fragmentmanager fragmentmanager, arraylist<string> imagelist, int imageposition) { super(fragmentmanager); this.imagelist = imagelist; this.imageposition = imageposition; } @override public fragment getitem(int index) { return new galleryfragment(imagelist.get(index)); } @override public int getcount() { return imagelist.size(); } }
imagelist
used hold urls of pictures want display. can replace arraylist<integer> imagelist
if pictures want show in drawable folder.
for indicator part, textview text "●" fine.it may looks little strange, it's quite neat , easy. can change size , color of indicators wish.
then what's left put fragment
viewpager
gallery_pager.setadapter(new mypageradapter(galleryactivity.this.getsupportfragmentmanager(), curimagelist, imageposition)); gallery_pager.setcurrentitem(imageposition); gallery_pager.setonpagechangelistener(new onpagechangelistener() { @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } @override public void onpageselected(int index) { // todo auto-generated method stub (int = 0; < curimagelist.size(); i++) { pagerindicator[i].setvisibility(view.visible); pagerindicator[i].settextcolor(0xff666666); if (i == index) { pagerindicator[i].settextcolor(0xffffffff); } } } });
in method onpageselected
can controll how dots when picture slided to.
edited
most parts of codes had been added above, may need code fragment:
public class galleryfragment extends fragment{ private context context; private string imageurl; public galleryfragment(string imageurl) { this.imageurl = imageurl; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { context = galleryfragment.this.getactivity(); imageview image = new imageview(context); layoutparams params = new layoutparams(layoutparams.match_parent, layoutparams.match_parent); image.setlayoutparams(params); image.setscaletype(scaletype.fit_center); //todo use imageurl load , display image; linearlayout layout = new linearlayout(context); layout.setgravity(gravity.center); layout.addview(image); return layout; } }
and xml of activity should this:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#222222" > <android.support.v4.view.viewpager android:id="@+id/gallery_pager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" /> <linearlayout android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:orientation="horizontal" > <textview android:id="@+id/dot1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="●" android:textcolor="#15ede2" android:textsize="13sp" /> <!--add many dots here need. if size of imagelist changes, keep same amount of dots visible , others gone--> </linearlayout> </relativelayout>
Comments
Post a Comment