Commit 2797147cda006b2e3f42b23efe3da34a2e3a40a2

Authored by 朱显松
1 parent cfb6f42e

The DataBinding that adds the RecyclerView Adapter supports and stores ViewState

app/build.gradle
@@ -41,6 +41,6 @@ dependencies { @@ -41,6 +41,6 @@ dependencies {
41 testCompile 'junit:junit:4.12' 41 testCompile 'junit:junit:4.12'
42 // TODO 本地酷使用说明 代码仓库标识 42 // TODO 本地酷使用说明 代码仓库标识
43 // compile 'com.cnlive.libs:base:1.0.0@aar' 43 // compile 'com.cnlive.libs:base:1.0.0@aar'
44 - compile 'com.cnlive:cnlive:0.2.0'  
45 -// compile project(':cnlive') 44 +// compile 'com.cnlive:cnlive:0.2.0'
  45 + compile project(':cnlive')
46 } 46 }
app/src/main/java/com/cnlive/libs/demo/MainActivity.java
1 package com.cnlive.libs.demo; 1 package com.cnlive.libs.demo;
2 2
3 -import android.databinding.DataBindingUtil;  
4 import android.os.Bundle; 3 import android.os.Bundle;
5 import android.support.v7.app.AppCompatActivity; 4 import android.support.v7.app.AppCompatActivity;
6 -import android.util.Log;  
7 -import android.view.View;  
8 -import android.widget.TextView;  
9 -  
10 -import com.cnlive.libs.demo.databinding.ActivityMainBinding;  
11 5
12 public class MainActivity extends AppCompatActivity { 6 public class MainActivity extends AppCompatActivity {
13 7
14 - private MainPageData data = new MainPageData();  
15 -  
16 @Override 8 @Override
17 protected void onCreate(Bundle savedInstanceState) { 9 protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState); 10 super.onCreate(savedInstanceState);
19 -  
20 - ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
21 - binding.setData(data);  
22 - //Toast 测试  
23 - ToastTest.test(this);  
24 - Java8Test.test(this);  
25 } 11 }
26 12
27 - protected void onAddItemClick(View view){  
28 - int id = data.getList().size();  
29 - data.getList().add(new MainListItemData(""+id));  
30 - }  
31 -  
32 - protected void onMainItemClick(View view) {  
33 - TextView itemView = (TextView) view;  
34 - Log.e("DemoView", "view ~" + itemView.getText());  
35 - }  
36 } 13 }
app/src/main/java/com/cnlive/libs/demo/MainAdapter.java deleted 100644 → 0
1 -package com.cnlive.libs.demo;  
2 -  
3 -import com.cnlive.libs.demo.databinding.ListitemMainBinding;  
4 -  
5 -import java.util.List;  
6 -  
7 -/**  
8 - * Created by xiansong on 2017/8/30.  
9 - */  
10 -  
11 -public class MainAdapter extends DataBindingRecyclerAdapter<MainListItemData, ListitemMainBinding> {  
12 -  
13 - private List<MainListItemData> list;  
14 -  
15 - public MainAdapter(List<MainListItemData> list){  
16 - this.list = list;  
17 - }  
18 -  
19 - @Override  
20 - public int getLayoutId() {  
21 - return R.layout.listitem_main;  
22 - }  
23 -  
24 - @Override  
25 - public List<MainListItemData> getList() {  
26 - return list;  
27 - }  
28 -}  
app/src/main/java/com/cnlive/libs/demo/MainPageData.java deleted 100644 → 0
1 -package com.cnlive.libs.demo;  
2 -  
3 -import android.databinding.BaseObservable;  
4 -import android.databinding.Bindable;  
5 -import android.support.v7.widget.LinearLayoutManager;  
6 -  
7 -import com.cnlive.libs.demo.databinding.ListitemMainBinding;  
8 -  
9 -import java.util.Arrays;  
10 -import java.util.List;  
11 -  
12 -/**  
13 - * Created by xiansong on 2017/8/30.  
14 - */  
15 -  
16 -public class MainPageData extends BaseObservable {  
17 -  
18 - private List<MainListItemData> list = Arrays.asList(  
19 - new MainListItemData("Title 1"),  
20 - new MainListItemData("Title 2"),  
21 - new MainListItemData("Title 3"),  
22 - new MainListItemData("Title 4"),  
23 - new MainListItemData("Title 5"));  
24 -  
25 - private DataBindingRecyclerAdapter adapter = new DataBindingRecyclerAdapter<MainListItemData, ListitemMainBinding>() {  
26 - @Override  
27 - public int getLayoutId() {  
28 - return R.layout.listitem_main;  
29 - }  
30 -  
31 - @Override  
32 - public List<MainListItemData> getList() {  
33 - return list;  
34 - }  
35 - };  
36 -  
37 - private LinearLayoutManager manager = new LinearLayoutManager(null);  
38 -  
39 - public List<MainListItemData> getList() {  
40 - return list;  
41 - }  
42 -  
43 - public void setList(List<MainListItemData> list) {  
44 - this.list = list;  
45 - }  
46 -  
47 - @Bindable  
48 - public DataBindingRecyclerAdapter getAdapter() {  
49 - return adapter;  
50 - }  
51 -  
52 - public void setAdapter(DataBindingRecyclerAdapter adapter) {  
53 - this.adapter = adapter;  
54 - notifyPropertyChanged(BR.adapter);  
55 - }  
56 -  
57 - @Bindable  
58 - public LinearLayoutManager getManager() {  
59 - return manager;  
60 - }  
61 -  
62 - public void setManager(LinearLayoutManager manager) {  
63 - this.manager = manager;  
64 - notifyPropertyChanged(BR.manager);  
65 - }  
66 -}  
app/src/main/java/com/cnlive/libs/demo/ToastTest.java
@@ -23,8 +23,8 @@ public class ToastTest { @@ -23,8 +23,8 @@ public class ToastTest {
23 .setView(R.layout.layout_toast_1, R.id.text); 23 .setView(R.layout.layout_toast_1, R.id.text);
24 24
25 25
26 - public static void test(Context context) { 26 + public static void showMainToast(Context context,String message) {
27 //Toast 调用示例 27 //Toast 调用示例
28 - mainToast.build(context).showToast("Start ~~~"); 28 + mainToast.build(context).showToast(message);
29 } 29 }
30 } 30 }
app/src/main/java/com/cnlive/libs/demo/MainListItemData.java renamed to app/src/main/java/com/cnlive/libs/demo/mvptest/model/LaunchListItemData.java
1 -package com.cnlive.libs.demo; 1 +package com.cnlive.libs.demo.mvptest.model;
2 2
3 import android.databinding.BaseObservable; 3 import android.databinding.BaseObservable;
4 import android.databinding.Bindable; 4 import android.databinding.Bindable;
  5 +import android.os.Parcel;
  6 +import android.os.Parcelable;
  7 +
  8 +import com.cnlive.libs.demo.BR;
5 9
6 /** 10 /**
7 * Created by xiansong on 2017/8/30. 11 * Created by xiansong on 2017/8/30.
8 */ 12 */
9 13
10 -public class MainListItemData extends BaseObservable { 14 +public class LaunchListItemData extends BaseObservable implements Parcelable {
11 private String name; 15 private String name;
12 16
13 - public MainListItemData(String name ){ 17 + public LaunchListItemData(String name ){
14 this.name = name; 18 this.name = name;
15 } 19 }
16 20
  21 + protected LaunchListItemData(Parcel in) {
  22 + name = in.readString();
  23 + }
  24 +
  25 + @Override
  26 + public void writeToParcel(Parcel dest, int flags) {
  27 + dest.writeString(name);
  28 + }
  29 +
  30 + @Override
  31 + public int describeContents() {
  32 + return 0;
  33 + }
  34 +
  35 + public static final Creator<LaunchListItemData> CREATOR = new Creator<LaunchListItemData>() {
  36 + @Override
  37 + public LaunchListItemData createFromParcel(Parcel in) {
  38 + return new LaunchListItemData(in);
  39 + }
  40 +
  41 + @Override
  42 + public LaunchListItemData[] newArray(int size) {
  43 + return new LaunchListItemData[size];
  44 + }
  45 + };
  46 +
17 @Bindable 47 @Bindable
18 public String getName() { 48 public String getName() {
19 return name; 49 return name;
app/src/main/java/com/cnlive/libs/demo/mvptest/model/LaunchPage.java
1 package com.cnlive.libs.demo.mvptest.model; 1 package com.cnlive.libs.demo.mvptest.model;
2 2
3 -import android.databinding.BaseObservable;  
4 import android.databinding.Bindable; 3 import android.databinding.Bindable;
5 import android.os.Parcel; 4 import android.os.Parcel;
6 -import android.os.Parcelable; 5 +import android.support.v7.widget.LinearLayoutManager;
  6 +import android.support.v7.widget.RecyclerView;
7 import android.view.View; 7 import android.view.View;
8 8
  9 +import com.cnlive.libs.base.frame.model.ViewStateListData;
9 import com.cnlive.libs.demo.BR; 10 import com.cnlive.libs.demo.BR;
  11 +import com.cnlive.libs.demo.R;
  12 +import com.cnlive.libs.demo.databinding.ListitemMainBinding;
10 13
11 -public class LaunchPage extends BaseObservable implements Parcelable { 14 +public class LaunchPage extends ViewStateListData<LaunchListItemData,ListitemMainBinding> {
12 private long time = -1; 15 private long time = -1;
13 private int timing = View.GONE; 16 private int timing = View.GONE;
14 private int loading = View.GONE; 17 private int loading = View.GONE;
15 18
  19 + @Override
  20 + public int getLayoutId() {
  21 + return R.layout.listitem_main;
  22 + }
  23 +
  24 + @Override
  25 + public RecyclerView.LayoutManager getLayoutManager() {
  26 + return new LinearLayoutManager(null);
  27 + }
  28 +
16 @Bindable 29 @Bindable
17 public long getTime() { 30 public long getTime() {
18 return time; 31 return time;
@@ -45,26 +58,25 @@ public class LaunchPage extends BaseObservable implements Parcelable { @@ -45,26 +58,25 @@ public class LaunchPage extends BaseObservable implements Parcelable {
45 } 58 }
46 59
47 @Override 60 @Override
48 - public int describeContents() {  
49 - return 0;  
50 - }  
51 -  
52 - @Override  
53 public void writeToParcel(Parcel parcel, int i) { 61 public void writeToParcel(Parcel parcel, int i) {
  62 + super.writeToParcel(parcel, i);
54 parcel.writeLong(this.time); 63 parcel.writeLong(this.time);
55 parcel.writeInt(this.timing); 64 parcel.writeInt(this.timing);
56 parcel.writeInt(this.loading); 65 parcel.writeInt(this.loading);
57 } 66 }
58 67
  68 + @Override
  69 + public void createFromParcel(Parcel in) {
  70 + super.createFromParcel(in);
  71 + this.time = in.readLong();
  72 + this.timing = in.readInt();
  73 + this.loading = in.readInt();
  74 + }
59 75
60 - public static final Creator<LaunchPage> CREATOR = new Creator<LaunchPage>() { 76 + public static final Creator<LaunchPage> CREATOR = new Creator<LaunchPage>() {
61 @Override 77 @Override
62 public LaunchPage createFromParcel(Parcel in) { 78 public LaunchPage createFromParcel(Parcel in) {
63 - LaunchPage target = new LaunchPage();  
64 - target.time = in.readLong();  
65 - target.timing = in.readInt();  
66 - target.loading = in.readInt();  
67 - return target; 79 + return ViewStateListData.newInstance(LaunchPage.class,in);
68 } 80 }
69 81
70 @Override 82 @Override
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/LaunchActivity.java
@@ -6,7 +6,9 @@ import android.os.Bundle; @@ -6,7 +6,9 @@ import android.os.Bundle;
6 import android.view.View; 6 import android.view.View;
7 7
8 import com.cnlive.libs.base.frame.activity.MvpViewStateBindingActivity; 8 import com.cnlive.libs.base.frame.activity.MvpViewStateBindingActivity;
  9 +import com.cnlive.libs.demo.mvptest.model.LaunchListItemData;
9 import com.cnlive.libs.demo.R; 10 import com.cnlive.libs.demo.R;
  11 +import com.cnlive.libs.demo.ToastTest;
10 import com.cnlive.libs.demo.databinding.ActivityLaunchBinding; 12 import com.cnlive.libs.demo.databinding.ActivityLaunchBinding;
11 import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter; 13 import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter;
12 import com.cnlive.libs.demo.mvptest.frame.view.LaunchView; 14 import com.cnlive.libs.demo.mvptest.frame.view.LaunchView;
@@ -25,6 +27,11 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun @@ -25,6 +27,11 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
25 } 27 }
26 28
27 @Override 29 @Override
  30 + protected void onPostCreate(Bundle savedInstanceState) {
  31 + super.onPostCreate(savedInstanceState);
  32 + }
  33 +
  34 + @Override
28 protected void onNewIntent(Intent intent) { 35 protected void onNewIntent(Intent intent) {
29 super.onNewIntent(intent); 36 super.onNewIntent(intent);
30 } 37 }
@@ -48,4 +55,14 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun @@ -48,4 +55,14 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
48 public void onNewViewStateInstance() { 55 public void onNewViewStateInstance() {
49 getPresenter().initData(this); 56 getPresenter().initData(this);
50 } 57 }
  58 +
  59 + private int index = 1;
  60 + public void onAddItemClick(View view){
  61 + getViewData().getAdapter().addItem(new LaunchListItemData(""+index++));
  62 + getViewData().setTime(getViewData().getAdapter().getItemCount());
  63 + }
  64 +
  65 + public void onMainItemClick(View view) {
  66 + ToastTest.showMainToast(this,((LaunchListItemData)view.getTag()).getName());
  67 + }
51 } 68 }
app/src/main/res/layout/activity_launch.xml
@@ -16,6 +16,21 @@ @@ -16,6 +16,21 @@
16 android:background="#FFc2c4f6" 16 android:background="#FFc2c4f6"
17 tools:context=".mvptest.ui.activity.LaunchActivity"> 17 tools:context=".mvptest.ui.activity.LaunchActivity">
18 18
  19 + <Button
  20 + android:layout_width="368dp"
  21 + android:layout_height="wrap_content"
  22 + android:layout_marginBottom="8dp"
  23 + android:layout_marginLeft="8dp"
  24 + android:layout_marginRight="8dp"
  25 + android:layout_marginTop="8dp"
  26 + android:onClick="onAddItemClick"
  27 + android:text="Add Item"
  28 + app:layout_constraintBottom_toBottomOf="parent"
  29 + app:layout_constraintHorizontal_bias="0.0"
  30 + app:layout_constraintLeft_toLeftOf="parent"
  31 + app:layout_constraintRight_toRightOf="parent"
  32 + app:layout_constraintVertical_bias="0.013"
  33 + app:layout_constraintTop_toBottomOf="@+id/textView2" />
19 <ProgressBar 34 <ProgressBar
20 android:id="@+id/progressBar" 35 android:id="@+id/progressBar"
21 android:layout_width="32dp" 36 android:layout_width="32dp"
@@ -38,7 +53,8 @@ @@ -38,7 +53,8 @@
38 android:visibility="@{data.timing}" 53 android:visibility="@{data.timing}"
39 app:layout_constraintRight_toRightOf="parent" 54 app:layout_constraintRight_toRightOf="parent"
40 app:layout_constraintTop_toTopOf="parent" 55 app:layout_constraintTop_toTopOf="parent"
41 - tools:text="0" /> 56 + tools:text="0"
  57 + android:id="@+id/textView2" />
42 58
43 <View 59 <View
44 android:id="@+id/click" 60 android:id="@+id/click"
@@ -51,6 +67,27 @@ @@ -51,6 +67,27 @@
51 app:layout_constraintBottom_toBottomOf="parent" 67 app:layout_constraintBottom_toBottomOf="parent"
52 app:layout_constraintLeft_toLeftOf="parent" 68 app:layout_constraintLeft_toLeftOf="parent"
53 app:layout_constraintRight_toRightOf="parent" /> 69 app:layout_constraintRight_toRightOf="parent" />
  70 +
  71 + <android.support.v7.widget.RecyclerView
  72 + android:id="@+id/recyclerView"
  73 + android:layout_width="368dp"
  74 + android:layout_height="200dp"
  75 + android:layout_marginBottom="8dp"
  76 + android:layout_marginEnd="8dp"
  77 + android:layout_marginLeft="8dp"
  78 + android:layout_marginRight="8dp"
  79 + android:layout_marginStart="8dp"
  80 + android:layout_marginTop="8dp"
  81 + app:layout_constraintBottom_toBottomOf="parent"
  82 + app:layout_constraintHorizontal_bias="0.0"
  83 + app:layout_constraintLeft_toLeftOf="parent"
  84 + app:layout_constraintRight_toRightOf="parent"
  85 + app:layout_constraintTop_toTopOf="parent"
  86 + app:layout_constraintVertical_bias="1.0"
  87 +
  88 +
  89 + app:adapter="@{data.adapter}"
  90 + app:layoutManager="@{data.manager}"/>
54 </android.support.constraint.ConstraintLayout> 91 </android.support.constraint.ConstraintLayout>
55 92
56 </layout> 93 </layout>
app/src/main/res/layout/activity_main.xml
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 7
8 <variable 8 <variable
9 name="data" 9 name="data"
10 - type="com.cnlive.libs.demo.MainPageData" /> 10 + type="String" />
11 </data> 11 </data>
12 12
13 <android.support.constraint.ConstraintLayout 13 <android.support.constraint.ConstraintLayout
@@ -41,25 +41,6 @@ @@ -41,25 +41,6 @@
41 app:layout_constraintRight_toRightOf="parent" 41 app:layout_constraintRight_toRightOf="parent"
42 app:layout_constraintTop_toTopOf="parent" /> 42 app:layout_constraintTop_toTopOf="parent" />
43 43
44 - <android.support.v7.widget.RecyclerView  
45 - android:id="@+id/recyclerView"  
46 - android:layout_width="368dp"  
47 - android:layout_height="200dp"  
48 - android:layout_marginBottom="8dp"  
49 - android:layout_marginEnd="8dp"  
50 - android:layout_marginLeft="8dp"  
51 -  
52 - android:layout_marginRight="8dp"  
53 - android:layout_marginStart="8dp"  
54 - android:layout_marginTop="8dp"  
55 - app:adapter="@{data.adapter}"  
56 - app:layoutManager="@{data.manager}"  
57 - app:layout_constraintBottom_toBottomOf="parent"  
58 - app:layout_constraintHorizontal_bias="0.0"  
59 - app:layout_constraintLeft_toLeftOf="parent"  
60 - app:layout_constraintRight_toRightOf="parent"  
61 - app:layout_constraintTop_toTopOf="parent"  
62 - app:layout_constraintVertical_bias="1.0" />  
63 44
64 </android.support.constraint.ConstraintLayout> 45 </android.support.constraint.ConstraintLayout>
65 46
app/src/main/res/layout/listitem_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 1 <?xml version="1.0" encoding="utf-8"?>
2 -<layout xmlns:android="http://schemas.android.com/apk/res/android"> 2 +<layout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:app="http://schemas.android.com/apk/res-auto">
3 4
4 <data> 5 <data>
5 6
6 <variable 7 <variable
7 name="data" 8 name="data"
8 - type="com.cnlive.libs.demo.MainListItemData" /> 9 + type="com.cnlive.libs.demo.mvptest.model.LaunchListItemData" />
9 </data> 10 </data>
10 11
11 <RelativeLayout 12 <RelativeLayout
@@ -18,7 +19,9 @@ @@ -18,7 +19,9 @@
18 android:layout_centerVertical="true" 19 android:layout_centerVertical="true"
19 android:onClick="onMainItemClick" 20 android:onClick="onMainItemClick"
20 android:padding="8dp" 21 android:padding="8dp"
21 - android:text="@{data.name}" /> 22 +
  23 + android:text="@{data.name}"
  24 + app:tag="@{data}" />
22 25
23 </RelativeLayout> 26 </RelativeLayout>
24 27
cnlive/build.gradle
@@ -35,6 +35,7 @@ android { @@ -35,6 +35,7 @@ android {
35 35
36 dependencies { 36 dependencies {
37 compile fileTree(include: ['*.jar'], dir: 'libs') 37 compile fileTree(include: ['*.jar'], dir: 'libs')
  38 + compile 'com.android.support:recyclerview-v7:26.+'
38 //MVP 39 //MVP
39 compile 'com.hannesdorfmann.mosby3:mvp:3.0.4' 40 compile 'com.hannesdorfmann.mosby3:mvp:3.0.4'
40 compile 'com.hannesdorfmann.mosby3:viewstate:3.0.4' 41 compile 'com.hannesdorfmann.mosby3:viewstate:3.0.4'
cnlive/from_installv1.gradle
@@ -41,7 +41,7 @@ install { @@ -41,7 +41,7 @@ install {
41 List list = new ArrayList() 41 List list = new ArrayList()
42 pom.dependencies.findAll { 42 pom.dependencies.findAll {
43 dep -> 43 dep ->
44 - dep.groupId == rootProject.name 44 + dep.groupId == rootProject.name || dep.groupId == 'com.android.support'
45 }.collect { 45 }.collect {
46 dep -> 46 dep ->
47 list.add(dep) 47 list.add(dep)
app/src/main/java/com/cnlive/libs/demo/DataBindingRecyclerAdapter.java renamed to cnlive/src/main/java/com/cnlive/libs/base/frame/adapter/DataBindingRecyclerAdapter.java
1 -package com.cnlive.libs.demo; 1 +package com.cnlive.libs.base.frame.adapter;
  2 +
  3 +/**
  4 + * Created by xiansong on 2017/9/1.
  5 + */
2 6
3 import android.databinding.DataBindingUtil; 7 import android.databinding.DataBindingUtil;
4 import android.databinding.ViewDataBinding; 8 import android.databinding.ViewDataBinding;
@@ -14,6 +18,23 @@ import java.util.List; @@ -14,6 +18,23 @@ import java.util.List;
14 18
15 public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> extends RecyclerView.Adapter<DataBindingRecyclerAdapter<D, T>.DataBindingViewHolder> { 19 public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> extends RecyclerView.Adapter<DataBindingRecyclerAdapter<D, T>.DataBindingViewHolder> {
16 20
  21 + class DataBindingViewHolder extends RecyclerView.ViewHolder {
  22 +
  23 + private ViewDataBinding binding;
  24 +
  25 + private DataBindingViewHolder(View itemView) {
  26 + super(itemView);
  27 + }
  28 +
  29 + public void setBinding(ViewDataBinding binding) {
  30 + this.binding = binding;
  31 + }
  32 +
  33 + public ViewDataBinding getBinding() {
  34 + return binding;
  35 + }
  36 + }
  37 +
17 public abstract int getLayoutId(); 38 public abstract int getLayoutId();
18 39
19 public abstract List<D> getList(); 40 public abstract List<D> getList();
@@ -32,26 +53,51 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e @@ -32,26 +53,51 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e
32 holder.binding.executePendingBindings(); 53 holder.binding.executePendingBindings();
33 } 54 }
34 55
  56 + public D getItem(int location) {
  57 + return getList().get(location);
  58 + }
  59 +
35 @Override 60 @Override
36 public int getItemCount() { 61 public int getItemCount() {
37 - return getList().size(); 62 + return getList() == null ? 0 : getList().size();
38 } 63 }
39 64
40 - public class DataBindingViewHolder extends RecyclerView.ViewHolder { 65 + public void addItem(D item) {
  66 + if (item == null) return;
  67 + getList().add(getList().size(), item);
  68 + notifyItemInserted(getList().size());
  69 + }
41 70
42 - private ViewDataBinding binding; 71 + public void addItems(List<D> items) {
  72 + if (items == null) return;
  73 + this.getList().addAll(items);
  74 + notifyDataSetChanged();
  75 + }
43 76
44 - public DataBindingViewHolder(View itemView) {  
45 - super(itemView);  
46 - } 77 + public boolean containsAll(List<D> items) {
  78 + return getList().containsAll(items);
  79 + }
47 80
48 - public void setBinding(ViewDataBinding binding) {  
49 - this.binding = binding;  
50 - } 81 + public void updateItem(D tasks, int position) {
  82 + if (tasks == null) return;
  83 + getList().set(position, tasks);
  84 + notifyItemChanged(position);
  85 + }
51 86
52 - public ViewDataBinding getBinding() {  
53 - return binding;  
54 - } 87 + public void updateItems(List<D> items) {
  88 + if (items == null) return;
  89 + this.getList().clear();
  90 + this.getList().addAll(items);
  91 + notifyDataSetChanged();
  92 + }
  93 +
  94 + public void removeItem(int index) {
  95 + getList().remove(index);
  96 + notifyItemRemoved(index);
  97 + }
55 98
  99 + public void removeAllItems() {
  100 + getList().clear();
  101 + notifyDataSetChanged();
56 } 102 }
57 -} 103 -}
  104 +}
58 \ No newline at end of file 105 \ No newline at end of file
cnlive/src/main/java/com/cnlive/libs/base/frame/model/ViewStateListData.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.model;
  2 +
  3 +import android.databinding.BaseObservable;
  4 +import android.databinding.Bindable;
  5 +import android.databinding.ViewDataBinding;
  6 +import android.os.Parcel;
  7 +import android.os.Parcelable;
  8 +import android.support.v7.widget.LinearLayoutManager;
  9 +import android.support.v7.widget.RecyclerView;
  10 +
  11 +import com.cnlive.libs.base.frame.adapter.DataBindingRecyclerAdapter;
  12 +import com.cnlive.libs.base.util.LogUtil;
  13 +
  14 +import java.util.ArrayList;
  15 +import java.util.List;
  16 +
  17 +/**
  18 + * Created by xiansong on 2017/9/1.
  19 + */
  20 +
  21 +public abstract class ViewStateListData<ITEM extends BaseObservable, BINDING extends ViewDataBinding> extends BaseObservable implements Parcelable {
  22 +
  23 + private static final String TAG = "PageListData";
  24 +
  25 + public static <T extends ViewStateListData<ITEM, BINDING>, ITEM extends BaseObservable, BINDING extends ViewDataBinding> T newInstance(Class<T> clazz, Parcel in) {
  26 + T data = null;
  27 + try {
  28 + data = clazz.newInstance();
  29 + data.createFromParcel(in);
  30 + } catch (Exception e) {
  31 + LogUtil.e(TAG, "Presenter or data need to implement the default constructor: constructor ()", e);
  32 + }
  33 + return data;
  34 + }
  35 +
  36 + public abstract int getLayoutId();
  37 +
  38 + public abstract RecyclerView.LayoutManager getLayoutManager();
  39 +
  40 + private List<ITEM> list = new ArrayList<>();
  41 +
  42 + private RecyclerView.LayoutManager manager = getLayoutManager();
  43 +
  44 + private DataBindingRecyclerAdapter<ITEM, BINDING> adapter = new DataBindingRecyclerAdapter<ITEM, BINDING>() {
  45 + @Override
  46 + public int getLayoutId() {
  47 + return ViewStateListData.this.getLayoutId();
  48 + }
  49 +
  50 + @Override
  51 + public List<ITEM> getList() {
  52 + return list;
  53 + }
  54 + };
  55 +
  56 + @Bindable
  57 + public DataBindingRecyclerAdapter<ITEM, BINDING> getAdapter() {
  58 + return adapter;
  59 + }
  60 +
  61 + public void setAdapter(DataBindingRecyclerAdapter<ITEM, BINDING> adapter) {
  62 + this.adapter = adapter;
  63 + notifyPropertyChanged(com.cnlive.libs.base.BR.adapter);
  64 + }
  65 +
  66 + @Bindable
  67 + public RecyclerView.LayoutManager getManager() {
  68 + return manager;
  69 + }
  70 +
  71 + public void setManager(LinearLayoutManager manager) {
  72 + this.manager = manager;
  73 + notifyPropertyChanged(com.cnlive.libs.base.BR.manager);
  74 + }
  75 +
  76 + @Override
  77 + public int describeContents() {
  78 + return 0;
  79 + }
  80 +
  81 + @Override
  82 + public void writeToParcel(Parcel parcel, int i) {
  83 + parcel.writeList(this.list);
  84 + }
  85 +
  86 + public void createFromParcel(Parcel in) {
  87 + in.readList(list, getClass().getClassLoader());
  88 + }
  89 +
  90 +}