前幾天寫了DataBind 不寫adapter和viewholder了在掘金上吸收了幾位大佬的建議,因此這篇文章主要是補充上文的,建議沒有看過的能夠考慮看一看。好了進入正文:android
因爲上次實現把layout和bean綁定一塊兒了這就致使了bean的複用性不好,並且當只是讓bean承擔實體的做用時 bean會有額外的職責,很不符合單一職責原則,感謝大佬們的提醒git
怎麼解決這個問題呢?個人解決方案很簡單 就是加了一個wrapper.簡單的來講,就是將實體注入wrapper,wrapper持有一個bean對象,那麼layout裏就能夠操做bean了,同時ui的交互也交給了wrapper,保證了bean類的純淨github
這裏因爲BindAdapterType接口不是很適合需求而後我就刪除了,取而代之的是LayoutWrapperbash
public interface LayoutWrapper<T> {
/**
* @param t 適應的目標
* @return this
*/
LayoutWrapper setTarget(T t);
/**
* @return 適應的目標
*/
T getTarget();
/**
* @return layoutId
*/
int getType();
/**
* @return layout中的 BR.id
*/
int getId();
}
複製代碼
實體架構
public class UserInfo{
private String name;
private String age;
private String avator;
//省略set/get
public UserInfo(String name, String age, String avator) {
this.name = name;
this.age = age;
this.avator = avator;
}
}
public class StudentInfo{
private String name;
private String age;
private String avator;
//省略set/get
public StudentInfo(String name, String age, String avator) {
this.name = name;
this.age = age;
this.avator = avator;
}
}
複製代碼
wrapperapp
//userItemWrapper
public class UserItemWrapper implements LayoutWrapper<UserInfo> {
private UserInfo userInfo;
@Override
public UserItemWrapper setTarget(UserInfo userInfo) {
this.userInfo = userInfo;
return this;
}
@Override
public UserInfo getTarget() {
return userInfo;
}
@Override
public int getType() {
return R.layout.user_item;
}
@Override
public int getId() {
return BR.user;
}
@BindingAdapter("avator")
public static void setUserInfoAvtor(ImageView imageView, String avator) {
Glide.with(imageView.getContext()).load(avator).into(imageView);
}
public void clickParent(View view) {
Toast.makeText(view.getContext(), "福步距", Toast.LENGTH_SHORT).show();
}
public void clickImage(View view) {
Toast.makeText(view.getContext(), "ImageView", Toast.LENGTH_SHORT).show();
}
}
//studentItemWrapper
public class StudentItemWrapper implements LayoutWrapper<StudentInfo> {
private StudentInfo studentInfo;
@Override
public StudentItemWrapper setTarget(StudentInfo studentInfo) {
this.studentInfo = studentInfo;
return this;
}
@Override
public StudentInfo getTarget() {
return studentInfo;
}
@Override
public int getType() {
return R.layout.student_item;
}
@Override
public int getId() {
return BR.student;
}
@BindingAdapter("avator")
public static void setUserInfoAvtor(ImageView imageView, String avator) {
Glide.with(imageView.getContext()).load(avator).into(imageView);
}
public void clickParent(View view) {
Toast.makeText(view.getContext(), "福步距", Toast.LENGTH_SHORT).show();
}
public void clickImage(View view) {
Toast.makeText(view.getContext(), "ImageView", Toast.LENGTH_SHORT).show();
}
}
複製代碼
layoutide
//user_item
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.google.simple.model.wrapper.UserItemWrapper"
/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{user.clickParent}"
android:padding="9dp">
<ImageView
android:id="@+id/avator"
android:layout_width="120dp"
android:layout_height="120dp"
android:onClick="@{user.clickImage}"
app:avator="@{user.target.avator}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@{user.target.name??`zhangsan`}"
android:textSize="14sp"
app:layout_constraintLeft_toRightOf="@id/avator"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/age"
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@{user.target.age}"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/avator"
app:layout_constraintLeft_toRightOf="@id/avator"
app:layout_constraintTop_toBottomOf="@id/name" />
</android.support.constraint.ConstraintLayout>
</layout>
//studentItem
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="student"
type="com.google.simple.model.wrapper.StudentItemWrapper" />
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{student.clickParent}"
android:padding="9dp">
<ImageView
android:id="@+id/student_avator"
android:layout_width="120dp"
android:layout_height="120dp"
android:onClick="@{student.clickImage}"
app:avator="@{student.target.avator}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/student_name"
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@{student.target.name??`zhangsan`}"
android:textColor="#f00"
android:textSize="14sp"
app:layout_constraintLeft_toRightOf="@id/student_avator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/student_age"
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@{student.target.age}"
android:textColor="#f00"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/student_avator"
app:layout_constraintLeft_toRightOf="@id/student_avator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/student_name" />
</android.support.constraint.ConstraintLayout>
</layout>
複製代碼
具體操做與上一篇相似(注意兩種方案天生支持多類型) 這裏有個代碼地址 simplepost
雖然咱們把adapter和viewholder省掉了 可是咱們多了一個LayoutWrapper,LayoutWrapper牽扯到具體的ui交互,也就是說不能經過註解來解決(儘管咱們能夠自定義註解將接口的代碼省去,可是並不能解決根本問題),那麼這麼作到底值不值得呢?我我的以爲 應該是值得的,首先adapter和Viewholder是兩個類吧,咱們用一個wrapper代替,其次wrapper配合databind+google新架構 數據刷新、設置等操做不用手寫了,這樣也就少寫不少代碼了。以上只是我的見解,若有建議歡迎評論 多謝指教 (0.0)ui