sourcecode

포함 태그를 사용한 Android 데이터 바인딩

copyscript 2022. 8. 7. 16:48
반응형

포함 태그를 사용한 Android 데이터 바인딩

업데이트 노트:

위의 는 릴리스 1.0-rc4에서 불필요한 변수가 필요한 문제가 수정되었기 때문에 올바르게 동작합니다.

첫 번째 질문:

설명서에 기재되어 있는 대로 실행해도 동작하지 않습니다.

main.xml:

<layout xmlns:andr...
    <data>
    </data>
       <include layout="@layout/buttons"></include>
....

buttons.xml:

<layout xmlns:andr...>
    <data>
    </data>
    <Button
        android:id="@+id/button"
        ...." />

MyActivity.java:

 ... binding = DataBindingUtil.inflate...
binding.button; ->cannot resolve symbol 'button'

버튼을 얻는 방법?

문제는 포함된 레이아웃이 데이터 바인딩된 레이아웃으로 간주되지 않는다는 것입니다.하나로 동작시키려면 변수를 전달해야 합니다.

buttons.xml:

<layout xmlns:andr...>
  <data>
    <variable name="foo" type="int"/>
  </data>
  <Button
    android:id="@+id/button"
    ...." />

main.xml:

<layout xmlns:andr...
...
   <include layout="@layout/buttons"
            android:id="@+id/buttons"
            app:foo="@{1}"/>
....

그런 다음 버튼 필드를 통해 버튼에 간접적으로 액세스할 수 있습니다.

MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.button

1.0-rc4(방금 출시)부터는 변수가 더 이상 필요하지 않습니다.다음과 같이 간소화할 수 있습니다.

buttons.xml:

<layout xmlns:andr...>
  <Button
    android:id="@+id/button"
    ...." />

main.xml:

<layout xmlns:andr...
...
   <include layout="@layout/buttons"
            android:id="@+id/buttons"/>
....

간단한 완전한 예

★★★★★★★★★★를 설정해 주세요.id하여 를 사용합니다.binding.includedLayout.anyView.

에서는, 「이러다」에의의 전달에 이 됩니다.<include코드에 포함된 뷰에 액세스합니다.

순서 1

합격하고 싶다, 가 있습니다.String레이아웃을 포함합니다.

String를 .String로로 합니다.TextView

<data>
    // declare fields
    <variable
        name="passedText"
        type="String"/>
</data>

<TextView
    android:id="@+id/textView"
    ...
    android:text="@{passedText}"/> //set field to your view.

순서 2

이 레이아웃을 상위 레이아웃에 포함합니다.포함 레이아웃에 a를 붙여서 바인딩 클래스에서 사용할 수 있도록 합니다.이제 String을 전달할 수 있습니다.passedTextyour <include

activity_main.xml

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

    <LinearLayout
        ..
        >

        <include
            android:id="@+id/includedLayout"
            layout="@layout/layout_common"
            app:passedText="@{@string/app_name}" // here we pass any String 
            />

    </LinearLayout>
</layout>
  • 바로 할 수 .binding.includedLayout.textView네 반에서.
  • 위와 같이 모든 변수를 포함된 레이아웃에 전달할 수 있습니다.

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.includedLayout.textView.setText("text");
    

주의: 두 레이아웃 모두 (부모 및 포함)binding layout로 포장되어 있습니다.<layout

포함 레이아웃의 ID를 설정하기만 하면 됩니다.

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

그리고나서

BUTTONSBINDING binding = yourMainBinding.layout;

BUTTONSBINDING ressyslog res/sys/syslog.xml

지금:

binding.button.setText("simple_Way");

에 바인드를 유효하게 하려면 , 다음과 같이 ID 를 추가하는 것만으로 실행할 수 있습니다.

<include
            android:id="@+id/loading"
            layout="@layout/loading_layout"
            bind:booleanVisibility="@{viewModel.showLoading}" />

여기서 또 다른 흥미로운 점은 다음과 같이 바인더의 Import된 레이아웃에 변수를 전달할 수 있다는 것입니다.

MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.setVariable(BR.varID, variable)

xml 파일에 빈 데이터 태그가 있는 것 같습니다.이것이 include layout 파일을 생성하지 않는 원인입니다.

 <data>

</data>

사용하지 않을 경우 이 태그를 삭제하면 문제가 해결됩니다.

덧붙이고 싶은 것은, 저도 같은 문제가 있었습니다.제 문제는 변수 이름이 ID 이름과 같은 제목이라는 것이었습니다.컴파일 오류는 없었습니다.(100% 문제가 있는지는 모르겠지만 프로젝트 청소도 하고 있습니다)

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

    <variable
        name="title"
        type="String" />
</data>
...
<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/title"...>
<androidx.appcompat.widget.AppCompatTextView>
...
</layout>

포함 레이아웃이 dataBinding 태그를 활성화했는지 확인만 하면 됩니다.

아래 코드는 다른 레이아웃에 포함된 레이아웃입니다.

<data>

    <variable
        name="backBinding"
        type="String" />

</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cl_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dimen_30"
    android:layout_marginTop="@dimen/dimen_30"
    android:padding="@dimen/dimen_2"
    app:layout_constraintStart_toStartOf="parent">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="@dimen/dimen_10"
        android:layout_height="@dimen/dimen_20"
        android:contentDescription="@string/back"
        android:src="@drawable/ic_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_back"
        style="@style/AidoFTTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dimen_10"
        android:text="@string/face_training"
        android:textSize="@dimen/text_20"
        app:layout_constraintBottom_toBottomOf="@id/iv_back"
        app:layout_constraintStart_toEndOf="@id/iv_back"
        app:layout_constraintTop_toTopOf="@id/iv_back" />

</androidx.constraintlayout.widget.ConstraintLayout>

여기에 나는 나의 메인 레이아웃에 포함시키고 있다.

<data>

    <variable
        name="viewModel"
        type="com.ingenDynamic.coreaidokotlin.viewModels.VideoCallViewModel" />

</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/aido_main_background"
    tools:context=".ui.aidoVideoCall.ContactActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/back_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_20"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cl_appBar">

        <include
            android:id="@+id/back"
            layout="@layout/app_back_layout"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

포함된 레이아웃에 직접 액세스할 수 있습니다.

binding.backLayout 입니다.setOnClickListener {finish() }

binding.back.tvBack.text = getText(R.string.video_call)

언급URL : https://stackoverflow.com/questions/32947440/android-data-binding-using-include-tag

반응형