将点击处理程序添加到自定义布局中

问题描述 投票:0回答:1

大家好。

我将自定义布局夸大为对象的行。

一切正常,但我无法将Click / LongClick设置为该行。

这是我所做的。

    Layout_BatchRow batchRow = new Layout_BatchRow(this);
    batchRow.New(batchObject);
    batchRow.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            . . .
        }
    });
    batchRow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            . . .
        }
    });

以下是自定义布局类:

public class Layout_BatchRow extends RelativeLayout {
    public Layout_BatchRow(Context context) {
        super(context);
        inflate(getContext(), R.layout.batch_row, this);
        . . .
    }
}

这是布局XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    tool:context=".Layout_BatchRow">

    . . .

</RelativeLayout>

调试时未访问侦听器。

任何想法?

android android-custom-view android-relativelayout
1个回答
1
投票

我认为您不需要添加此内容:

android:clickable="true"
android:focusable="true"
android:longClickable="true"
© www.soinside.com 2019 - 2024. All rights reserved.