Epoxy学习

使用这个框架最主要就是学习如何创建我们的Models。

这个框架的设计思路就是创建Controller,Controller的作用就是用来安排我们的视图怎么展示,看他们的源码的时候在添加数据或者更新数据的时候,会调用buildModels方法。添加我们的视图模型,如果已经添加过了就不会添加到Controller的模型数组里面去。在Controller里面我们需要创建咱们的视图模型,也就是modelView,创建他的方式有三种。

  1. Custom Views
  2. DataBinding
  3. View Holders

创建模型视图:

From Custom Views

通过注解@ModelView

1
2
3
4
5
6
7
@ModelView(autoLayout = Size.MATCH_WIDTH_WRAP_HEIGHT)
public class HeaderView extends LinearLayout {
@TextProp
public void setTitle(CharSequence text) {
titleView.setText(text);
}
}

这个方式创建视图我还不是很懂


From DataBinding

1
2
3
4
5
6
7
8
9
10
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="title" type="String" />
</data>

<TextView
android:layout_width="120dp"
android:layout_height="40dp"
android:text="@{title}" />
</layout>

然后我们只需要随便创建一个文件,然后使用注解 @EpoxyDataBindingLayouts


From ViewHolders

1
2
3
4
5
6
7
8
9
10
11
12
13
@EpoxyModelClass(layout = R.layout.header_view)
public abstract class HeaderModel extends EpoxyModelWithHolder<Holder> {
@EpoxyAttribute String title;

@Override
public void bind(Holder holder) {
holder.header.setText(title);
}

static class Holder extends BaseEpoxyHolder {
@BindView(R.id.text) TextView header;
}
}