Reputation: 697
I have a layout that is pretty simple - it has two views: a list view and a fully-covering "overlay" view; below is an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="360dp"
android:layout_height="270dp"
android:id="@+id/imageView"
android:layout_centerInParent="true"
android:background="#8888"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is some text"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
I made the overlay view a little simpler than what mine really is, there are no views that should want to have a touch (that is, no buttons or scrollable areas) - some LinearLayout's scattered about). What I'd like to do is ignore touches not only in the layout but in all the subviews of the layout as well. The only view I want to receive touches is the ListView.
I've implemented it in the simple way first - very similar to above - and the touches never get through to the underlying ListView (it doesn't scroll when I drag).
I don't really have the first clue how to do this for Android. Another SO answer (https://stackoverflow.com/a/9462091/875486) points to using some flags (specifically FLAG_NOT_TOUCHABLE and FLAG_NOT_FOCUSABLE) but I can't quite figure out how to use those flags.
Upvotes: 1
Views: 4127
Reputation: 1852
Why don't you assign an id in your xml to your RelativeLayout and give it an OnTouchListener to not do anything in code. This will override the underlying views without listeners.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="360dp"
android:layout_height="270dp"
android:id="@+id/imageView"
android:layout_centerInParent="true"
android:background="#8888"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is some text"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
...
RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.layout);
mLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Intercept touch events so they are not passed to underlying views.
return true;
}
});
ListView mListView = (ListView) findViewById(R.id.listView);
mListView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Do stuff
}
});
Upvotes: 4