How to access to nested view using data binding and navigation component?

Viewed 1626

I'm using android data binding and navigation component. I have activity_member layout. This layout includes another layout:

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

and in app_bar_main layout include another layout:

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

In content_mainI want to put main host fragmnet for using navigation component:

  <?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>

This is the host fragment in navigation graph. I need to access recyclerView in java class but I can not:

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

and:

     <?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>

How to access to nested elements in this situation like recyclerView id here?

2 Answers

I think you should put the RecyclerView tag in the fragment layout and use DataBinding in the fragment class.

I will give you an example of how you can access views in your situation.

Let's say you have a String title inside class object myObject to pass from activity_member to app_bar_main and your object is defined like this in activity_member.

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

Now to pass string named title which is inside your myObject to app_bar_main, you will have to define the variable in app_bar_main like this

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

after doing so, you will be able to pass the title from where you had included app_bar_main in activity_member layout.

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

Also, now you will be able to access all views which are inside app_bar_main. Let's say you have a view with id myTextView

TextView textView = binding.appbar.myTextView;
Related