【发布时间】:2021-04-21 16:26:00
【问题描述】:
我有一个包含视图寻呼机和晶圆厂的布局。 视图寻呼机将片段作为页面。 我想要的是从片段内部访问 FAB。
fragment_user_anime_list(父)
<layout 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">
<data>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.destructo.sushi.ui.user.animeList.MyAnimeListFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/ToolBarWithNavigation"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/PopupMenu"
app:title="@string/my_anime_list"
app:titleTextAppearance="@style/TextAppearance.Sushi.H1" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/my_anime_list_tablayout"
style="@style/TabLayoutStyle"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:tabIndicator="@drawable/custom_tab_layout_indicator"
app:tabMode="scrollable" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/my_anime_list_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/random_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_normal"
android:layout_marginBottom="@dimen/margin_normal"
android:layout_gravity="bottom|right"
android:src="@drawable/ic_question_line"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
fragment_my_anime_list(子浏览器页面)
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/userAnimeRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/user_anime_list_pagination_progressbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/user_anime_list_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/userAnimeRecycler" />
<ProgressBar
android:id="@+id/user_anime_list_pagination_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
在子片段中
private lateinit var binding: FragmentUserAnimeListBinding
private lateinit var randomAnimeFab: FloatingActionButton
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentUserAnimeListBinding
.inflate(inflater, container, false).apply {
lifecycleOwner = viewLifecycleOwner
}
randomAnimeFab = binding.root.random_fab
return binding.root
}
这会引发空指针异常。视图不能为空
如何使用 viewBinding 获得对 fab 的引用?
我找到了一个不使用视图绑定的解决方案。不知道是不是最好的。 你得到 parentfragment 并直接引用它的视图。
val fab = requireParentFragment().random_fab
然后你可以检查它是否为空并做任何你想做的事情
fab?.let{
//Do something
}
如果有人知道如何使用 viewBinding 做到这一点,请告诉我。
【问题讨论】:
-
使用 binding.random_fab 代替 binding.root.random_fab
-
这样它只显示当前布局中的视图。不是父布局中的视图。
标签: android android-fragments android-databinding android-viewbinding