Skip to content

Commit

Permalink
Deprecated interface ItemProvideTag;
Browse files Browse the repository at this point in the history
Replace reflections with native method;
  • Loading branch information
chaychan committed Apr 12, 2018
1 parent b85d6c7 commit 1017685
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +18,7 @@
* @date 2018/3/21 14:40
*/

public class MessageListAdapter extends MultipleItemRvAdapter<Message> {
public class MessageListAdapter extends MultipleItemRvAdapter<Message,BaseViewHolder> {

public static final int TYPE_TEXT = 0;
public static final int TYPE_IMG = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageMessage> {
public class ImageMessageItemProvider extends BaseItemProvider<ImageMessage,BaseViewHolder> {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextMessage> {
public class TextMessageItemProvider extends BaseItemProvider<TextMessage,BaseViewHolder> {

@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) {
Expand Down
27 changes: 20 additions & 7 deletions library/src/main/java/com/chaychan/adapter/BaseItemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
public abstract class BaseItemProvider<T,V extends BaseViewHolder> {

protected Context mContext;
protected List<T> mData;
public Context mContext;
public List<T> 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;};
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Deprecated
public @interface ItemProviderTag {
int viewType();
int layout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends BaseQuickAdapter<T, BaseViewHolder> {
public abstract class MultipleItemRvAdapter<T,V extends BaseViewHolder> extends BaseQuickAdapter<T, V> {

private SparseArray<BaseItemProvider> mItemProviders;
protected ProviderDelegate mProviderDelegate;
Expand All @@ -27,6 +30,7 @@ public MultipleItemRvAdapter(@Nullable List<T> data) {

/**
* 用于adapter构造函数完成参数的赋值后调用
* Called after the assignment of the argument to the adapter constructor
*/
public void finishInitialize() {
mProviderDelegate = new ProviderDelegate();
Expand All @@ -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());
}
}

Expand All @@ -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);

Expand All @@ -70,19 +72,21 @@ 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;
}

View itemView = helper.itemView;

if (clickListener == null){
//如果没有设置点击监听,则回调给itemProvider
//Callback to itemProvider if no click listener is set
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.util.SparseArray;

/**
* https://github.com/chaychan
* @author ChayChan
* @date 2018/3/21 11:04
*/
Expand All @@ -12,12 +13,12 @@ public class ProviderDelegate {
private SparseArray<BaseItemProvider> 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);
}
Expand Down

0 comments on commit 1017685

Please sign in to comment.