android - 如何使用数据绑定和导航组件访问嵌套视图?
问题描述
我正在使用 android 数据绑定和导航组件。我有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>
这是导航图中的主机片段。我需要访问 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 标签放在片段布局中,并在片段类中使用 DataBinding。
推荐阅读
- java - 如何阻止用户通过后按关闭奖励广告
- python - 底图类型错误:“模块”对象不可调用
- ios - 绘制后如何使雕刻线的边缘具有纯色
- android - 仅在将 sourceSets 与其他 ABI 的库一起使用时支持 x86 ABI
- typescript - 推送通知后,Ionic 未导航到指定的 URL
- javascript - 在 HTML 中单击按钮时如何运行 JavaScript 文件?
- html - 如何使这个 div 完全自动大小?
- c# - Vector3.MoveTowards 将对象放在错误的位置
- perl - EmbPerl 中的 Foreach 循环
- c# - 如何使用 IIS 运行 Orchard Core