android - DataBinding visibility depending on a list size -


i having object using in data binding variable.

public class company {     private int id;     private string name;     private list<employee> employees;     private list<room> rooms; }   <data>      <variable          name="item"          type="com.blablabla.model.entity.company"/>  </data> 

want change visibility of view depending on list size (employees), if list null or empty - visibility gone, otherwise visible.

what have tried far:
1) setting visibility directly:

  android:visibility="@{item.employees.size() > 0 ? view.visible : view.gone}" 

in fact visibility gone.

of course imported

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

2) using bindingconverter:

@bindingconversion     public static int convertcollectiontovisibility(collection collection) {         log.d(tag, "collection: " + (collection == null ? "null" : collection.size()));         return collection == null || collection.isempty() ? view.gone : view.visible;     } 

and in layout:

android:visibility="@{item.employees}" 

here log shows, collection null. not

company company = new company(1, "company 1"); list<employee> employees = new arraylist<>();         employees.add(new employee(i, "jack", "floyd"));         company.setemployees(employees); mbinding.setitem(company); 

any thoughts?

after recreating sample program op found out, setting wrong variable.

sample:

public class mainactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         activitymainbinding binding =                  databindingutil.setcontentview(this, r.layout.activity_main);         company company1 = new company();         company company2 = new company();          arraylist<string> employee2 = new arraylist<>();         employee2.add("first");         company2.setemployees(employee2);          binding.setcompany1(company1);         binding.setcompany2(company2);     }      @bindingconversion     public static int listtovisibility(list list) {         //also works collection         return (list == null || list.isempty()) ? view.invisible : view.visible;     } } 

xml:

<layout>     <data>         <variable             name="company1"             type="com.may.amy.bindingconversiontest.company" />         <variable             name="company2"             type="com.may.amy.bindingconversiontest.company" />     </data>     <textview         android:text="company1 has employees"         android:visibility="@{company1.employees}"/>      <textview         android:text="company2 has employees"         android:visibility="@{company2.employees}"/> </layout> 

my preferred solution "visibility problem" use

observableint listvisibility = new observableint(view.gone); 

in viewmodel.

boolean visible = list.size() > 0 || showheaderifempty;  listvisibility .set(visible ? view.visible : view.gone);   

the advantage condition can depend on many factors instead of list size.

in xml:

android:visibility="@{listvisibility}" 

of course may need updated, or used observablelist.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -