【问题标题】:Include TAG with click listener在点击监听器中包含 TAG
【发布时间】:2019-02-12 19:39:36
【问题描述】:

我有一个带有两个包含外部布局的片段:

        <include
        android:id="@+id/buttonSelectDate"
        layout="@layout/content_dateinstall_datebutton"
        android:layout_width="141dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginStart="38dp"
        android:layout_marginTop="35.9dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtInsertPhoneNumber"/>

    <include
        android:id="@+id/buttonSelectPeriod"
        layout="@layout/content_dateinstall_periodbutton"
        android:layout_width="141dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginEnd="38dp"
        android:layout_marginTop="35.9dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtInsertPhoneNumber"/>

我想设置 OnClick 方法,因为我将使用该布局作为按钮。

当我设置 View 的 onclicklistener 时,应用程序在运行时崩溃。我一直在尝试通过 ButterKnife 与 @BindView 和 @OnClick 绑定 onClick 并以常见的方式创建对象并设置 onClickListener。

有人知道我该怎么做吗?

这是我的按钮布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable name="orderViewModel" type="com.oi.pap.viewmodels.OrderViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/linearLayout2"
    android:layout_width="141dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/custom_border_button">

    <TextView
        style="@style/TextAppearance.simplonRegular"
        android:id="@+id/txtSelectPeriod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/all_period"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="11dp"
        android:textSize="12sp"
        android:textColor="@color/lipstick"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        style="@style/TextAppearance.simplonHeadline"
        android:id="@+id/txtSelectPeriodValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@={orderViewModel.periodInstallValue}"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="11dp"
        android:layout_marginBottom="10dp"
        android:textSize="14sp"
        android:textColor="@color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtSelectPeriod"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 发布错误日志。

标签: android button include click fragment


【解决方案1】:

据我所知,您尝试从主布局访问包含布局并且您正在使用 MVVM 方法,当您尝试访问内部包含布局时的主要问题是它无法获取已声明的数据,所以您还必须在主布局中声明它,然后将其传递给包含布局,例如: 在我的 LoginLayout.xml 中:

<data>
    <variable
        name="activityLogin"
        type="...LoginViewModel"/>
</data>

在这里,我想从活动登录中获取数据访问权限,然后包括:

<include
    ....
    bind:contentLogin="@{activityLogin}"
    ..../>

在内容登录中:

<data>
        <variable
            name="contentLogin"
            type="...LoginViewModel"/>
    </data>

那么,您就可以通过点击获取数据了

【讨论】:

  • 我只有一个按活动划分的 ViewModel。这些片段使用的是她父亲的同一个 viewModel。
  • 但如果您在布局中使用 include,我认为您可能需要将 bind 放入其中以访问您的其他布局,如果您直接使用您的布局,我认为它不会识别 @ 987654325@,因为你没有膨胀content_dateinstall_periodbutton 这就是你出错的原因。您的 content_dateinstall_periodbuttonlayout.fragment_dateinstall_home 内的孩子,因此您需要将值从父母传递给您的孩子
【解决方案2】:

我找到了。

private FragmentDateinstallHomeBinding dataBinding;
dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_dateinstall_home, 
container, false);
View view = dataBinding.getRoot();
//here
dataBinding.buttonSelectDate.dateInstallButtonLayout.setOnClickListener(tst);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多