getData() {
66 | return mDataList;
67 | }
68 |
69 | @Override
70 | public long getItemId(int position) {
71 | return position;
72 | }
73 |
74 | /**
75 | * instead by{@link #getItemType(Object)}
76 | *
77 | * 通过数据得到obj的类型的type
78 | * 然后,通过{@link ItemTypeUtil}来转换位int类型的type
79 | */
80 | @Deprecated
81 | @Override
82 | public int getItemViewType(int position) {
83 | this.currentPos = position;
84 | mType = getItemType(mDataList.get(position));
85 | return mUtil.getIntType(mType);
86 | }
87 |
88 | @Override
89 | public Object getItemType(T t) {
90 | return -1; // default
91 | }
92 |
93 | @Override
94 | public RcvAdapterItem onCreateViewHolder(ViewGroup parent, int viewType) {
95 | return new RcvAdapterItem(parent.getContext(), parent, createItem(mType));
96 | }
97 |
98 | @Override
99 | public void onBindViewHolder(RcvAdapterItem holder, int position) {
100 |
101 | holder.item.handleData(getConvertedData(mDataList.get(position), mType), position);
102 |
103 |
104 | }
105 |
106 | @NonNull
107 | @Override
108 | public Object getConvertedData(T data, Object type) {
109 | return data;
110 | }
111 |
112 | @Override
113 | public int getCurrentPosition() {
114 | return currentPos;
115 | }
116 |
117 | ///////////////////////////////////////////////////////////////////////////
118 | // 内部用到的viewHold
119 | ///////////////////////////////////////////////////////////////////////////
120 |
121 | public static class RcvAdapterItem extends RecyclerView.ViewHolder {
122 |
123 | protected AdapterItem item;
124 |
125 | boolean isNew = true; // debug中才用到
126 |
127 | RcvAdapterItem(Context context, ViewGroup parent, AdapterItem item) {
128 | super(LayoutInflater.from(context).inflate(item.getLayoutResId(), parent, false));
129 | this.item = item;
130 | this.item.bindViews(itemView);
131 | this.item.setViews();
132 | }
133 |
134 | }
135 |
136 | ///////////////////////////////////////////////////////////////////////////
137 | // For debug
138 | ///////////////////////////////////////////////////////////////////////////
139 |
140 | private void debug(RcvAdapterItem holder) {
141 | boolean debug = false;
142 | if (debug) {
143 | holder.itemView.setBackgroundColor(holder.isNew ? 0xffff0000 : 0xff00ff00);
144 | holder.isNew = false;
145 | }
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/base/src/main/java/com/ccj/base/adapter/bean/AdapterBean.java:
--------------------------------------------------------------------------------
1 | package com.ccj.base.adapter.bean;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 数据bean
7 | * 为了不同viewItem 分别对应不同的bean,
8 | * 避免不同item的字段都杂糅的同一个类中的情况,比如{ mobile中的 CommonRowsBean}
9 | * 根据Holder建立一个新的bean, 自定义的bean要继承AdapterBean,在bean中添置所需字段,供holder调用,比如(mobile中 BrandRcvAdapter中的holder)
10 | *
11 | * Created by chenchangjun on 17/12/28.
12 | */
13 |
14 | public abstract class AdapterBean implements Serializable {
15 |
16 | //唯一标示!
17 |
18 |
19 | public abstract void setCell_type(int cell_type);
20 |
21 |
22 | public abstract int getCell_type();
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/base/src/main/java/com/ccj/base/adapter/bean/AdapterGroupBean.java:
--------------------------------------------------------------------------------
1 | package com.ccj.base.adapter.bean;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * 类似于 view和 viewgroup 的概念 这里是 viewgroup
8 | * Created by chenchangjun on 18/1/4.
9 | */
10 |
11 | public abstract class AdapterGroupBean extends AdapterBean implements Serializable {
12 |
13 |
14 | //唯一标示!
15 |
16 | public abstract List getList();
17 |
18 | public abstract String getTitle();
19 |
20 | public abstract T getRedirect_data();
21 |
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/base/src/main/java/com/ccj/base/adapter/item/AdapterItem.java:
--------------------------------------------------------------------------------
1 | package com.ccj.base.adapter.item;
2 |
3 | import android.support.annotation.LayoutRes;
4 | import android.view.View;
5 |
6 | /**
7 | * @author ccj
8 | * @date 2017/3/21
9 | */
10 | public interface AdapterItem {
11 |
12 |
13 | /**
14 | * @return item布局文件的layoutId
15 | */
16 | @LayoutRes
17 | int getLayoutResId();
18 |
19 | /**
20 | * 初始化views
21 | */
22 | void bindViews(final View root);
23 |
24 | /**
25 | * 设置view的参数
26 | * 设置监听等,只执行一次
27 | */
28 | void setViews();
29 |
30 | /**
31 | * 根据数据来设置item的内部views
32 | *
33 | * @param t 数据list内部的model
34 | * @param position 当前adapter调用item的位置
35 | */
36 | void handleData(T t, int position);
37 |
38 | }
--------------------------------------------------------------------------------
/base/src/main/java/com/ccj/base/adapter/util/IAdapter.java:
--------------------------------------------------------------------------------
1 | package com.ccj.base.adapter.util;
2 |
3 | import android.support.annotation.Keep;
4 | import android.support.annotation.NonNull;
5 |
6 | import com.ccj.base.adapter.item.AdapterItem;
7 |
8 | import java.util.List;
9 |
10 |
11 | /**
12 | * @author ccj
13 | * @date 2017/3/22
14 | * 通用的adapter必须实现的接口,作为方法名统一的一个规范
15 | */
16 | public interface IAdapter {
17 |
18 | /**
19 | * @param data 设置数据源
20 | */
21 | void setData(@NonNull List data);
22 |
23 | List getData();
24 |
25 | /**
26 | * @param t list中的一条数据
27 | * @return 强烈建议返回string, int, bool类似的基础对象做type,不要返回data中的某个对象
28 | */
29 | Object getItemType(T t);
30 |
31 | /**
32 | * 当缓存中无法得到所需item时才会调用
33 | *
34 | * @param type 通过{@link #getItemType(Object)}得到的type
35 | * @return 任意类型的 AdapterItem
36 | */
37 | @Keep
38 | @NonNull
39 | AdapterItem createItem(Object type);
40 |
41 | /**
42 | * 如果放入item的最终数据和list中的每一条数据类型是不同的,可以通过此方法进行转换
43 | *
44 | * @param data 从原始的list中取得得数据
45 | * @param type item的类型
46 | * @return 放入adapterItem的最终数据
47 | */
48 | @Keep
49 | @NonNull
50 | Object getConvertedData(T data, Object type);
51 |
52 | /**
53 | * 通知adapter更新当前页面的所有数据
54 | */
55 | void notifyDataSetChanged();
56 |
57 | /**
58 | * 得到当前要渲染的最后一个item的position
59 | */
60 | int getCurrentPosition();
61 | }
62 |
--------------------------------------------------------------------------------
/base/src/main/java/com/ccj/base/adapter/util/ItemTypeUtil.java:
--------------------------------------------------------------------------------
1 | package com.ccj.base.adapter.util;
2 |
3 | import android.support.annotation.VisibleForTesting;
4 |
5 | import java.util.HashMap;
6 |
7 | /**
8 | * @author ccj
9 | * @date 2017/3/22
10 | */
11 | @VisibleForTesting
12 | /*package*/ public class ItemTypeUtil {
13 |
14 | private HashMap