android - Scrolling a HorizontalScrollView by clicking buttons on its sides -
i using horizontalscrollview
within fragment
. when scroll view instead of scrolling items within horizontalscrollview
whole fragment scrolled either left or right. have thought of using buttons on both sides of layout. cannot how scroll scroll view on either side.
any in regard highly appreciated.
edit
following xml file.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <imagebutton android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:src="@drawable/left_arrow" /> <imagebutton android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:src="@drawable/right_arrow" /> <horizontalscrollview android:id="@+id/horizontal_scrollview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toleftof="@id/btn_right" android:layout_torightof="@id/btn_left" android:fadescrollbars="false" android:padding="5dp" android:scrollbaralwaysdrawhorizontaltrack="true" android:scrollbars="horizontal" > <linearlayout android:id="@+id/dynamic_generation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </linearlayout> </horizontalscrollview>
and java file
public class mygallery extends activity { private linearlayout linearlayout; private imagebutton leftbtn; private imagebutton rightbtn; private horizontalscrollview hsv; int currentscrollx = 0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.horizontalscrolling); leftbtn = (imagebutton) findviewbyid(r.id.btn_left); rightbtn = (imagebutton) findviewbyid(r.id.btn_right); hsv = (horizontalscrollview) findviewbyid(r.id.horizontal_scrollview); linearlayout = (linearlayout) findviewbyid(r.id.dynamic_generation); leftbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub } }); rightbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); string[] users = new string[20]; (int = 0; < 20; i++) { users[i] = "user " + (i + 1); } (int = 0; < users.length; i++) { final textview userid = new textview(mygallery.this); userid.settext(users[i]); imageview imageview = new imageview(mygallery.this); linearlayout.addview(userid); linearlayout.addview(imageview); userid.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(mygallery.this, userid.gettext() + " has been clicked!", toast.length_long).show(); // hsv. } }); } } }
what need
when press of left or right button scroll view must scroll left or right respectively.
if add following line of code existing handler, view scroll right every button click:
rightbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { hsv.scrollto((int)hsv.getscrollx() + 10, (int)hsv.getscrolly()); } });
if you'd scroll more smoothly can use ontouchlistener instead:
rightbtn.setontouchlistener(new view.ontouchlistener() { private handler mhandler; private long minitialdelay = 300; private long mrepeatdelay = 100; @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: if (mhandler != null) return true; mhandler = new handler(); mhandler.postdelayed(maction, minitialdelay); break; case motionevent.action_up: if (mhandler == null) return true; mhandler.removecallbacks(maction); mhandler = null; break; } return false; } runnable maction = new runnable() { @override public void run() { hsv.scrollto((int) hsv.getscrollx() + 10, (int) hsv.getscrolly()); mhandler.postdelayed(maction, mrepeatdelay); } }; });
vary delays liking smoothness , responsiveness want.
Comments
Post a Comment