ViewBinding-不同的风味布局资源 [英] ViewBinding - Different Flavor Layout Resource

查看:219
本文介绍了ViewBinding-不同的风味布局资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的口味包含与main源集中相同的布局资源.这样一来,它就可以用特定的风味替代主要风味.我已经在很多地方做到了这一点,并且在我引入ViewBinding之前,它都可以正常工作.为了精确起见,该布局是一个嵌套的布局,它包含"在一个片段中.

I have a flavor that contains the same layout resource that is in my main source set. This is so it overrides the main one with the flavor specific one. I have done this in many places and it works fine until I introduced ViewBinding. For accuracy, the layout is a nested layout that is "included" in a fragment.

这里是结构:

main
 - res 
 - - layout 
 - - - layout_sign_up_details_fields.xml
flavor 
 - res 
 - - layout 
 - - - layout_sign_up_details_fields.xml

在我的风味特定的布局资源中,我有一个视图,其ID为til_sudf_first_name,而我的主资源中没有该视图.这是有意的.运行此构建变体时,通过查看生成的源代码,我可以看到此View在绑定类中是绑定"的.

I have a view, whose ID is til_sudf_first_name, in my flavor specific layout resource that I do not have in my main resource. This is intended. I can see this View is "bound" in my binding class by looking in the generated sources when running this build variant.

@NonNull
public final TextInputLayout tilSudfFirstName;

现在,实际的绑定发生在基"类中,并使用protected可见性修饰符与子类共享,如下所示:

Now the actual binding happens in a "base" class and is shared with child classes using the protected visibility modifier like so:

private var _binding : FragmentSignUpDetailsBinding? = null
protected val binding get() = _binding!!
protected val detailsBinding get() = binding.lSudfDetails

问题是detailsBinding不包含我需要的视图,即使该视图存在于生成的类中也是如此.

The problem is detailsBinding does not contain the view I require even though it's present in the generated class.

此外,另一个奇怪的事情是,即使main布局文件中的某些视图不在风味特定的布局文件中,但它们仍包含在生成的布局文件中.

In addition, the other strange thing is even though there are views in the main layout file that are not in the flavor specific layout file, they are still included in the generated layout file.

我想做的甚至有可能吗?我可以使用的解决方法是像往常一样对此实例使用kotlin的综合视图处理,但是我尝试使用推荐的方法

Is what I am trying to do even possible? The workaround available to me is to use kotlin's synthetic view handling for this instance as I have been but I was trying to use the recommended approach

推荐答案

诀窍是为片段和视图模型使用一个共享的程序包名称:

The trick is to use one shared package name for your fragments and viewmodels:

main
 - java
 - - MainFragment (package com.rahul.sample.<..>)
 - - MainViewModel (package com.rahul.sample.<..>)

flavor
 - java
 - - FlavorFragment (package com.rahul.sample.<..>)
 - - FlavorViewModel (package com.rahul.sample.<..>)

通过这种方式进行绑定.

This way the binding works.

这篇关于ViewBinding-不同的风味布局资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆