【发布时间】: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>
Fragment 是 inflated 正确,但是当我在 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