doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.doumee.lib_coremodel.view.recyclerview.adapter;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.DiffUtil;
 
import com.king.base.adapter.holder.ViewHolder;
 
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
 
public abstract class DiffBaseRecyclerAdapter<T,H extends ViewHolder> extends DiffHolderRecyclerAdapter<T,H> {
 
    private int layoutId;
 
    public DiffBaseRecyclerAdapter(Context context, @LayoutRes int layoutId,DiffUtil.ItemCallback<T> DIFF_CALLBACK) {
        super(context, DIFF_CALLBACK);
        this.layoutId = layoutId;
    }
 
    @Override
    public H buildHolder(View convertView, int viewType) {
        return createViewHolder(convertView);
    }
 
    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ViewGroup parent, int viewType) {
        return layoutInflater.inflate(layoutId,parent,false);
    }
 
    /**
     * 创建ViewHolder
     * @param view view
     * @return new ViewHolder
     */
    @SuppressWarnings("unchecked")
    protected H createViewHolder(View view) {
        Class temp = getClass();
        Class z = null;
        while (z == null && null != temp) {
            z = getInstancedGenericHClass(temp);
            temp = temp.getSuperclass();
        }
        H holder;
        // 泛型擦除会导致z为null
        if (z == null) {
            holder = (H) new ViewHolder(view);
        } else {
            holder = createGenericHInstance(z, view);
        }
        return holder != null ? holder : (H) new ViewHolder(view);
    }
 
    /**
     * 创建泛型{@link H}实例
     * @param z
     * @param view
     * @return
     */
    @SuppressWarnings("unchecked")
    private H createGenericHInstance(Class z, View view) {
        try {
            Constructor constructor;
            // 成员类和非静态类
            if (z.isMemberClass() && !Modifier.isStatic(z.getModifiers())) {
                constructor = z.getDeclaredConstructor(getClass(), View.class);
                constructor.setAccessible(true);
                return (H) constructor.newInstance(this, view);
            } else {
                constructor = z.getDeclaredConstructor(View.class);
                constructor.setAccessible(true);
                return (H) constructor.newInstance(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取泛型{@link H}
     *
     * @param z
     * @return
     */
    private Class getInstancedGenericHClass(Class z) {
        Type type = z.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            for (Type temp : types) {
                if (temp instanceof Class) {
                    Class tempClass = (Class) temp;
                    if (ViewHolder.class.isAssignableFrom(tempClass)) {
                        return tempClass;
                    }
                } else if (temp instanceof ParameterizedType) {
                    Type rawType = ((ParameterizedType) temp).getRawType();
                    if (rawType instanceof Class && ViewHolder.class.isAssignableFrom((Class<?>) rawType)) {
                        return (Class<?>) rawType;
                    }
                }
            }
        }
        return null;
    }
 
}