Android mvvm DataBinding踩坑 2018-02-08

1.配置 build.gradle 开启databinding

dataBinding {
        enabled = true
    }

2.xml新增layout data variable然后build回主界面 LayoutActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.layout_activity_main);



    
        

        
    

    

        

        

        

        
    


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.layout_activity_main);
        User user = new User("zn", "25","http://www.landleafservice.cn:8080/pics/icon_cloudy.png");
        Progress progress = new Progress(20, 0, 36, Color.GREEN, "优");
        activityMainBinding.setUser(user);
        activityMainBinding.setProgress(progress);
    }

    @BindingAdapter("imageUrl")
    public static void bindImage(ImageView imageView, String url){
        Log.d("ImageLoadAdaptersss", url);
        Picasso.with(imageView.getContext()).load(url).into(imageView);
    }
}

坑1变量引入


        
        
        

        
    

坑2设置默认值&使用引用value属性

android:text='@{yinchuan.voc+@string/tv_unit_mg,default="暂无"}'

坑3图片绑定

    //绑定一个下载图片的方法:需要图片URL和ImageView来显示,让这个方法能直接调用,最简单的就先定义为static  可放在Activity中或者新开一个class丢进去,会自动调用,照格式写就行
    @BindingAdapter({"imageUrl"})
    public static void loadImage(ImageView view, String url) {
        Glide.with(view.getContext()).load(url).into(view);
    }
    app:imageUrl='@{weatherModel.picUrl}'

坑4绑定自定义控件


坑5绑定点击事件

 

        binding.setOnClickListener(this);
    @Override
    public void onClick(View v) {
        switch (v.getId()) {}
    }

留作纪念。。。

你可能感兴趣的:(Android mvvm DataBinding踩坑 2018-02-08)