android - How to use two different GestureDetector for two different views? -


i have 2 linear layouts inside relativelayout. able detect gestures gesturedetector class. want detect gestures simultaneously on different views. this, want have 2 different gesturedetectors don't know how achieve this.

this xml file :

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffffff">  <linearlayout     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="500dp"     android:layout_alignparenttop="true"     android:id="@+id/linearlayout1"     android:layout_above="@+id/textview2"     android:layout_alignparentright="true"     android:layout_alignparentend="true"     android:background="#673ab7"></linearlayout>  <linearlayout     android:orientation="vertical"     android:id="@+id/linearlayout2"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_aligntop="@+id/textview2"     android:layout_alignparentright="true"     android:layout_alignparentend="true"     android:background="#1de9b6"></linearlayout>  <textview     android:id="@+id/textview1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_above="@+id/textview2"     android:layout_alignstart="@+id/textview2"     android:layout_marginbottom="47dp"     android:text="large text"     android:textappearance="?android:attr/textappearancelarge" />  <textview     android:id="@+id/textview2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerhorizontal="true"     android:layout_centervertical="true"     android:text="large text"     android:textappearance="?android:attr/textappearancelarge" /> 

this .java file :

public class mainactivity extends appcompatactivity implements ongesturelistener {  linearlayout mylayout; linearlayout mylayout2; gesturedetectorcompat detector;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     detector = new gesturedetectorcompat(this, this); }  @override protected void onstart() {     super.onstart();      mylayout = (linearlayout) findviewbyid(r.id.linearlayout1);     mylayout2 = (linearlayout) findviewbyid(r.id.linearlayout2);     mylayout.setontouchlistener(new linearlayout.ontouchlistener() {                                     public boolean ontouch(view v, motionevent m) {                                          return detector.ontouchevent(m);                                     }                                 }     );     mylayout2.setontouchlistener(new linearlayout.ontouchlistener() {                                      public boolean ontouch(view v, motionevent m) {                                            return detector.ontouchevent(m);                                      }                                  }     ); }  @override public boolean ondown(motionevent event) {      return true; }  @override public boolean onfling(motionevent event1, motionevent event2,                        float velocityx, float velocityy) {      return true; }  @override public void onlongpress(motionevent event) {     log.d("long press", "........."); }  @override public boolean onscroll(motionevent e1, motionevent e2, float distancex,                         float distancey) {     log.d("x,y", " " + e1.getx() + " " + e2.getx() + " " + e1.gety() + " " + e2.gety());      return true; }  @override public void onshowpress(motionevent event) {  }  @override public boolean onsingletapup(motionevent event) {      return true; }   } 

i want use different gesturedetector mylayout2.

try this, note put not operator in front of ontouchevent return.

package com.android.example.detectortestproj;  import android.support.v4.view.gesturedetectorcompat; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.gesturedetector; import android.view.motionevent; import android.view.view; import android.widget.linearlayout;  public class mainactivity extends appcompatactivity {      private static final string tag = "mainactivity";      linearlayout mylayout0;     linearlayout mylayout1;     gesturedetectorcompat mdetector0;     gesturedetectorcompat mdetector1;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      @override     protected void onstart() {         super.onstart();          mylayout0 = (linearlayout) findviewbyid(r.id.linearlayout1);         mylayout1 = (linearlayout) findviewbyid(r.id.linearlayout2);          mdetector0 = new gesturedetectorcompat(this, new simplegesturelistener(0));         mdetector1 = new gesturedetectorcompat(this, new simplegesturelistener(1));          mylayout0.setontouchlistener(new view.ontouchlistener() {             @override             public boolean ontouch(view view, motionevent motionevent) {                 return !mdetector0.ontouchevent(motionevent);             }         });          mylayout1.setontouchlistener(new view.ontouchlistener() {             @override             public boolean ontouch(view view, motionevent motionevent) {                 return !mdetector1.ontouchevent(motionevent);             }         });      }      public class simplegesturelistener extends gesturedetector.simpleongesturelistener{          private int mcontrolid = -1;         private static final string tag = "simplegesturelistener";          public simplegesturelistener(int controlid){             super();             mcontrolid = controlid;         }          @override         public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {             log.d(tag, "onfling() " + mcontrolid + " called with: " + "e1 = [" + e1 + "], e2 = [" +                     e2 + "], velocityx = [" + velocityx + "], velocityy = [" + velocityy + "]");             return true;         }          @override         public boolean ondoubletap(motionevent e) {             log.d(tag, "ondoubletap()  " + mcontrolid + " called with: " + "e = [" + e + "]");             return true;         }     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -