From 8b968df5a6e0daf3ed90918412760698899f8351 Mon Sep 17 00:00:00 2001 From: donnieSky Date: Mon, 31 Jul 2017 17:17:35 +0800 Subject: [PATCH] support databinding --- app/build.gradle | 4 + .../main/java/me/donnie/app/MainActivity.java | 37 +++- .../app/databinding/ItemDb1Delegate.java | 28 +++ .../app/databinding/ItemDb2Delegate.java | 28 +++ .../databinding/ItemDbDefaultDelegate.java | 29 +++ .../app/databinding/StringViewModel.java | 24 +++ .../app/databinding/TestBindingAdapter.java | 32 +++ .../app/databinding/TextMultiDbAdapter.java | 21 ++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/view_db_item.xml | 38 ++++ app/src/main/res/layout/view_db_item_1.xml | 36 ++++ app/src/main/res/layout/view_db_item_2.xml | 32 +++ library/build.gradle | 4 + .../java/me/donnie/adapter/BaseAdapter.java | 2 +- .../me/donnie/adapter/MultiItemAdapter.java | 2 +- .../adapter/databinding/BaseBindHolder.java | 28 +++ .../databinding/BaseBindingAdapter.java | 200 ++++++++++++++++++ .../databinding/MultiItemBindingAdapter.java | 74 +++++++ .../delegate/ItemViewDelegate.java | 19 ++ .../delegate/ItemViewDelegateManager.java | 98 +++++++++ .../delegate/ItemViewDelegateManager.java | 1 - local.properties | 12 ++ 22 files changed, 741 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/me/donnie/app/databinding/ItemDb1Delegate.java create mode 100644 app/src/main/java/me/donnie/app/databinding/ItemDb2Delegate.java create mode 100644 app/src/main/java/me/donnie/app/databinding/ItemDbDefaultDelegate.java create mode 100644 app/src/main/java/me/donnie/app/databinding/StringViewModel.java create mode 100644 app/src/main/java/me/donnie/app/databinding/TestBindingAdapter.java create mode 100644 app/src/main/java/me/donnie/app/databinding/TextMultiDbAdapter.java create mode 100644 app/src/main/res/layout/view_db_item.xml create mode 100644 app/src/main/res/layout/view_db_item_1.xml create mode 100644 app/src/main/res/layout/view_db_item_2.xml create mode 100644 library/src/main/java/me/donnie/adapter/databinding/BaseBindHolder.java create mode 100644 library/src/main/java/me/donnie/adapter/databinding/BaseBindingAdapter.java create mode 100644 library/src/main/java/me/donnie/adapter/databinding/MultiItemBindingAdapter.java create mode 100644 library/src/main/java/me/donnie/adapter/databinding/delegate/ItemViewDelegate.java create mode 100644 library/src/main/java/me/donnie/adapter/databinding/delegate/ItemViewDelegateManager.java create mode 100644 local.properties diff --git a/app/build.gradle b/app/build.gradle index 8d0f812..a35460a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,6 +14,10 @@ android { preDexLibraries = preDexEnabled && !isCi } + dataBinding { + enabled = true + } + lintOptions { abortOnError false } diff --git a/app/src/main/java/me/donnie/app/MainActivity.java b/app/src/main/java/me/donnie/app/MainActivity.java index 3468826..ccbf321 100644 --- a/app/src/main/java/me/donnie/app/MainActivity.java +++ b/app/src/main/java/me/donnie/app/MainActivity.java @@ -1,5 +1,6 @@ package me.donnie.app; +import android.databinding.ObservableArrayList; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; @@ -10,15 +11,21 @@ import java.util.ArrayList; import java.util.List; -import me.donnie.adapter.BaseAdapter; import me.donnie.adapter.BaseViewHolder; +import me.donnie.adapter.databinding.BaseBindingAdapter; import me.donnie.adapter.wrapper.Walle; +import me.donnie.app.databinding.StringViewModel; +import me.donnie.app.databinding.TextMultiDbAdapter; public class MainActivity extends AppCompatActivity implements Walle.OnLoadMoreListener { private RecyclerView rv; - private Test2Adapter adapter; + //private Test2Adapter adapter; + + //private TestBindingAdapter mAdapter; + + private TextMultiDbAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,7 +33,8 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.rv); rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); - adapter = new Test2Adapter(); + //adapter = new Test2Adapter(); + mAdapter = new TextMultiDbAdapter(); Walle walle = Walle.newBuilder() .enableHeader(true) @@ -36,12 +44,13 @@ protected void onCreate(Bundle savedInstanceState) { .enableLoadMore(true) .loadmoreRes(R.layout.view_load_more) .addLoadMoreListener(this) - .wrapperAdapter(adapter).build(); + .wrapperAdapter(mAdapter).build(); rv.setAdapter(walle.getWrapperAdapter()); + //rv.setAdapter(mAdapter); - adapter.setData(getdatas()); + mAdapter.setData(getViewModels()); - adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() { + mAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener() { @Override public void onItemClick(View view, BaseViewHolder holder, int position) { Toast.makeText(MainActivity.this, "点击"+(position - 1), Toast.LENGTH_SHORT).show(); @@ -49,6 +58,22 @@ public void onItemClick(View view, BaseViewHolder holder, int position) { }); } + private ObservableArrayList getViewModels() { + ObservableArrayList models = new ObservableArrayList<>(); + for (int i = 0; i < 30; i++) { + String txt = ""; + if (i == 2) { + txt = ""+2; + } else if (i == 3) { + txt = ""+3; + } else { + txt = "This is Test -- "+i; + } + models.add(new StringViewModel(txt)); + } + return models; + } + private List getdatas() { List data = new ArrayList<>(); for (int i = 0; i < 30 ; i++) { diff --git a/app/src/main/java/me/donnie/app/databinding/ItemDb1Delegate.java b/app/src/main/java/me/donnie/app/databinding/ItemDb1Delegate.java new file mode 100644 index 0000000..6952ee6 --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/ItemDb1Delegate.java @@ -0,0 +1,28 @@ +package me.donnie.app.databinding; + +import me.donnie.adapter.databinding.delegate.ItemViewDelegate; +import me.donnie.app.R; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class ItemDb1Delegate implements ItemViewDelegate { + @Override + public int getItemViewLayoutId() { + return R.layout.view_db_item_1; + } + + @Override + public boolean isForViewType(StringViewModel item, int position) { + return item.getTxt().equals("2"); + } + + @Override + public void convert(me.donnie.app.databinding.ViewDbItem1Binding binding, StringViewModel stringViewModel, int position) { + binding.setViewModel(stringViewModel); + binding.executePendingBindings(); + } +} diff --git a/app/src/main/java/me/donnie/app/databinding/ItemDb2Delegate.java b/app/src/main/java/me/donnie/app/databinding/ItemDb2Delegate.java new file mode 100644 index 0000000..2a8a7ac --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/ItemDb2Delegate.java @@ -0,0 +1,28 @@ +package me.donnie.app.databinding; + +import me.donnie.adapter.databinding.delegate.ItemViewDelegate; +import me.donnie.app.R; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class ItemDb2Delegate implements ItemViewDelegate { + @Override + public int getItemViewLayoutId() { + return R.layout.view_db_item_2; + } + + @Override + public boolean isForViewType(StringViewModel item, int position) { + return item.getTxt().equals("3"); + } + + @Override + public void convert(me.donnie.app.databinding.ViewDbItem2Binding binding, StringViewModel stringViewModel, int position) { + binding.setViewModel(stringViewModel); + binding.executePendingBindings(); + } +} diff --git a/app/src/main/java/me/donnie/app/databinding/ItemDbDefaultDelegate.java b/app/src/main/java/me/donnie/app/databinding/ItemDbDefaultDelegate.java new file mode 100644 index 0000000..4432603 --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/ItemDbDefaultDelegate.java @@ -0,0 +1,29 @@ +package me.donnie.app.databinding; + +import me.donnie.adapter.databinding.delegate.ItemViewDelegate; +import me.donnie.app.R; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class ItemDbDefaultDelegate implements ItemViewDelegate { + @Override + public int getItemViewLayoutId() { + return R.layout.view_db_item; + } + + @Override + public boolean isForViewType(StringViewModel item, int position) { + return !item.getTxt().equals("2") && + !item.getTxt().equals("3"); + } + + @Override + public void convert(ViewDbItemBinding binding, StringViewModel stringViewModel, int position) { + binding.setViewModel(stringViewModel); + binding.executePendingBindings(); + } +} diff --git a/app/src/main/java/me/donnie/app/databinding/StringViewModel.java b/app/src/main/java/me/donnie/app/databinding/StringViewModel.java new file mode 100644 index 0000000..2760827 --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/StringViewModel.java @@ -0,0 +1,24 @@ +package me.donnie.app.databinding; + +import android.databinding.BaseObservable; +import android.databinding.Bindable; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class StringViewModel extends BaseObservable { + + private String txt; + + public StringViewModel(String txt) { + this.txt = txt; + } + + @Bindable + public String getTxt() { + return txt; + } +} diff --git a/app/src/main/java/me/donnie/app/databinding/TestBindingAdapter.java b/app/src/main/java/me/donnie/app/databinding/TestBindingAdapter.java new file mode 100644 index 0000000..45024df --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/TestBindingAdapter.java @@ -0,0 +1,32 @@ +package me.donnie.app.databinding; + +import me.donnie.adapter.BaseViewHolder; +import me.donnie.adapter.databinding.BaseBindingAdapter; +import me.donnie.app.R; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class TestBindingAdapter extends BaseBindingAdapter { + + public TestBindingAdapter() { + super(R.layout.view_db_item); + } + + /*@Override + protected void convert(BaseBindHolder holder, StringViewModel model, int position) { + StringViewModel viewModel = getItem(position); + ViewDbItemBinding itemBinding = holder.binding; + itemBinding.setViewModel(viewModel); + itemBinding.executePendingBindings(); + }*/ + + @Override + protected void convert(ViewDbItemBinding binding, StringViewModel stringViewModel, int position) { + binding.setViewModel(stringViewModel); + binding.executePendingBindings(); + } +} diff --git a/app/src/main/java/me/donnie/app/databinding/TextMultiDbAdapter.java b/app/src/main/java/me/donnie/app/databinding/TextMultiDbAdapter.java new file mode 100644 index 0000000..36723e0 --- /dev/null +++ b/app/src/main/java/me/donnie/app/databinding/TextMultiDbAdapter.java @@ -0,0 +1,21 @@ +package me.donnie.app.databinding; + +import me.donnie.adapter.BaseViewHolder; +import me.donnie.adapter.databinding.MultiItemBindingAdapter; + +/** + * @author donnieSky + * @created_at 2017/7/31. + * @description + */ + +public class TextMultiDbAdapter extends MultiItemBindingAdapter { + + public TextMultiDbAdapter() { + super(null); + + addItemViewDelegate(new ItemDbDefaultDelegate()); + addItemViewDelegate(new ItemDb1Delegate()); + addItemViewDelegate(new ItemDb2Delegate()); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 41547a4..dd167bf 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,4 +6,4 @@ android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="me.donnie.app.MainActivity"/> + tools:context="me.donnie.app.MainActivity"/> \ No newline at end of file diff --git a/app/src/main/res/layout/view_db_item.xml b/app/src/main/res/layout/view_db_item.xml new file mode 100644 index 0000000..65e583c --- /dev/null +++ b/app/src/main/res/layout/view_db_item.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/view_db_item_1.xml b/app/src/main/res/layout/view_db_item_1.xml new file mode 100644 index 0000000..5d765e2 --- /dev/null +++ b/app/src/main/res/layout/view_db_item_1.xml @@ -0,0 +1,36 @@ + + + + + + + + + +