简体   繁体   English

Android LinearLayout,最后添加的孩子在为onClickListener窃取点击

[英]Android LinearLayout, last added children stealing click for onClickListener

Setup: 设定:

I have a custom LinearLayout that has a TextView and an ImageView inside it (my attempts to resolve the issue are commented out): 我有一个自定义的LinearLayout,其中包含一个TextView和一个ImageView(我解决该问题的尝试已被注释掉):

public class MyTextView extends LinearLayout {

    private final TextView textView;
    private final ImageView imageView;

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);

        textView = new TextView(context);
        imageView = new ImageView(context);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(layoutParams);
        imageView.setImageResource(R.drawable.img);

        /* imageView.setDuplicateParentStateEnabled(true);
         textView.setDuplicateParentStateEnabled(true); */

        /* imageView.setFocusable(false);
        textView.setFocusable(false);
        textView.setTextIsSelectable(false); */

        addView(textView);
        addView(imageView);

        /* this.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        this.setClickable(true); */

    }
}

I'm including this custom LinearLayout in a larger layout: 我在更大的布局中包含了此自定义LinearLayout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <my.package.MyTextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

I'm setting an onClickListener this way: 我以这种方式设置onClickListener:

final MyTextView textView = (MyTextView) view.findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Code
    }
});

My issue: 我的问题:

The onClickListener only fires if I tap the ImageView, and not the TextView. 仅当我点击ImageView而不是TextView时才会触发onClickListener。 I'd like it to work if I tap any part of the LinearLayout instead. 我希望它能工作,如果我点击LinearLayout的任何部分。

I can't understand why this wouldn't work, not even with eg setDescendantFocusability set to FOCUS_BLOCK_DESCENDANTS, and would appreciate an explanation. 我什至无法理解为什么这行不通,即使将setDescendantFocusability设置为FOCUS_BLOCK_DESCENDANTS也是如此,请您多加解释。 Thanks a lot. 非常感谢。

问题是setMovementMethod在TextView上被调用,而我没有注意到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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