How to use View Binding with Included Views?

只谈情不闲聊 提交于 2020-05-12 04:51:45

问题


View Binding got released with v3.6.

Docs: https://developer.android.com/topic/libraries/view-binding

My question is, does anyone know how to use view binding with included layouts?

Given Layout that includes another Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

I am trying to reference items inside of the item_header layout.

binder.my_header (<-- this just returns back the view)
binder.root (<-- this just returns back the root view)

Even if I add an id to the root of the item_header, such as id="@+id/parent_id" and try to reference that, I receive null pointer exceptions

binder.parentId (<-- I have access to views inside of the item_header, however, I receive exceptions. Says that "parentId" cannot be found)

How to reference the layout, item_header?


回答1:


Let's suppose that the layout in your question is activity_main.xml. The generated view binding class for it is ActivityMainBinding. Similarly, for item_header.xml, the generated view binding is ItemHeaderBinding.

If we pretend that item_header.xml has a TextView named @+id/foo, then you wind up with this chunk of Kotlin:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val mainBinding = ActivityMainBinding.inflate(layoutInflater)

    setContentView(mainBinding.root)

    mainBinding.myHeader.foo.text = "this is a test"
  }
}

So, the ActivityMainBinding object should have a property with the android:id that you gave to the <include>myHeader in this case. That should be an ItemHeaderBinding, as view binding appears to set up the nested binding object for the <include>. Since myHeader is an ItemHeaderBinding, you can then reference widgets on it just as you would if you directly inflated ItemHeaderBinding yourself.

Note that view binding appears to convert lower_snake_case into lowerCamelCase, so the my_header ID turns into myHeader in terms of the generated code.




回答2:


I have checked this problem in Java.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

item_header.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:background="@android:color/transparent">
    <TextView
        android:id="@+id/foo"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        binding.myHeader.foo.setText("this is a test");
    }
}}

I checked that it worked in my new project. I hope it will help.



来源:https://stackoverflow.com/questions/60405044/how-to-use-view-binding-with-included-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!