2015-05-26 46 views
6

我有一個佈局,其中包括我在同一子佈局多次,每一個具有不同的作用:使用多個<include />標籤佈局ButterKnife

<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> 

它的工作原理,如果我發現這個意見方法:

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現在,我希望我可以簡單地註釋每個視圖(顯然以下不工作,我可以看到爲什麼),然後再使用每個佈局包括它們注入根:

@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); 

做它用這種方式,雖然,使我這個錯誤在第二次注射:

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

我的問題是:有沒有在這種情況下使用ButterKnife的方法嗎?

回答

8

你可以使用某種類型的子容器是這樣的:

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); 

和throught這個類,你可以用途:

layout1.mEcoText.setText(... etc 
+0

以這種方式它有點挫敗使用標籤的目的,儘管... – Stephan

+0

我同意它不是那麼理想和自動,因爲它應該是,我只是找到一個解決方法你的問題。但是你仍然在編寫更少的代碼,更多的模塊化代碼,你仍然可以使用ButterKnifeZelezny插件(https://github.com/avast/android-butterknife-zelezny)來自動生成這個子容器。 – Budius

1

我的答案和Budius提出的一樣,我在ButterKnife的github repo上發現了一個相關問題。原作者TomazMartins

在MainActivity:

public MainActivity extends AppCompatActivity { 
    // 1. First, we declare the layout that was included as a View objects. 
    @BindView(R.id.layout_1) View layout_1; 
    @BindView(R.id.layout_2) View layout_2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // 2. In here, we bind the included layouts 
     ButterKnife.bind(this); 

     // 4. Then, we create objects of the type of the IncludedLayout. 
     //  In this example the layout reuse the same layout twice, so, there are two 
     //  IncludedLayouts. 
     IncludedLayout includedLayout_1 = new IncludedLayout(); 
     IncludedLayout includedLayout_2 = new IncludedLayout(); 

     // 5. We bind the elements of the included layouts. 
     ButerKnife.bind(includedLayout_1, layout_1); 
     ButerKnife.bind(includedLayout_2, layout_2); 

     // 6. And, finally, we use them. 
     includedLayout_1.displayed_text.setText( "Hello"); 
     includedLayout_2.displayed_text.setText( "Hey!"); 
    } 

    // 3. We create a static class that will be an container of the elements 
    //  of the included layout. In here we declare the components that 
    //  hold this. In this example, there is only one TextView. 
    static class IncludedLayout { 
     @BindView(R.id.displayed_text) TextView displayed_text; 
    } 
} 

的MainAcitvity的XML:

<!--...--> 
<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <include android:id="@+id/layout_1" layout="@layout/included_layout" /> 
     <include android:id="@+id/layout_2" layout="@layout/included_layout" /> 
</LinearLayout> 
<!--...--> 

被包含佈局的XML:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/displayed_text"/> 
</LinearLayout> 

這就是它!

當我運行它,雖然id是相同的,因爲我重用它,TextView中的文本是不同的。

相關問題