Testing the touch event mechanism on Android with a few Custom ViewGroups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I had this piece in my ConversationsActivity.java | |
scrollView.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
Log.d(TAG, "scrollView onTouch"); | |
return false; | |
} | |
}); | |
/*mViewPager.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
//scrollView.requestDisallowInterceptTouchEvent(false); | |
//mViewPager.requestDisallowInterceptTouchEvent(false); | |
Log.d(TAG, "mViewPager onTouch"); | |
return false; | |
} | |
});*/ | |
final ListView listView = (DisabledListView) findViewById(R.id.chatLog); | |
listView.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
Log.d(TAG, "listView onTouch"); | |
listView.requestDisallowInterceptTouchEvent(true); | |
return false; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.pycitup.pyc.CustomList.CustomScrollView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:fillViewport="true" | |
android:id="@+id/scrollView"> | |
<RelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:id="@+id/parentLayout"> | |
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/pager" | |
android:layout_width="wrap_content" | |
android:layout_height="400dp" | |
android:layout_marginBottom="10dp" /> | |
<com.pycitup.pyc.CustomList.DisabledListView | |
android:id="@+id/chatLog" | |
android:transcriptMode="alwaysScroll" | |
android:layout_width="match_parent" | |
android:layout_height="300dp" | |
android:layout_below="@+id/pager" | |
/> | |
<RelativeLayout | |
android:id="@+id/chatBox" | |
android:layout_width="match_parent" | |
android:background="#ffffff" | |
android:paddingTop="5dp" | |
android:paddingBottom="10dp" | |
android:paddingLeft="0dp" | |
android:paddingRight="0dp" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true"> | |
<EditText | |
android:id="@+id/chatMessage" | |
android:layout_toLeftOf="@+id/chatSend" | |
android:layout_alignBottom="@+id/chatSend" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Enter your message..." | |
android:inputType="textShortMessage" | |
android:imeOptions="actionSend"/> | |
<Button | |
android:id="@+id/chatSend" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingRight="10dp" | |
android:layout_alignParentRight="true" | |
android:text="Send" | |
android:textSize="18sp" > | |
</Button> | |
</RelativeLayout> | |
</RelativeLayout> | |
</com.pycitup.pyc.CustomList.CustomScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pycitup.pyc.CustomList; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.widget.ListView; | |
import android.widget.ScrollView; | |
import com.pycitup.pyc.ConversationsActivity; | |
/** | |
* Created by rishabhpugalia on 04/11/14. | |
*/ | |
public class CustomScrollView extends ScrollView { | |
private int mPosition; | |
private String TAG = DisabledListView.class.getSimpleName(); | |
public CustomScrollView(Context context) { | |
super(context); | |
} | |
public CustomScrollView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
//@Override | |
public boolean dispatchTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "ScrollView dispatchTouchEvent: " + ev.getAction()); | |
//super.dispatchTouchEvent(ev); | |
// return value depends on the child.dispatchTouchEvent's return value | |
// or if thats false then then this.onTouchEvent's return value | |
boolean bool = super.dispatchTouchEvent(ev); | |
Log.d(TAG, String.valueOf(bool)); | |
return true; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "ScrollView onintercepttouchevent: " + ev.getAction()); | |
//Thread.dumpStack(); | |
super.onInterceptTouchEvent(ev); | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "ScrollView ontouchevent custom implementation: " + ev.getAction()); | |
//Thread.dumpStack(); | |
super.onTouchEvent(ev); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pycitup.pyc.CustomList; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.widget.ListView; | |
import com.pycitup.pyc.ConversationsActivity; | |
/** | |
* Created by rishabhpugalia on 04/11/14. | |
*/ | |
public class DisabledListView extends ScrollDisabledListView { | |
private int mPosition; | |
private String TAG = DisabledListView.class.getSimpleName(); | |
public DisabledListView(Context context) { | |
super(context); | |
} | |
public DisabledListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public DisabledListView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
//@Override | |
public boolean dispatchTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "Child dispatchTouchEvent: " + ev.getAction()); | |
super.dispatchTouchEvent(ev); | |
// return value depends on the child.dispatchTouchEvent's return value | |
// or if thats false then then this.onTouchEvent's return value | |
//boolean bool = super.dispatchTouchEvent(ev); | |
//Log.d(TAG, String.valueOf(bool)); | |
return false; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "Child onintercepttouchevent: " + ev.getAction()); | |
//Thread.dumpStack(); | |
super.onInterceptTouchEvent(ev); | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
Log.d(TAG, "Child ontouchevent custom implementation: " + ev.getAction()); | |
Thread.dumpStack(); | |
super.onTouchEvent(ev); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pycitup.pyc.CustomList; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.widget.ListView; | |
import com.pycitup.pyc.ConversationsActivity; | |
/** | |
* Created by rishabhpugalia on 04/11/14. | |
*/ | |
public class ScrollDisabledListView extends ListView { | |
private int mPosition; | |
private String TAG = ScrollDisabledListView.class.getSimpleName(); | |
public ScrollDisabledListView(Context context) { | |
super(context); | |
} | |
public ScrollDisabledListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
//@Override | |
public boolean dispatchTouchEvent(MotionEvent ev) { | |
return super.dispatchTouchEvent(ev); | |
//Log.d(TAG, "dispatchTouchEvent: " + ev.getAction()); | |
//return false; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
//Log.d(TAG, "onintercepttouchevent: " + ev.getAction()); | |
//Thread.dumpStack(); | |
super.onInterceptTouchEvent(ev); | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
//Log.d(TAG, "ontouchevent custom implementation: " + ev.getAction()); | |
//Thread.dumpStack(); | |
super.onTouchEvent(ev); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DisabledListView
is the custom ListView implementation. It extendsScrollDisabledListView
which in turn extendsListView
.CustomScrollView
extendsScrollView
as a custom scroll view implementation.The View contains a
ViewPager
andDisabledListView
inside theCustomScrollView
. You can keep on logging different values by changing thedispatchTouchEvent()
,onInterceptTouchEvent()
,onTouchEvent()
code and even dump the stack to get more insights regarding how the touch system sort of works and the event propagates.The activity code is sort of optional to test with
View.OnTouchListener.onTouch()
event listeners.