OnTouchListener不适用于相对布局

Muh*_*mar 15 java android

我有一个动态RelativeLayout添加很多的东西TextViews.我面临的问题是,每当我TextViews单独应用onTouch监听器时,它会检测到触摸,但是当我向相对布局添加触摸时,它永远不会响应.

此代码检测触摸事件就好了:

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);
Run Code Online (Sandbox Code Playgroud)

但是当我在myRelativeLayout中添加所有这些TextView时:

myRelativeLayout.setOnTouchListener(cellTouch);
Run Code Online (Sandbox Code Playgroud)

现在,onTouchListener永远不会被调用.为什么会这样?

 <?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"
             > 

        <RelativeLayout  

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

            >
        </RelativeLayout>

        </ScrollView> 
    .
    .
    .
        </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

ρяσ*_*я K 18

在myRelativeLayout.xml中添加:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试过了.它不起作用.看看我做的补充 (2认同)

小智 14

这对我有用:

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
    }
});
Run Code Online (Sandbox Code Playgroud)


Muh*_*mar 7

tv.setClickable(true);
Run Code Online (Sandbox Code Playgroud)

导致你的布局,而不是触发触摸事件.尝试删除它.