Android DataBinding:即使已生成,也无法访问LayoutNameDataBinding

问题描述 投票:0回答:1

我的问题是,我需要访问我的LayoutNameBinding,但无法访问。只有可用的DataBinding生成的类是LayoutNameBindingImpl。

  • 构建成功(无错误)
  • 在build.gradle(:app)中启用了数据绑定
  • DataBinding插件kotlin-apt在build.gradle(:app)中应用
  • 我的布局包含在<layout>
  • LobbyFragmentBinding.java生成并包含LobbyFragmentBinding

注释部分是我希望能够在我的代码中执行的操作。显示的部分代码受https://github.com/android/sunflower的启发,它解释了如何使用Android JetPack的不同部分。

尝试的解决方案

LobbyFragment.kt
package com.kotlin.projet3.ui.lobby

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import com.kotlin.projet3.R
import com.kotlin.projet3.adapters.GAME_LIST_INDEX
import com.kotlin.projet3.adapters.MATCH_HISTORY_INDEX
import com.kotlin.projet3.adapters.PROFILE_INDEX
import com.kotlin.projet3.databinding.LobbyFragmentBindingImpl

class LobbyFragment : Fragment(){
    private lateinit var nav: NavController

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

//        val binding = LobbyFragmentBinding.inflate(inflater,container,false)
//
//        viewPager = binding.viewPager
//        viewPager.adapter = LobbyTabsAdapter(this)
//
//        TabLayoutMediator(binding.tabs, viewPager) { tab, position ->
//            tab.setIcon(getTabIcon(position))
//            tab.text = getTabTitle(position)
//        }.attach()
//
//        return binding.root
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    private fun getTabIcon(position: Int): Int {
        return when (position) {
            GAME_LIST_INDEX -> R.drawable.ic_home_black_24dp
            PROFILE_INDEX -> R.drawable.ic_person_black_24dp
            MATCH_HISTORY_INDEX -> R.drawable.ic_assessment_black_24dp
            else -> throw IndexOutOfBoundsException()
        }
    }

    private fun getTabTitle(position: Int): String? {
        return when (position) {
            GAME_LIST_INDEX -> getString(R.string.home_button)
            PROFILE_INDEX -> getString(R.string.profile_button)
            MATCH_HISTORY_INDEX -> getString(R.string.match_history_button)
            else -> null
        }
    }
}

lobby_fragment.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"
    xmlns:tools="http://schemas.android.com/tools">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/page_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"


            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="parent">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/view_pager"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent=".15"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tabMode="fixed"
                app:tabGravity="fill"
                app:tabIndicatorFullWidth="true" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintTop_toBottomOf="@+id/tabs"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

LobbyFragmentBinding.java(生成的文件)
// Generated by data binding compiler. Do not edit!
package com.kotlin.projet3.databinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.kotlin.projet3.R;
import java.lang.Deprecated;
import java.lang.Object;

public abstract class LobbyFragmentBinding extends ViewDataBinding {
  @NonNull
  public final ConstraintLayout pageContainer;

  @NonNull
  public final TabLayout tabs;

  @NonNull
  public final ViewPager2 viewPager;

  protected LobbyFragmentBinding(Object _bindingComponent, View _root, int _localFieldCount,
      ConstraintLayout pageContainer, TabLayout tabs, ViewPager2 viewPager) {
    super(_bindingComponent, _root, _localFieldCount);
    this.pageContainer = pageContainer;
    this.tabs = tabs;
    this.viewPager = viewPager;
  }

  @NonNull
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot) {
    return inflate(inflater, root, attachToRoot, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.lobby_fragment, root, attachToRoot, component)
   */
  @NonNull
  @Deprecated
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot, @Nullable Object component) {
    return ViewDataBinding.<LobbyFragmentBinding>inflateInternal(inflater, R.layout.lobby_fragment, root, attachToRoot, component);
  }

  @NonNull
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.lobby_fragment, null, false, component)
   */
  @NonNull
  @Deprecated
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable Object component) {
    return ViewDataBinding.<LobbyFragmentBinding>inflateInternal(inflater, R.layout.lobby_fragment, null, false, component);
  }

  public static LobbyFragmentBinding bind(@NonNull View view) {
    return bind(view, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.bind(view, component)
   */
  @Deprecated
  public static LobbyFragmentBinding bind(@NonNull View view, @Nullable Object component) {
    return (LobbyFragmentBinding)bind(component, view, R.layout.lobby_fragment);
  }
}

我的问题是,我需要访问我的LayoutNameBinding,但无法访问。只有可用的DataBinding生成的类是LayoutNameBindingImpl。构建成功(无错误)...

android android-studio kotlin data-binding android-databinding
1个回答
0
投票

找到解决方案

© www.soinside.com 2019 - 2024. All rights reserved.