From 101768581bfc0ffe779959541907c2f51acef359 Mon Sep 17 00:00:00 2001 From: chaychan <844738237@qq.com> Date: Thu, 12 Apr 2018 17:39:43 +0800 Subject: [PATCH] Deprecated interface ItemProvideTag; Replace reflections with native method; --- .../adapter/MessageListAdapter.java | 3 ++- .../provider/ImageMessageItemProvider.java | 19 ++++++++----- .../provider/TextMessageItemProvider.java | 19 ++++++++----- .../chaychan/adapter/BaseItemProvider.java | 27 ++++++++++++++----- .../adapter/ItemProviderException.java | 15 +++++++++++ .../com/chaychan/adapter/ItemProviderTag.java | 1 + .../adapter/MultipleItemRvAdapter.java | 21 +++++++++------ .../chaychan/adapter/ProviderDelegate.java | 9 ++++--- 8 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 library/src/main/java/com/chaychan/adapter/ItemProviderException.java diff --git a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/MessageListAdapter.java b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/MessageListAdapter.java index 6d028e1..89ae3bc 100644 --- a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/MessageListAdapter.java +++ b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/MessageListAdapter.java @@ -2,6 +2,7 @@ import android.support.annotation.Nullable; +import com.chad.library.adapter.base.BaseViewHolder; import com.chaychan.adapter.MultipleItemRvAdapter; import com.chaychan.multiitemrvadapter.adapter.provider.ImageMessageItemProvider; import com.chaychan.multiitemrvadapter.adapter.provider.TextMessageItemProvider; @@ -17,7 +18,7 @@ * @date 2018/3/21 14:40 */ -public class MessageListAdapter extends MultipleItemRvAdapter { +public class MessageListAdapter extends MultipleItemRvAdapter { public static final int TYPE_TEXT = 0; public static final int TYPE_IMG = 1; diff --git a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/ImageMessageItemProvider.java b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/ImageMessageItemProvider.java index bad139e..55d8d33 100644 --- a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/ImageMessageItemProvider.java +++ b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/ImageMessageItemProvider.java @@ -6,21 +6,26 @@ import com.bumptech.glide.Glide; import com.chad.library.adapter.base.BaseViewHolder; import com.chaychan.adapter.BaseItemProvider; -import com.chaychan.adapter.ItemProviderTag; -import com.chaychan.multiitemrvadapter.model.ImageMessage; import com.chaychan.multiitemrvadapter.R; import com.chaychan.multiitemrvadapter.adapter.MessageListAdapter; +import com.chaychan.multiitemrvadapter.model.ImageMessage; /** * @author ChayChan * @description 图片消息条目的provider * @date 2018/3/21 14:43 */ -@ItemProviderTag( - viewType = MessageListAdapter.TYPE_IMG, - layout = R.layout.item_image_message -) -public class ImageMessageItemProvider extends BaseItemProvider { +public class ImageMessageItemProvider extends BaseItemProvider { + + @Override + public int viewType() { + return MessageListAdapter.TYPE_IMG; + } + + @Override + public int layout() { + return R.layout.item_image_message; + } @Override public void convert(BaseViewHolder helper, ImageMessage data, int position) { diff --git a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/TextMessageItemProvider.java b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/TextMessageItemProvider.java index edac071..78dba1e 100644 --- a/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/TextMessageItemProvider.java +++ b/app/src/main/java/com/chaychan/multiitemrvadapter/adapter/provider/TextMessageItemProvider.java @@ -4,21 +4,26 @@ import com.chad.library.adapter.base.BaseViewHolder; import com.chaychan.adapter.BaseItemProvider; -import com.chaychan.adapter.ItemProviderTag; import com.chaychan.multiitemrvadapter.R; -import com.chaychan.multiitemrvadapter.model.TextMessage; import com.chaychan.multiitemrvadapter.adapter.MessageListAdapter; +import com.chaychan.multiitemrvadapter.model.TextMessage; /** * @author ChayChan * @description 文本消息条目的provider * @date 2018/3/21 14:43 */ -@ItemProviderTag( - viewType = MessageListAdapter.TYPE_TEXT, - layout = R.layout.item_text_message -) -public class TextMessageItemProvider extends BaseItemProvider { +public class TextMessageItemProvider extends BaseItemProvider { + + @Override + public int viewType() { + return MessageListAdapter.TYPE_TEXT; + } + + @Override + public int layout() { + return R.layout.item_text_message; + } @Override public void convert(BaseViewHolder helper, TextMessage data, int position) { diff --git a/library/src/main/java/com/chaychan/adapter/BaseItemProvider.java b/library/src/main/java/com/chaychan/adapter/BaseItemProvider.java index 4f4b807..064193c 100644 --- a/library/src/main/java/com/chaychan/adapter/BaseItemProvider.java +++ b/library/src/main/java/com/chaychan/adapter/BaseItemProvider.java @@ -7,19 +7,32 @@ import java.util.List; /** + * https://github.com/chaychan * @author ChayChan - * @description: 条目provider的基类 + * @description: The base class of ItemProvider * @date 2018/3/21 10:41 */ -public abstract class BaseItemProvider { +public abstract class BaseItemProvider { - protected Context mContext; - protected List mData; + public Context mContext; + public List mData; - public abstract void convert(BaseViewHolder helper, T data, int position); + //子类须重写该方法返回viewType + //Rewrite this method to return viewType + public abstract int viewType(); - public abstract void onClick(BaseViewHolder helper,T data,int position); + //子类须重写该方法返回layout + //Rewrite this method to return layout + public abstract int layout(); - public abstract boolean onLongClick(BaseViewHolder helper,T data,int position); + public abstract void convert(V helper, T data, int position); + + //子类若想实现条目点击事件则重写该方法 + //Subclasses override this method if you want to implement an item click event + public void onClick(V helper, T data, int position){}; + + //子类若想实现条目长按事件则重写该方法 + //Subclasses override this method if you want to implement an item long press event + public boolean onLongClick(V helper, T data, int position){return false;}; } diff --git a/library/src/main/java/com/chaychan/adapter/ItemProviderException.java b/library/src/main/java/com/chaychan/adapter/ItemProviderException.java new file mode 100644 index 0000000..30764de --- /dev/null +++ b/library/src/main/java/com/chaychan/adapter/ItemProviderException.java @@ -0,0 +1,15 @@ +package com.chaychan.adapter; + +/** + * @author ChayChan + * @description: ItemProviderException + * @date 2018/4/12 9:10 + */ + +public class ItemProviderException extends NullPointerException { + + public ItemProviderException(String message) { + super(message); + } + +} diff --git a/library/src/main/java/com/chaychan/adapter/ItemProviderTag.java b/library/src/main/java/com/chaychan/adapter/ItemProviderTag.java index 7eef7e4..30b5a21 100644 --- a/library/src/main/java/com/chaychan/adapter/ItemProviderTag.java +++ b/library/src/main/java/com/chaychan/adapter/ItemProviderTag.java @@ -13,6 +13,7 @@ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) +@Deprecated public @interface ItemProviderTag { int viewType(); int layout(); diff --git a/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java b/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java index 0f96775..52a1b10 100644 --- a/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java +++ b/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java @@ -11,12 +11,15 @@ import java.util.List; /** + * https://github.com/chaychan * @author ChayChan - * @description: 封装多条目adapter + * @description: + * When there are multiple entries, avoid too much business logic in convert(),Put the logic of each item in the corresponding ItemProvider + * 当有多种条目的时候,避免在convert()中做太多的业务逻辑,把逻辑放在对应的ItemProvider中 * @date 2018/3/21 9:55 */ -public abstract class MultipleItemRvAdapter extends BaseQuickAdapter { +public abstract class MultipleItemRvAdapter extends BaseQuickAdapter { private SparseArray mItemProviders; protected ProviderDelegate mProviderDelegate; @@ -27,6 +30,7 @@ public MultipleItemRvAdapter(@Nullable List data) { /** * 用于adapter构造函数完成参数的赋值后调用 + * Called after the assignment of the argument to the adapter constructor */ public void finishInitialize() { mProviderDelegate = new ProviderDelegate(); @@ -42,14 +46,12 @@ protected int getItemType(T t) { registerItemProvider(); mItemProviders = mProviderDelegate.getItemProviders(); + for (int i = 0; i < mItemProviders.size(); i++) { int key = mItemProviders.keyAt(i); - BaseItemProvider provider = mItemProviders.get(key); provider.mData = mData; - - ItemProviderTag tag = provider.getClass().getAnnotation(ItemProviderTag.class); - getMultiTypeDelegate().registerItemType(key, tag.layout()); + getMultiTypeDelegate().registerItemType(key, provider.layout()); } } @@ -58,7 +60,7 @@ protected int getItemType(T t) { public abstract void registerItemProvider(); @Override - protected void convert(BaseViewHolder helper, T item) { + protected void convert(V helper, T item) { int itemViewType = helper.getItemViewType(); BaseItemProvider provider = mItemProviders.get(itemViewType); @@ -70,12 +72,13 @@ protected void convert(BaseViewHolder helper, T item) { bindClick(helper, item, position, provider); } - private void bindClick(final BaseViewHolder helper, final T item, final int position, final BaseItemProvider provider) { + private void bindClick(final V helper, final T item, final int position, final BaseItemProvider provider) { OnItemClickListener clickListener = getOnItemClickListener(); OnItemLongClickListener longClickListener = getOnItemLongClickListener(); if (clickListener != null && longClickListener != null){ //如果已经设置了子条目点击监听和子条目长按监听 + // If you have set up a sub-entry click monitor and sub-entries long press listen return; } @@ -83,6 +86,7 @@ private void bindClick(final BaseViewHolder helper, final T item, final int posi if (clickListener == null){ //如果没有设置点击监听,则回调给itemProvider + //Callback to itemProvider if no click listener is set itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -93,6 +97,7 @@ public void onClick(View v) { if (longClickListener == null){ //如果没有设置长按监听,则回调给itemProvider + // If you do not set a long press listener, callback to the itemProvider itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { diff --git a/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java b/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java index 08066c3..0ea4fe9 100644 --- a/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java +++ b/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java @@ -3,6 +3,7 @@ import android.util.SparseArray; /** + * https://github.com/chaychan * @author ChayChan * @date 2018/3/21 11:04 */ @@ -12,12 +13,12 @@ public class ProviderDelegate { private SparseArray mItemProviders = new SparseArray<>(); public void registerProvider(BaseItemProvider provider){ - ItemProviderTag tag = provider.getClass().getAnnotation(ItemProviderTag.class); - if (tag == null){ - throw new RuntimeException("Can not find the annotation: 'ItemProviderTag'"); + if (provider == null){ + throw new ItemProviderException("ItemProvider can not be null"); } - int viewType = tag.viewType(); + int viewType = provider.viewType(); + if (mItemProviders.get(viewType) == null){ mItemProviders.put(viewType,provider); }