android - Handling touch Events for all layouts in xml -
i have linear layout contains 5 linear layouts child. want handle touch event each child linear layouts. layout looks this
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container"> <linearlayout android:id="@+id/item1" android:layout_width="match_parent" style="@style/navlinkitemcontainer" android:layout_height="wrap_content"> <textview style="@style/navilinkselected" android:text="first"/> </linearlayout> <linearlayout android:id="@+id/item2" android:layout_width="match_parent" style="@style/navlinkitemcontainer" android:layout_height="wrap_content"> <textview style="@style/navilinks" android:text="second"/> </linearlayout> <linearlayout android:id="@+id/item3" android:layout_width="match_parent" style="@style/navlinkitemcontainer" android:layout_height="wrap_content"> <textview style="@style/navilinks" android:text="third"/> </linearlayout> <linearlayout android:id="@+id/item4" android:layout_width="match_parent" style="@style/navlinkitemcontainer" android:layout_height="wrap_content"> <textview style="@style/navilinks" android:text="fourth"/> </linearlayout> <linearlayout android:id="@+id/item5" android:layout_width="match_parent" style="@style/navlinkitemcontainer" android:layout_height="wrap_content"> <textview style="@style/navilinks" android:text="fifth"/> </linearlayout> </linearlayout>
and activity using layout looks like
public class mainactivity extends activity implements ontouchlistener{ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } @override public boolean ontouch(view v, motionevent event) { log.d("","on touch view ######## "+v.getid()); return false; } }
when touching child layouts, not getting id(item1,item2...) in ontouch event please advice.
for each layout want add touch listener, set ontouchlistener.
for example,
linearlayout l1 = (linearlayout) findviewbyid(r.id.item2); l1.setontouchlistener(this);
so each viewgroup have set listener. rest of things done your. int ontouch method can handle touch or viewgroup
@override public boolean ontouch(view v, motionevent event) { switch (v.getid()) { case r.id.item2: break; case r.id.item3: break; default: break; } // if want consume behavior return true else retur false }
Comments
Post a Comment