2017-05-02 16 views
0

我有RecyclerView,其中每個項目代表,CheckBox和EditText上 當複選框點擊EditText上的文字應該通過罷工, 我有ObservableBoolean這是article.complete 我用它在app:checkBoxChangeListener="@{article.complete}" app:itemComplete="@{article.complete}"綁定與RecyclerView,其中每個項目的CheckBox

它的工作,除非我滾動RecyclerView,然後在複選框另一個項目的文字點擊是通過

@BindingAdapter("itemComplete") 
public static void bindItemComplete(EditText itemInput, boolean complete){ 
itemInput.setPaintFlags(complete ? 
(itemInput.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG) : 0); 
} 

Article.java

罷工0
public class Article{ 
    public final ObservableBoolean complete = new ObservableBoolean(); 
} 

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

    <import type="android.view.View" /> 

    <variable 
     name="viewModel" 
     type="se.ica.handla.articles.ArticleListViewModel" /> 

    <variable 
     name="article" 
     type="se.ica.handla.models.articles.Article" /> 

</data> 

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical"> 

<EditText 
android:id="@+id/editText" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_marginBottom="8dp" 
app:itemComplete="@{article.complete}" 
/> 
<CheckBox 
android:id="@+id/checkBox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:checked="@={article.complete}" /> 
</android.support.constraint.ConstraintLayout> 
</layout> 
+0

所以..在'bindCheckBox '你想設置粒子屬性'完成',我說得對嗎?也許你可以通過['Two-way-Databinding']來實現這一點(http://stackoverflow.com/documentation/android/111/data-binding-library/6634/built-in-twoway-data-binding# T = 201705030857408356564)? 「數據綁定」對很多人都適用 - 這可能是您的錯誤。 ;) – yennsarah

+0

complete是ObservableBoolean對實現雙向綁定的方式,我在 app中使用完整的:checkBoxChangeListener =「@ {article.complete}」爲CheckBox 和app:itemComplete =「@ {article.complete}」in EditText上, 和它的作品的問題滾動 –

+0

你得到滾動時的錯誤,因爲你設置你的'complete'到'view'過程中出現 - 而不是到'article'('view.setTag()...') 。而且由於'RecyclerView'中的視圖被回收*,所以當您滾動時,這些值會被搞亂。我看到的最簡單的方法是使用「雙向數據綁定」。你爲什麼不想用它? – yennsarah

回答

0

寫在我的意見,我建議你使用雙向-數據綁定

您可以完全刪除該BindingAdapter

@BindingAdapter(value = {"checkBoxChangeListener", "article"}, requireAll = false) 
public static void bindCheckBox(CheckBox view, final ObservableBoolean checked, Article article) {  
     if (view.getTag(R.id.binded) == null) {  
      //Here you are setting the attributes to your *view* and 
      //decouple it from your article. It does not reference it, 
      //the properties (isChecked) isnow on the view.    
      //So when your view gets recycled when you scroll, 
      //it still has the property you set the last time - 
      //and not from your current article, which is displayed now in the view. 

      view.setTag(R.id.binded, true); 
      view.setOnCheckedChangeListener((buttonView, isChecked) -> checked.set(isChecked));} 
     } 
} 

正如你已經發現了,你的XML應該是現在這個樣子,採用雙向數據綁定:

<EditText 
android:id="@+id/editText" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_marginBottom="8dp" 
app:itemComplete="@{article.complete}" 
/> 

<CheckBox 
android:id="@+id/checkBox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:checked="@={article.complete}" //@={} for Two-Way Databinding 
/> 
+0

是的,我明白了,但在這個例子中的android:檢查=「@ = {} article.complete」 @ = {}可以作爲機器人:檢查是標準的屬性 但是當我想嘗試它的自定義屬性的應用程序:焦點我不能使用@ = {} 應用:重點=「@ {} article.focus」 和我有這樣的方法和滾動 @BindingAdapter時(「焦點」),我面臨着同樣的問題 公共靜態無效bindFocus(EditText itemInput,ObservableBoolean focus)if(focus.get()){itemIQueue.requestFocus(); } else { itemInput.clearFocus(); }} 您 –

+0

需要一個'InverseBindingAdapter'定製雙向數據綁定。您可以查看關於此主題的[George Mounts文章](https://medium.com/google-developers/android-data-binding-2-way-your-way-ccac20f6313)。 – yennsarah

相關問題