我对Android的Kotlin综合扩展感兴趣,并认为我们是否可以对自定义文件(例如我们保留在项目中的原始XML文件)执行相同的操作。例如,让我们考虑一下Kotlin中的综合视图。
import kotlinx.android.synthetic.main.fragment_profile_management.*
textview_shop_name.text = merchant.establishmentName
生成的代码是这样的:
TextView textView = (TextView) _$_findCachedViewById(com.goharsha.testapp.R.id.textview_shop_name);
Intrinsics.checkExpressionValueIsNotNull(textView, "textview_shop_name");
Merchant merchant3 = this.merchant;
if (merchant3 == null) {
Intrinsics.throwUninitializedPropertyAccessException("merchant");
}
textView.setText(merchant3.getEstablishmentName());
并且该_$_findCachedViewById
方法也被生成到相同的类中,如下所示:
private HashMap _$_findViewCache;
public View _$_findCachedViewById(int i) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View view = (View) this._$_findViewCache.get(Integer.valueOf(i));
if (view != null) {
return view;
}
View view2 = getView();
if (view2 == null) {
return null;
}
View findViewById = view2.findViewById(i);
this._$_findViewCache.put(Integer.valueOf(i), findViewById);
return findViewById;
}
由于这是特定于Android的,因此我猜想这可以在Kotlin中完成,也许我可以将其扩展为自定义的原始XML文件,例如配置文件,并将其解析为一个对象可能很有趣。
但是,我找不到该怎么办。我知道Kotlin中的扩展功能,但是在这里,将基于导入来合成整个文件。当我反编译该应用程序时,还存在找不到此Kotlin导入的魔力。
我还尝试查看core-ktx和view-ktx库,但到目前为止还没有运气。任何想法如何做到这一点?
是的,可以这样做,您需要做三件事才能完成此任务。
这些综合视图的生成就是按照这种方式完成的。如果您想尝试做类似的事情,这里有一些资源。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句