Commit 0a7f60e4133eb2427837f253ae650f8132e9c8ca

Authored by 朱显松
1 parent e3b05a97

add adapter click callback

.idea/modules.xml
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 <component name="ProjectModuleManager"> 3 <component name="ProjectModuleManager">
4 <modules> 4 <modules>
5 <module fileurl="file://$PROJECT_DIR$/CNLiveLibs.iml" filepath="$PROJECT_DIR$/CNLiveLibs.iml" /> 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 <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> 7 <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
8 <module fileurl="file://$PROJECT_DIR$/cnlive/cnlive.iml" filepath="$PROJECT_DIR$/cnlive/cnlive.iml" /> 8 <module fileurl="file://$PROJECT_DIR$/cnlive/cnlive.iml" filepath="$PROJECT_DIR$/cnlive/cnlive.iml" />
9 </modules> 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 +6,7 @@ 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.base.frame.adapter.DataBindingRecyclerAdapter;
9 import com.cnlive.libs.demo.mvptest.model.LaunchListItemData; 10 import com.cnlive.libs.demo.mvptest.model.LaunchListItemData;
10 import com.cnlive.libs.demo.R; 11 import com.cnlive.libs.demo.R;
11 import com.cnlive.libs.demo.ToastTest; 12 import com.cnlive.libs.demo.ToastTest;
@@ -29,6 +30,12 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun @@ -29,6 +30,12 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
29 @Override 30 @Override
30 protected void onPostCreate(Bundle savedInstanceState) { 31 protected void onPostCreate(Bundle savedInstanceState) {
31 super.onPostCreate(savedInstanceState); 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 @Override 41 @Override
@@ -42,11 +49,11 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun @@ -42,11 +49,11 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
42 getPresenter().destroy(); 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 case R.id.click: 54 case R.id.click:
48 getViewData().setTime(2); 55 getViewData().setTime(2);
49 - startActivity(new Intent(this,LaunchActivity.class)); 56 + startActivity(new Intent(this, LaunchActivity.class));
50 break; 57 break;
51 } 58 }
52 } 59 }
@@ -57,12 +64,13 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun @@ -57,12 +64,13 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
57 } 64 }
58 65
59 private int index = 1; 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 getViewData().setTime(getViewData().getAdapter().getItemCount()); 70 getViewData().setTime(getViewData().getAdapter().getItemCount());
63 } 71 }
64 72
65 public void onMainItemClick(View view) { 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,6 +3,10 @@
3 buildscript { 3 buildscript {
4 repositories { 4 repositories {
5 jcenter() 5 jcenter()
  6 + maven {
  7 + url 'https://maven.google.com/'
  8 + name 'Google'
  9 + }
6 } 10 }
7 dependencies { 11 dependencies {
8 classpath 'com.android.tools.build:gradle:2.3.3' 12 classpath 'com.android.tools.build:gradle:2.3.3'
@@ -17,6 +21,10 @@ buildscript { @@ -17,6 +21,10 @@ buildscript {
17 allprojects { 21 allprojects {
18 repositories { 22 repositories {
19 jcenter() 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,6 +38,7 @@ dependencies {
38 //分包 38 //分包
39 compile 'com.android.support:multidex:1.0.1' 39 compile 'com.android.support:multidex:1.0.1'
40 //RecyclerView 需要支持库 ,打包时已取消对其发布 40 //RecyclerView 需要支持库 ,打包时已取消对其发布
  41 + compile 'com.android.support:appcompat-v7:26.+'
41 compile 'com.android.support:recyclerview-v7:26.+' 42 compile 'com.android.support:recyclerview-v7:26.+'
42 //MVP 43 //MVP
43 compile 'com.hannesdorfmann.mosby3:mvp:3.0.4' 44 compile 'com.hannesdorfmann.mosby3:mvp:3.0.4'
@@ -77,7 +78,7 @@ ext { @@ -77,7 +78,7 @@ ext {
77 siteUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo' 78 siteUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo'
78 gitUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo.git' 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 developerId = 'CNlive' 83 developerId = 'CNlive'
83 developerName = 'Granzon' 84 developerName = 'Granzon'
cnlive/src/main/java/com/cnlive/libs/base/frame/adapter/DataBindingRecyclerAdapter.java
@@ -35,10 +35,24 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e @@ -35,10 +35,24 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; 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 public abstract int getLayoutId(); 50 public abstract int getLayoutId();
39 51
40 public abstract List<D> getList(); 52 public abstract List<D> getList();
41 53
  54 + private OnItemClickListener<D> listener;
  55 +
42 @Override 56 @Override
43 public DataBindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 57 public DataBindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
44 T binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), getLayoutId(), parent, false); 58 T binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), getLayoutId(), parent, false);
@@ -50,6 +64,12 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e @@ -50,6 +64,12 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e
50 @Override 64 @Override
51 public void onBindViewHolder(DataBindingViewHolder holder, int position) { 65 public void onBindViewHolder(DataBindingViewHolder holder, int position) {
52 ReflectUtil.invoke(holder.binding, "setData", getList().get(position)); 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 holder.binding.executePendingBindings(); 73 holder.binding.executePendingBindings();
54 } 74 }
55 75
@@ -62,6 +82,10 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e @@ -62,6 +82,10 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e
62 return getList() == null ? 0 : getList().size(); 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 public void addItem(int position, D item) { 89 public void addItem(int position, D item) {
66 if (item == null) return; 90 if (item == null) return;
67 getList().add(position, item); 91 getList().add(position, item);