RoboBinding学习二:ImageView及自定义控件实现

最近通过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}"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值