Commit 0a7f60e4133eb2427837f253ae650f8132e9c8ca
1 parent
e3b05a97
add adapter click callback
Showing
5 changed files
with
49 additions
and
8 deletions
.idea/modules.xml
| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | <component name="ProjectModuleManager"> |
| 4 | 4 | <modules> |
| 5 | 5 | <module fileurl="file://$PROJECT_DIR$/CNLiveLibs.iml" filepath="$PROJECT_DIR$/CNLiveLibs.iml" /> |
| 6 | - <module fileurl="file://$PROJECT_DIR$/OPenCNLive_BaseLib.iml" filepath="$PROJECT_DIR$/OPenCNLive_BaseLib.iml" /> | |
| 6 | + <module fileurl="file://E:\android-project\CNLiveLibs\OPenCNLive_BaseLib.iml" filepath="E:\android-project\CNLiveLibs\OPenCNLive_BaseLib.iml" /> | |
| 7 | 7 | <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> |
| 8 | 8 | <module fileurl="file://$PROJECT_DIR$/cnlive/cnlive.iml" filepath="$PROJECT_DIR$/cnlive/cnlive.iml" /> |
| 9 | 9 | </modules> | ... | ... |
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/LaunchActivity.java
| ... | ... | @@ -6,6 +6,7 @@ import android.os.Bundle; |
| 6 | 6 | import android.view.View; |
| 7 | 7 | |
| 8 | 8 | import com.cnlive.libs.base.frame.activity.MvpViewStateBindingActivity; |
| 9 | +import com.cnlive.libs.base.frame.adapter.DataBindingRecyclerAdapter; | |
| 9 | 10 | import com.cnlive.libs.demo.mvptest.model.LaunchListItemData; |
| 10 | 11 | import com.cnlive.libs.demo.R; |
| 11 | 12 | import com.cnlive.libs.demo.ToastTest; |
| ... | ... | @@ -29,6 +30,12 @@ public class LaunchActivity extends MvpViewStateBindingActivity<LaunchView, Laun |
| 29 | 30 | @Override |
| 30 | 31 | protected void onPostCreate(Bundle savedInstanceState) { |
| 31 | 32 | super.onPostCreate(savedInstanceState); |
| 33 | + getViewData().getAdapter().setItemClickListener(new DataBindingRecyclerAdapter.OnItemClickListener<LaunchListItemData>() { | |
| 34 | + @Override | |
| 35 | + public void onItemClick(LaunchListItemData item) { | |
| 36 | + | |
| 37 | + } | |
| 38 | + }); | |
| 32 | 39 | } |
| 33 | 40 | |
| 34 | 41 | @Override |
| ... | ... | @@ -42,11 +49,11 @@ public class LaunchActivity extends MvpViewStateBindingActivity<LaunchView, Laun |
| 42 | 49 | getPresenter().destroy(); |
| 43 | 50 | } |
| 44 | 51 | |
| 45 | - public void onClick(View view){ | |
| 46 | - switch (view.getId()){ | |
| 52 | + public void onClick(View view) { | |
| 53 | + switch (view.getId()) { | |
| 47 | 54 | case R.id.click: |
| 48 | 55 | getViewData().setTime(2); |
| 49 | - startActivity(new Intent(this,LaunchActivity.class)); | |
| 56 | + startActivity(new Intent(this, LaunchActivity.class)); | |
| 50 | 57 | break; |
| 51 | 58 | } |
| 52 | 59 | } |
| ... | ... | @@ -57,12 +64,13 @@ public class LaunchActivity extends MvpViewStateBindingActivity<LaunchView, Laun |
| 57 | 64 | } |
| 58 | 65 | |
| 59 | 66 | private int index = 1; |
| 60 | - public void onAddItemClick(View view){ | |
| 61 | - getViewData().getAdapter().addItem(new LaunchListItemData(""+index++)); | |
| 67 | + | |
| 68 | + public void onAddItemClick(View view) { | |
| 69 | + getViewData().getAdapter().addItem(new LaunchListItemData("" + index++)); | |
| 62 | 70 | getViewData().setTime(getViewData().getAdapter().getItemCount()); |
| 63 | 71 | } |
| 64 | 72 | |
| 65 | 73 | public void onMainItemClick(View view) { |
| 66 | - ToastTest.showMainToast(this,((LaunchListItemData)view.getTag()).getName()); | |
| 74 | + ToastTest.showMainToast(this, ((LaunchListItemData) view.getTag()).getName()); | |
| 67 | 75 | } |
| 68 | 76 | } | ... | ... |
build.gradle
| ... | ... | @@ -3,6 +3,10 @@ |
| 3 | 3 | buildscript { |
| 4 | 4 | repositories { |
| 5 | 5 | jcenter() |
| 6 | + maven { | |
| 7 | + url 'https://maven.google.com/' | |
| 8 | + name 'Google' | |
| 9 | + } | |
| 6 | 10 | } |
| 7 | 11 | dependencies { |
| 8 | 12 | classpath 'com.android.tools.build:gradle:2.3.3' |
| ... | ... | @@ -17,6 +21,10 @@ buildscript { |
| 17 | 21 | allprojects { |
| 18 | 22 | repositories { |
| 19 | 23 | jcenter() |
| 24 | + maven { | |
| 25 | + url 'https://maven.google.com/' | |
| 26 | + name 'Google' | |
| 27 | + } | |
| 20 | 28 | } |
| 21 | 29 | } |
| 22 | 30 | ... | ... |
cnlive/build.gradle
| ... | ... | @@ -38,6 +38,7 @@ dependencies { |
| 38 | 38 | //分包 |
| 39 | 39 | compile 'com.android.support:multidex:1.0.1' |
| 40 | 40 | //RecyclerView 需要支持库 ,打包时已取消对其发布 |
| 41 | + compile 'com.android.support:appcompat-v7:26.+' | |
| 41 | 42 | compile 'com.android.support:recyclerview-v7:26.+' |
| 42 | 43 | //MVP |
| 43 | 44 | compile 'com.hannesdorfmann.mosby3:mvp:3.0.4' |
| ... | ... | @@ -77,7 +78,7 @@ ext { |
| 77 | 78 | siteUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo' |
| 78 | 79 | gitUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo.git' |
| 79 | 80 | |
| 80 | - libraryVersion = '0.2.5' | |
| 81 | + libraryVersion = '0.2.7' | |
| 81 | 82 | |
| 82 | 83 | developerId = 'CNlive' |
| 83 | 84 | developerName = 'Granzon' | ... | ... |
cnlive/src/main/java/com/cnlive/libs/base/frame/adapter/DataBindingRecyclerAdapter.java
| ... | ... | @@ -35,10 +35,24 @@ public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> e |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | + abstract class Click implements View.OnClickListener { | |
| 39 | + int position; | |
| 40 | + | |
| 41 | + Click(int position) { | |
| 42 | + this.position = position; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + public interface OnItemClickListener<D> { | |
| 47 | + void onItemClick(D item); | |
| 48 | + } | |
| 49 | + | |
| 38 | 50 | public abstract int getLayoutId(); |
| 39 | 51 | |
| 40 | 52 | public abstract List<D> getList(); |
| 41 | 53 | |
| 54 | + private OnItemClickListener<D> listener; | |
| 55 | + | |
| 42 | 56 | @Override |
| 43 | 57 | public DataBindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 44 | 58 | T binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), getLayoutId(), parent, false); |
| ... | ... | @@ -50,6 +64,12 @@ public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> e |
| 50 | 64 | @Override |
| 51 | 65 | public void onBindViewHolder(DataBindingViewHolder holder, int position) { |
| 52 | 66 | ReflectUtil.invoke(holder.binding, "setData", getList().get(position)); |
| 67 | + ReflectUtil.invoke(holder.binding, "setClick", new Click(position) { | |
| 68 | + @Override | |
| 69 | + public void onClick(View v) { | |
| 70 | + if (listener != null) listener.onItemClick(getList().get(position)); | |
| 71 | + } | |
| 72 | + }); | |
| 53 | 73 | holder.binding.executePendingBindings(); |
| 54 | 74 | } |
| 55 | 75 | |
| ... | ... | @@ -62,6 +82,10 @@ public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> e |
| 62 | 82 | return getList() == null ? 0 : getList().size(); |
| 63 | 83 | } |
| 64 | 84 | |
| 85 | + public void setItemClickListener(OnItemClickListener<D> listener) { | |
| 86 | + this.listener = listener; | |
| 87 | + } | |
| 88 | + | |
| 65 | 89 | public void addItem(int position, D item) { |
| 66 | 90 | if (item == null) return; |
| 67 | 91 | getList().add(position, item); | ... | ... |