2016-07-07 74 views
5

我正在構建一個Android應用程序,我想根據點擊按鈕來切換某些視圖元素的可見性。我試圖用數據綁定來存檔它,而不是使用findViewById(),但是直到現在我發現的所有解決方案都不會在更改變量時更新佈局。使用Android數據綁定動態切換佈局元素的可見性。

這是我到目前爲止。 (我已經簡化了代碼,把重點放在這個問題)

Activicy.java

public class RecipeActivity extends AppCompatActivity { 
private Recipe recipe; 
private ActivityRecipeBinding binding; 
private RecipeBinderHelper rbhelper = new RecipeBinderHelper(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    recipe = intent.getParcelableExtra("recipe"); 
    binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    toolbar.setTitle(recipe.getName()); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    binding.recipeContent.setRecipe(recipe); 
    binding.recipeContent.setHelper(rbhelper); 

    binding.Button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //HERE I CHANGE THE VALUE OF THE VARIBLE 
      rbhelper.setPresentationViewVisible(false); 
      binding.notifyChange(); 
     } 
    }); 
} 
} 

Helper類

public class RecipeBinderHelper{ 
private Boolean presentationElementsVisible; 
private Boolean timerElementsVisible; 

public RecipeBinderHelper(){ 
    this.presentationElementsVisible = true; 
    this.timerElementsVisible = false; 
} 
public void setPresentationViewVisible(boolean presentationElementsVisible) { 
    this.presentationElementsVisible = presentationElementsVisible; 
} 
public Boolean getPresentationElementsVisible() { 
    return presentationElementsVisible; 
} 
//getters and setters for private Boolean timerElementsVisible; 
} 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<data> 
    <import type="android.view.View"/> 
    <variable 
     name="helper" 
     type="com.myapps.recipeApp.RecipeBinderHelper"/> 
    <variable 
     name="recipe" 
     type="com.myapps.recipeApp.Recipe"/> 
</data> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.myapp.recipeApp.RecipeActivity" 
    tools:showIn="@layout/activity_recipe"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/r_source" 
     android:textStyle="bold" 
     android:text="@{recipe.source}" 
     android:visibility="@{helper.presentationElementsVisible ? View.VISIBLE : View.GONE}" /> 
<!-- More TextViews here --> 
<!-- Button is located in parret layout --> 
</RelativeLayout> 
</layout> 

回答

3

我極力推薦閱讀喬治山的posts about Android data-binding,它們非常有用。


爲了解決我延伸助手類作爲BaseObservablethe documentation所述的問題。

Helper類

public class RecipeBinderHelper{ 
    private Boolean presentationElementsVisible; 
    private Boolean timerElementsVisible; 

    public RecipeBinderHelper(){ 
     this.presentationElementsVisible = true; 
     this.timerElementsVisible = false; 
    } 
    public void setPresentationViewVisible(boolean presentationElementsVisible) { 
     this.presentationElementsVisible = presentationElementsVisible; 
     //Notifying change in the setter. 
     notifyPropertyChanged(BR.presentationElementsVisible); 
    } 
    //assigning Bindable annotation to the getter 
    @Bindable 
    public Boolean getPresentationElementsVisible() { 
     return presentationElementsVisible; 
    } 
    //getters and setters for private Boolean timerElementsVisible; 
} 

在活動的binding.notifyChange();是不必要的,並且可以被去除。

當單擊該按鈕時,該應用程序現在會根據需要刪除TextView。


一個奇怪的事情是,Android的工作室(2.1.2,Ubuntu的)給了我一個Cannot resolve symbol 'BR'警告,但應用程序編譯和運行正常。