首页 > 解决方案 > 如何使用 ViewBinding 从父布局访问视图?

问题描述

我有一个包含视图寻呼机和晶圆厂的布局。视图寻呼机将片段作为页面。我想要的是从片段内部访问 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 让我知道。

标签: androidandroid-fragmentsandroid-databindingandroid-viewbinding

解决方案


如何使用 viewBinding 获得对 fab 的引用?

现在您想通过数据绑定从页面/片段FAB之一访问。ViewPager

通常应在其生命周期所有者(即其活动/片段)内访问视图,因此父片段(承载 FAB 和 ViewPager)的视图应由该片段而不是页面片段触及。

您使用requireParentFragment().

val fab = requireParentFragment().random_fab

但是FAB,您可以在父片段中创建一个方法并从页面访问此方法,而不是直接从页面引用。并在此方法上对 FAB 进行更改。

并且通过这种方法,您可以在页面和页面之间传递数据,parentFragment而无需从另一个片段中触摸片段的视图。

所以你可以在父片段中有一个方法:

fun changeMyFab() {
    binding.random_fab ...
}

requireParentFragment().changeMyFab()从页面调用,您可以传递一些数据作为方法参数来更改 FAB 行为。


推荐阅读