【问题标题】:Fragment Button is found by Activity's findViewById instead of Fragment's inflated view.findViewByIdFragment Button 由 Activity 的 findViewById 找到,而不是 Fragment 的膨胀 view.findViewById
【发布时间】:2020-06-18 20:05:38
【问题描述】:

我看到了这种奇怪的行为,找不到类似的东西。

所以我有一个父Activity,里面是一个Fragment,我通过include元素将它包含在父元素中,然后在父元素的onCreate中包含,创建Fragment并用这个@987654326替换它@ 布局(告诉我这是否正确?我使用的是FrameLayout,但后来切换到include 并为其定义了id)。

活动

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.sourcey.materiallogindemo.CustomerDetailActivity"
    tools:ignore="MergeRootFrame">

    <com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.appbar.MaterialToolbar />

    </com.google.android.material.appbar.AppBarLayout>

    <include
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/app_bar"
        app:layout_constraintBottom_toTopOf="@id/layout_bottom_bar"
        layout="@layout/fragment_customer_detail" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.bottomappbar.BottomAppBar>

        </com.google.android.material.bottomappbar.BottomAppBar>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

片段

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.sourcey.materiallogindemo.CustomerDetailFragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<!-- THIS IS THE CULPRIT -->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_update_position"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.button.MaterialButton />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sku_list"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.sourcey.materiallogindemo.CustomerDetailFragment"
        tools:listitem="@layout/fragment_s_k_u_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

Fragmentinflated 正确,但是当我在 onCreateView 内部执行此操作时

rootView.btn_update_position.setOnClickListener {
    // ... log something
}

然后按Button,它什么也没做?尽管大多数发现都导致了这个建议,我应该 inflate view 然后设置 onClickListener

我也试过做这些

rootView.findViewById<MaterialButton>(R.id.btn_update_position).setOnClickListener {
    // ... log something
}

val button = rootView.findViewById<MaterialButton>(R.id.btn_update_position)
button.setOnClickListener {
    // ... log something
}

但它们都不起作用。

我还在onViewCreated 中尝试了上述方法,看看我是否没有得到参考,但没有抛出错误,也没有反应。

唯一有效的就是这个

activity?.findViewById<MaterialButton>(R.id.btn_update_position)
    ?.setOnClickListener {
         // ... log something
}

我想了解为什么会发生这种情况?这可能是使用include Fragment 的问题吗?

注意我不是 android 专业人士,只是在其中做一些业余爱好,所以对它了解不多。

编辑如您所见,我在 Fragment 布局中有一个 RecyclerView,我正在扩展布局,然后设置其适配器项,这似乎与按钮相反。 p>

rootView.sku_list.adapter = Adapter()

【问题讨论】:

  • 我认为您应该将 FrameLayout 添加到 fragment_customer_detail.xml 中,这将是片段约束
  • 我有,但我想知道它们之间的区别是不是另一个不是布局?

标签: android android-fragments kotlin android-activity include


【解决方案1】:

我对你想在这里做什么感到有点困惑

首先,&lt;include&gt; 不会创建新视图,它只是将 xml 包含到父 xml 文件中,所以基本上它仍然处于活动状态,您需要活动才能找到 ViewById

第二,关于你的问题 FrameLayout 和&lt;include&gt; 有什么不同。

&lt;include&gt;就像我上面说的,它只是将xml文件添加到父文件中,主要用途是重复使用布局(你可以在任何地方包含它)。

使用 FrameLayout,来自官方文档:“FrameLayout 旨在阻止屏幕上的一个区域以显示单个项目”。例如:你希望你的布局有一个所有屏幕的页眉和页脚,只有中间部分发生变化,所以在中间放置一个框架布局,然后为每个屏幕加载不同的视图,因为这种灵活性框架布局通常用于显示片段(你可以谷歌如何使用框架布局了解更多细节)

【讨论】:

  • 对,所以如果我需要在片段中保留元素引用,我应该使用FrameLayout。我对膨胀感到困惑,因为我在膨胀 Fragment 中包含的布局,然后用 include 标签替换它
  • 我可能应该提到问题,我在Fragment 布局中也有一个RecyclerView,并且我正在设置它的适配器,因为我试图为按钮和它做的从视图中膨胀它似乎工作正常。即项目在Fragment 中加载并在屏幕上绘制
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 2014-12-23
  • 2018-04-14
相关资源
最近更新 更多