android - Scroll Images Horizontally with Page Indicator -
i trying implement feature user able scroll images left right or right left. since gallery
class has been deprecated, have added touchlistener
on imageview , implemented following method find out touch direction follows. have added radio button group give sense of page indicator.
it working , functional, wonder there drawbacks of approach? recommendation?
public class swipelistener implements view.ontouchlistener { private int min_distance = 100; private float downx, downy, upx, upy; view v; @override public boolean ontouch(view v, motionevent event) { this.v = v; switch(event.getaction()) { // check vertical , horizontal touches case motionevent.action_down: { downx = event.getx(); downy = event.gety(); return true; } case motionevent.action_up: { upx = event.getx(); upy = event.gety(); float deltax = downx - upx; float deltay = downy - upy; //horizontal scroll if (math.abs(deltax) > math.abs(deltay)) { if (math.abs(deltax) > min_distance) { // left or right if (deltax < 0) { this.onlefttorightswipe(); return true; } if (deltax > 0) { this.onrighttoleftswipe(); return true; } } else { //not long enough swipe... return false; } } } return false; } public void onlefttorightswipe(){ toast.maketext(v.getcontext(),"left right", toast.length_short).show(); } public void onrighttoleftswipe() { toast.maketext(v.getcontext(),"right left", toast.length_short).show(); } public void ontoptobottomswipe() { toast.maketext(v.getcontext(),"top bottom", toast.length_short).show(); } public void onbottomtotopswipe() { toast.maketext(v.getcontext(),"bottom top", toast.length_short).show(); } }
here radio buttons @ bottom of image view.
<radiogroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom" android:orientation="horizontal" android:id="@+id/radiogroup"> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radiobutton0" android:checked="true" /> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radiobutton1" /> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radiobutton2" /> </radiogroup>
i getting similar output follows. wonder drawbacks of approach?
you can use viewpager. add in xml file
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
and in class file
private viewpager mpager; private pageradapter mpageradapter;
in oncreate method add code
mpager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); mpager.setadapter(mpageradapter);
and add class subclass in main class.
private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch(position){ case 0: return new screenslidepagefragment(); case 1: return new screenslidepagefragment1(); } } @override public int getcount() { return num_pages; } }
Comments
Post a Comment