使用多个< include/>ButterKnife布局中的标签 [英] use of multiple <include /> tags in layout with ButterKnife

查看:128
本文介绍了使用多个< include/>ButterKnife布局中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个版式,其中多次包含相同的子版式,每次都具有不同的角色:

I have a layout where I include the same sub-layout multiple times, each one with a different role:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <include
        android:id="@+id/settings_eco_seekarc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/settings_arc" />

    <include
        android:id="@+id/settings_comfort_seekarc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/settings_arc" />
</LinearLayout>

如果我以这种方式找到视图,它将起作用:

It works if I find the views in this way:

View eco = root.findViewById(R.id.settings_eco_seekarc);
mEcoSeekArc = (SeekArc) eco.findViewById(R.id.settings_seekarc);
mEcoLeaf = (ImageView) eco.findViewById(R.id.settings_leaf_img);
mEcoText = (TextView) eco.findViewById(R.id.settings_text);
View cmf = root.findViewById(R.id.settings_comfort_seekarc);
mComfortSeekArc = (SeekArc) cmf.findViewById(R.id.settings_seekarc);
mComfortLeaf = (ImageView) cmf.findViewById(R.id.settings_leaf_img);
mComfortText = (TextView) cmf.findViewById(R.id.settings_text);

我现在在我的项目中引入ButterKnife,希望我可以简单地注释每个视图(以下内容显然不起作用,我可以理解为什么),并稍后使用每个包含的布局根目录将它们注入:

I am introducing ButterKnife in my project now, and I hoped I could simply annotate each view (the following obviously doesn't work, and I can see why) and inject them later using each included layout root:

@InjectView(R.id.settings_seekarc)
SeekArc mEcoSeekArc;
@InjectView(R.id.settings_leaf_img)
ImageView mEcoLeaf;
@InjectView(R.id.settings_text)
TextView mEcoText;
@InjectView(R.id.settings_seekarc)
SeekArc mComfortSeekArc;
@InjectView(R.id.settings_leaf_img)
ImageView mComfortLeaf;
@InjectView(R.id.settings_text)
TextView mComfortText;

//then later...
View eco = root.findViewById(R.id.settings_eco_seekarc);
ButterKnife.inject(this, eco);
View cmf = root.findViewById(R.id.settings_comfort_seekarc);
ButterKnife.inject(this, cmf);

以这种方式进行操作,却导致我在第二次注射时出现此错误:

Doing it in this way, though, leads me to this error at the second injection:

错误:(81,13)错误:尝试对已使用@InjectView的对象在'mEcoSeekArc'上注入了ID 2131493185.

Error:(81, 13) error: Attempt to use @InjectView for an already injected ID 2131493185 on 'mEcoSeekArc'.

我的问题是:在这种情况下是否可以使用ButterKnife?

My question is: is there a way to use ButterKnife in this scenario?

推荐答案

您可以使用以下类型的子容器:

you could use some type of sub-container like this:

public static class SettingsArcLayout {
  @InjectView(R.id.settings_text) public TextView mEcoText;
  @InjectView(R.id.settings_leaf_img) public ImageView mComfortLeaf;
  // etc...
}

那你就有了

SettingsArcLayout layout1 = new SettingsArcLayout();
SettingsArcLayout layout2 = new SettingsArcLayout();

然后:

ButterKnife.inject(this); // inject eco and cmf
ButterKnife.inject(layout1, eco);
ButterKnife.inject(layout2, cmf);

在本课程中,您可以使用:

and throught this class you can use:

layout1.mEcoText.setText(... etc

这篇关于使用多个&lt; include/&gt;ButterKnife布局中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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