onClick not triggered on LinearLayout with child

后端 未结 15 1064
生来不讨喜
生来不讨喜 2020-11-29 03:33

I\'ve got a custom LinearLayout with a smaller TextView child. I\'d like to be able to click the area not covered by the TextView, so I set clickable=true and an onclicklist

相关标签:
15条回答
  • 2020-11-29 03:41

    I faced the same problem, and all the XML Attributes didn't work. I am not sure if this happens because i programmatically inflate and add the views, but this is how i worked around the problem.

    I have a Class which extends LinearLayout, with a TextView and an ImageView. After inflating the layout and getting the views, I assigned the child views a OnClickListener, when pressed, executes the LineaLayout's onClickListner.

    public class MyView extends LinearLayout {
    private OnClickListener clickListener;
    ImageView imageView;
    TextView textView;
    
    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
        clickListener = l;
    }
    
    void afterViews() {
        imageView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                clickListener.onClick(MyView.this);
                return false;
            }
        });
        textView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                clickListener.onClick(MyView.this);
                return false;
            }
        });
    }
    

    I also tried overriding OnTouchListener, but then my child views didn't have the ripple effect, which I needed.

    0 讨论(0)
  • 2020-11-29 03:42

    for every child

    android:duplicateParentState="true"

    0 讨论(0)
  • 2020-11-29 03:43

    This isn't your case, but I had similar problem with clickable ViewGroup. After a hour of looking for solution a found out that I set android:inputType to TextView inside my ViewGroup which was blocking onClick() listener (no idea why)

    Don't use android:inputType with TextView

    0 讨论(0)
  • 2020-11-29 03:43

    The problem may be from the textview that has android:layout_height="fill_parent" in its layout. If that doesn't fix the issue, the problem may be the onClick() event. The linear layout may not actually ever call onClick() since its a layout. Try overriding the onTouch() event listener instead.

    0 讨论(0)
  • 2020-11-29 03:43

    I faced the same problem, and all the XML attributes didn't work. I think this happens because I programmatically inflate and add the views. The fix for me was to - also that programatically - set the inflated root view not clickable:

    View view = layoutInflater.inflate(R.layout.some_layout, someLinearLayout, false);
    view.setClickable(false);
    

    (Yes, I tried to have the some_layout not clickable in XML.)

    0 讨论(0)
  • 2020-11-29 03:46

    The android:duplicateParentState="true" made my TextView looks like it's disabled, and cannot receive click event.

    All you need is set the TextView clickable="false". So the click event will dispatch to parent layout, and the TextView still can react to touch event (with ripple effect).

    0 讨论(0)
提交回复
热议问题