在具有绑定的自定义布局内的约束布局上添加 onClickListener

Add onClickListener on constraint layout inside custom layout with binding

我试图在约束布局上添加一个 onClickListener,但我没有成功地将它嵌入到我的布局中。

我有一个 class 和一个名为 view_button 的布局,其中包含以下元素:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/main_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_button"
        android:clickable="true"
        android:focusable="true">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginStart="16dp"
            android:src="@drawable/ic_add_24_vector"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="5dip"
            android:fontFamily="@font/lato_regular"
            android:gravity="center_vertical"
            android:text="START"
            android:textColor="@color/color_white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/icon"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

     </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

和我对应的Javaclass

public class SprintLaunchButton extends ConstraintLayout {

    private TextView mTextView;
    private ImageView mIconImageView;

    protected CharSequence attr_text = null;
    protected Drawable attr_image_drawable = null;

    public SprintLaunchButton(Context context) {
        super(context);
        inflateView(context);
        init(null);
        setView(context);
    }

    public SprintLaunchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateView(context);
        init(attrs);
        setView(context);
    }

    public SprintLaunchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflateView(context);
        init(attrs);
        setView(context);
    }

    private void inflateView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_sprint_button, this, true);

        mTextView = findViewById(R.id.text);
        mIconImageView = findViewById(R.id.icon);

        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setLayoutParams(params);

    }

    private void init(AttributeSet attrs) {
        consumeAttrs(getContext().obtainStyledAttributes(attrs, R.styleable.SprintLaunchButtonLayout));
    }

    public TextView getTextView() {
        return mTextView;
    }

    private void consumeAttrs(TypedArray styledAttrs) {
        if (styledAttrs == null) {
            return;
        }

        attr_text = styledAttrs.getString(R.styleable.SprintLaunchButtonLayout_buttonText);
        attr_image_drawable = styledAttrs.getDrawable(R.styleable.SprintLaunchButtonLayout_iconImage);

        styledAttrs.recycle();
    }

    public void setIconImage(@DrawableRes int id) {
        try {
            if (mIconImageView != null) {
                mIconImageView.setImageDrawable(getContext().getDrawable(id));
            }

        } catch (Resources.NotFoundException e) {

        }
    }

    public void setButtonText(@StringRes int id) {
        try {
            if (mTextView != null) {
              mTextView.setText(getContext().getString(id));
            }

        } catch (Resources.NotFoundException e) {

        }
    }

    protected void setView(Context context) {

        if (attr_text == null) {
            mTextView.setText("");
        } else {
            mTextView.setText(attr_text);
        }

        if (attr_image_drawable == null) {
            mIconImageView.setImageDrawable(null);
        } else {
            mIconImageView.setImageDrawable(attr_image_drawable);
        }

    }

}

当我使用 Java class 将自定义布局添加到我的应用程序时,如下所示:

<com.netexlearning.play.view.SprintLaunchButton
android:id="@+id/main_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd=" app:layout_constraintBottom_toBottomOf="@+id/thumbnail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thumbnail">
</com.netexlearning.play.view.SprintLaunchButton>

我能够在我的应用程序的主布局中使用和看到这个新创建的布局,但是在尝试添加 onClickClickListener 时问题开始出现。

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_detail_sprint, dataBindingComponent);

        mViewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(SprintDetailViewModel.class);

        mBinding.mainAction.setOnClickListener(view -> Timber.e("test");
}

永远不会调用此侦听器,即使我在 TextView 本身上添加了 onClickListener..

我必须更改我的自定义布局并删除 TextView 和 ImageView 并添加一个按钮,然后在我的 MainActivity class 中添加 onClickListener 以便能够像这样触发操作:

mBinding.mainAction.getButton.setOnClickListener(view -> Timber.e("test");

我的问题是..有没有办法在自定义约束布局上触发 onClickListener?。即使我添加了一个方法来检索我的 textView 或 imageView,onClickListener 也不起作用。

非常感谢,如果您有任何问题,请随时问我。

SprintLaunchButton 必须实现 View.OnClickListener 接口。而且我相信您必须从 class 您创建视图变量

开始