简体   繁体   中英

OnTouchListener doesn't work with relative layout

I have a RelativeLayout in which I am adding many TextViews dynamically. The problem I am facing is, whenever I apply an onTouch listener to TextViews individually, it detects touches but when i add touch to my relative layout, it never responds.

This code detects touch events just fine:

TextView tv = new TextView(this);
tv.setText(values[i]);
Drawable d = getResources().getDrawable(R.drawable.level_small_corners);

tv.setClickable(true);
tv.setId(i+1);
tv.setTextSize(18);
tv.setOnTouchListener(cellTouch);

But when I add all these TextViews in myRelativeLayout:

myRelativeLayout.setOnTouchListener(cellTouch);

Now, the onTouchListener never gets called. Why is that so?

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@android:color/black">
    .
    .
    .

    <ScrollView 
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"
        android:layout_below="@+id/levelFirstLayout"
        android:layout_marginBottom="70dp" > 

        <RelativeLayou
            android:id="@+id/wordsRelativeLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true" >

        </RelativeLayout>

    </ScrollView> 
    .
    .
    .
</RelativeLayout>

In myRelativeLayout.xml add:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

This worked for me:

yourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {  
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        //gesture detector to detect swipe.
        gestureDetector.onTouchEvent(arg1);
        return true;//always return true to consume event
    }
});
tv.setClickable(true);

is causing your layout, not to fire touch event. Try removing this.

It may that something in your view is "on top" of myRelativeLayout. If it is, it's getting the touch event first. The worst part about that is that the default handling of events is to do nothing with the event and then consume it.

One solution would be to add this code onto whatever components of your display are "above" the View (or Layout) that SHOULD handle the event:

someView.setOnTouchListener (new View.OnTouchListener()
{
    @Override
    public boolean onTouch (View v, MotionEvent event)
    {
        return false;
    }
});

The key (obviously) is to return false. When you do that, the event will not be consumed, but rather passed "down" to something in your relative layout -- hopefully somewhere you want it to go.

I know this is not the solution for your problem, but yours was the closest one to my issue, so maybe this will help somebody else.

Even though it seems like that in the designer, the layout is not "screen wide" in reality with some settings.

Width wrapped, No tap event in padding/layout

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

Or this, No tap event in padding/layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dip"
            android:layout_height="fill_parent"

With match parent, Tap events are firing

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

You have to set to your RelativeLayout view this attribute to true: android:clickable="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM