onClick not triggered on LinearLayout with child

2019-01-08 18:00发布

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 onclicklistener to the LinearLayout, but onClick is not triggered. If I set the onclick listener on the TextView it works as expected...

Anybody can help?

ar_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ar_item" android:layout_width="202dp"
    android:layout_height="62dp" android:background="@drawable/bg_item_ar"
    android:clickable="true">

    <TextView android:id="@+id/ar_item_txt"
        android:layout_width="164dp" android:layout_height="fill_parent"
        android:paddingBottom="8dp" android:paddingLeft="8dp"
        android:paddingTop="8dp" android:paddingRight="6dp" android:gravity="center"
        android:background="#50000000" />

</LinearLayout>

My custom LinearLayout

public class ARView extends LinearLayout
{    

    public ARView(final Context context, String name, String id)
    {        
        super(context);  
        getLayoutInflater().inflate(R.layout.ar_item, this ,true);
        LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        inflater.inflate(R.layout.ar_item, null);

        TextView textView = (TextView) findViewById(R.id.ar_item_txt);
        textView.setText(name);

        setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {                   
                Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
                t.show();
            }
        });
    }
}

14条回答
走好不送
2楼-- · 2019-01-08 18:48

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.

查看更多
3楼-- · 2019-01-08 18:50

One thing to make sure of is that another view is not on top of the view you are trying to click. This is especially common in FrameLayouts (where your sub LinearLayout may be covered) or with Relative Layouts you might have forgot to update this line:

android:layout_below="@id/shouldBeTheIdOfTheViewCurrentlyBeingCovered"

so that views don't fill the same space.

查看更多
Emotional °昔
4楼-- · 2019-01-08 18:50

Add the following attributes to the linearlayout Any Click events not handled by the child views will be automatically passed over to the LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:focusable="true"
    android:clickable="true">

<child/>
<child/>

</LinearLayout>
查看更多
smile是对你的礼貌
5楼-- · 2019-01-08 18:51

You aren't using your custom View; you're using a standard LinearLayout. Your XML tag should be:

<com.yourcode.ARView ...> ... </com.yourcode.ARView>
查看更多
beautiful°
6楼-- · 2019-01-08 18:54

android:duplicateParentState="true" did not help me.

To make your layout clickable with its children you need add this option for every child:

 android:clickable="false"

Then click handling will go up to parent.

查看更多
神经病院院长
7楼-- · 2019-01-08 18:54

Your TextView height covers the whole parent (whole layout) so you might clicking on empty space but not on the layout. Try using wrap_content for android:layout_height for your TextView. Set click listener for the layout as well.

查看更多
登录 后发表回答