【问题标题】:How to access to nested view using data binding and navigation component?如何使用数据绑定和导航组件访问嵌套视图?
【发布时间】:2023-04-02 12:28:01
【问题描述】:

我正在使用 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?

【问题讨论】:

    标签: android data-binding android-jetpack


    【解决方案1】:

    我认为你应该把 RecyclerView 标签放在片段布局中,并在片段类中使用 DataBinding。

    【讨论】:

      【解决方案2】:

      我会给你一个例子,说明如何在你的情况下访问视图。

      假设您在类对象myObject 中有一个字符串titleactivity_member 传递到app_bar_main,并且您的对象在activity_member 中定义如下。

       <data>
          <variable
            name="myObject"
            type="com.example.model.Object"/>
        </data>
      

      现在要将myObject 中名为title 的字符串传递给app_bar_main,您必须在app_bar_main 中定义变量 像这样

       <data>
          <variable
            name="title"
            type="String"/>
        </data>
      

      这样做之后,您将能够传递您在 activity_member 布局中包含 app_bar_main 的标题。

       <include
          layout="@layout/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/appbar" 
          app:title = "@{myObject.title}"/>
      

      此外,现在您将能够访问app_bar_main 中的所有视图。假设您有一个 ID 为 myTextView

      的视图
      TextView textView = binding.appbar.myTextView;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-31
        • 2020-08-23
        • 1970-01-01
        • 2020-08-21
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多