lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
package cn.sinata.xldutils.adapter.util
 
import android.util.SparseArray
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.view.SimpleDraweeView
 
/**
 *
 */
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val mConvertView = view
    @Suppress("UNCHECKED_CAST")
    fun <T : View> bind(viewId: Int): T {// 通过ViewId得到View
        var viewArray: SparseArray<View>?
        if (mConvertView.tag == null) {
            viewArray = SparseArray<View>()
            mConvertView.tag = viewArray
        } else {
            viewArray = mConvertView.tag as SparseArray<View>
        }
 
        var childView: View? = viewArray.get(viewId)
        if (childView == null) {
            childView = mConvertView.findViewById(viewId)
            viewArray.put(viewId, childView)
        }
        return childView as T
 
    }
 
    /**
     * 设置TextView文字
 
     * @param resId TextView的id
     * *
     * @param text  文字内容
     */
    fun setText(resId: Int, text: CharSequence?) {
        if (bind<View>(resId) is TextView)
            bind<TextView>(resId).text = text
    }
 
    fun setImageURI(@IdRes resId: Int, url: String?) {
        val view = bind<View>(resId)
        if (view is SimpleDraweeView) {
            view.setImageURI(url)
        }
    }
 
    fun setOnClickListener(@IdRes resId: Int, l: View.OnClickListener) {
        bind<View>(resId).setOnClickListener(l)
    }
 
}