Android数据绑定问题绑定适配器调用两次

时间:2016-09-07 11:23:48

标签: java android data-binding android-databinding

我的布局中有简单的imageview,我有一个api,它给了我一个图像网址。我在我的布局中集成了数据绑定。现在解析了api之后,我通过这一行binding.setUserinfo(memberObj.getMemberdata());

设置了模型

现在我还有一个绑定适配器,其中编写了imgurl代码。现在,自定义绑定适配器在活动开始时和解析api后调用两次

现在我想在api成功解析后通知用户界面。

以下是我的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>

这是我的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);
    }
}

这是我的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);
    }

}

这是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) {

            }
        });
    }
}

请让我们知道如何解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:7)

正如拉维在评论中所说,它正在按预期运作。创建初始绑定后,应将值设置为原始值。如果您还没有设置任何内容,那么它们将被设置为空。

您可以通过明确告知绑定不绑定来更改行为。在绑定中添加OnRebindCallback

private OnRebindCallback<ActivityMainBinding> delayRebindCallback = 
        new OnRebindCallback<ActivityMainBinding>() {
    @Override
    public boolean onPreBind(ActivityMainBinding binding) {
        return false;
    }
};

// ... and then after creating the binding ...
binding.addOnRebindCallback(delayRebindCallback);

在你的onResponse中(假设它在UI线程上):

binding.removeOnRebindCallback(delayRebindCallback);
binding.setUserinfo(img);
binding.executePendingBindings();

如果它不在UI线程上,则必须发布可执行文件以在UI线程上运行binding.executePendingBindings()