java - Android Data binding issue Binding adapter call twice -
i have simple imageview in layout , have api gives me image url. integrate data binding in layout. after parsing api i'm setting model through line binding.setuserinfo(memberobj.getmemberdata());
now have binding adapter imgurl code written. custom binding adapter calls twice when activity start , after parsing api.
now want notify ui after api has been parsed.
here code of xml activity_main.xml:
<?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 class="mainbinding"> <variable name="userinfo" type="com.myapplication.retrofit.pojo.imgtest"/> </data> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.myapplication.retrofit.mainactivity"> <imageview android:layout_width="250dp" android:layout_height="250dp" android:id="@+id/imageview" app:image_url="@{userinfo.imgurl}" /> </relativelayout> </layout>
here pojo: imgtest.java
public class imgtest extends baseobservable { string imgurl; @bindable public string getimgurl() { return imgurl; } public void setimgurl(string imgurl) { this.imgurl = imgurl; notifypropertychanged(br.imgurl); } }
here custombinder.java
public class custombinders { private static final string tag = "custombinders"; @bindingadapter({"image_url"}) public static void loadimagewithoutprogressbar(imageview view, string imageurl){ log.d(tag, "before loadimagewithoutprogressbar: "+imageurl); log.d(tag, "after loadimagewithoutprogressbar: "+imageurl); picasso.with(view.getcontext()) .load(imageurl) .into(view); } }
here mainactivity.java
public class mainactivity extends appcompatactivity { private static final string tag = "mainactivity"; restmanager mmanager; mainbinding binding; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); binding = databindingutil.setcontentview(this,r.layout.activity_main); mmanager = new restmanager(); call<responsebody> getuserinfo = mmanager.getservice().getuserinfo("25","sell"); getuserinfo.enqueue(new callback<responsebody>() { @override public void onresponse(call<responsebody> call, response<responsebody> response) { int sc = response.code(); log.d(tag, "onresponse: "+sc); if(response.issuccessful()) { imgtest img = new imgtest(); try { string res = response.body().string(); jsonobject jobj = new jsonobject(res); jsonobject obj = jobj.getjsonobject("memberdata"); string imgurl = ""; imgurl = obj.getstring("prifile_picture"); img.setimgurl(imgurl); binding.setuserinfo(img); } catch (exception e) { e.printstacktrace(); } } } @override public void onfailure(call<responsebody> call, throwable t) { } }); } }
please let know how solve issue. in advance.
as ravi says in comments, working intended. after create initial binding, values should set values are. if haven't set set null.
you can change behavior explicitly telling binding not bind. add onrebindcallback
binding:
private onrebindcallback<activitymainbinding> delayrebindcallback = new onrebindcallback<activitymainbinding>() { @override public boolean onprebind(activitymainbinding binding) { return false; } }; // ... , after creating binding ... binding.addonrebindcallback(delayrebindcallback);
in onresponse (assuming on ui thread):
binding.removeonrebindcallback(delayrebindcallback); binding.setuserinfo(img); binding.executependingbindings();
if isn't on ui thread, have post executable run binding.executependingbindings()
on ui thread.
Comments
Post a Comment