最近通过RoboBinding-album-sample-master开源项目学习RoboBinding来着,完后就像给ablum加个图片然后就看到了这篇文章:http://blog.csdn.net/fly_xiaoba/article/details/50298473
然后文章只有代码,然后就看到了文章中提到的源文档http://robobinding.github.io/RoboBinding/getting_started.zh.html#_%E5%8D%95%E5%90%91%E5%B1%9E%E6%80%A7%E7%BB%91%E5%AE%9A,果断是有的,讲解的也很清晰,再次复习一下。
1.首先是自定义控件
xml+java文件,你们懂得。
xml:
myimagelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
java文件:MyImageLayout.java
public class MyImageLayout extends LinearLayout
{
private ImageView image;
public MyImageLayout(Context context, AttributeSet attrs)
{
this(context, attrs, R.layout.myimagelayout);
}
protected MyImageLayout(Context context, AttributeSet attrs, int layoutId)
{
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(layoutId, this);
image = (ImageView) findViewById(R.id.image);
}
@SuppressLint("NewApi")
public void setImage(int drawable)
{
image.setImageResource(drawable);
}
}
2.将自定义控件中的属性进行简单单向属性绑定。
我看的挺迷糊。也想多了不少,琢磨了好一顿才明白过来,实质:就是一个java文件+两行代码的事
ImageBinding.java
@ViewBinding(simpleOneWayProperties = { "image" })
public class ImageBinding extends CustomViewBinding<MyImageLayout>
{
}
越简单越容易想多了,还总以为{}还得写点啥???官网为啥不踢呢,呵呵哒····想多了的节奏。
3.通过org.robobinding.binder.BinderFactoryBuilder注册视图绑定实现
这个也很简单
reusableBinderFactory = new BinderFactoryBuilder().add(new ImageBinding().forView(MyImageLayout.class)).add(new ViewBindingForView().extend(View.class)).build();
在你原来定义的基础上在add一个新的add( new ImageBinding().forView(MyImageLayout. class))就完事了
4.用的时候就真么简单,和普通的一样绑定了。
<com.mstarc.mvvm.model.MyImageLayout
android:layout_width="20dp"
android:layout_height="20dp"
bind:image="{image}"/>