gpt4 book ai didi

android - 如何使用数据绑定(bind)和导航组件访问嵌套 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:17 25 4
gpt4 key购买 nike

我正在使用 android 数据绑定(bind)和导航组件。我有 activity_member 布局。此布局包括另一个布局:

    <include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/appbar" />

并且在 app_bar_main 布局中包含另一个布局:

        <include
android:id="@+id/member"
layout="@layout/content_main" />

content_main中,我想放置使用导航组件的主要主机fragmnet:

  <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.MembersPage.MemberActivity"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="500dp"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
tools:layout_editor_absoluteX="-29dp"
tools:layout_editor_absoluteY="215dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是导航图中的主机 fragment 。我需要在 java 类中访问 recyclerView 但我不能:

        binding.appbar.member.membersRecycler.setLayoutManager(new GridLayoutManager(this, 3));

和:

     <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MembersFragment"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/membersRecycler"
android:layout_width="400dp"
android:layout_height="wrap_content" />
</FrameLayout>

在这种情况下如何访问嵌套元素,比如这里的 recyclerView id?

最佳答案

我认为你应该将 RecyclerView 标记放在 fragment 布局中,并在 fragment 类中使用 DataBinding。

关于android - 如何使用数据绑定(bind)和导航组件访问嵌套 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56830726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com