18034727885
2026-05-14 dad2ded3e9e9d2edf55c7ce10e791588b526961f
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.example.smart.ui.item.adapter
 
import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.example.basecore.util.startAct
import com.example.smart.R
import com.example.smart.bean.ImageDrawData
import com.example.smart.bean.ImageRef
import com.example.smart.oss.ImageRemotePathProvider
import com.example.smart.ui.icondraw.DrawActivity
import com.example.smart.ui.item.bean.ImageBean
import com.example.smart.util.openImageViewerScheme
 
 
class ImageShowAdapter(
    private val mContext: Context, val data: ArrayList<ImageBean>, val needDraw: Boolean = true,
    val showClear:Boolean = true
) :
    Adapter<ImageShowAdapter.PageViewHolder>() {
 
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
        return PageViewHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_add_image, parent, false)
        )
    }
 
    override fun getItemCount() = data.size
    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        holder.takeLayout.setOnClickListener {}
        val bean = data[position]
        holder.takeLayout.isVisible = false
        holder.pic_layout.isVisible = true
        val path = ImageRemotePathProvider.IMAGE_BASE_URL + bean.accessPath
        if (!TextUtils.isEmpty(bean.accessPath)) {
            Glide.with(mContext).load(path).into(holder.pic_img)
        } else {
            holder.takeLayout.isVisible = true
            holder.pic_layout.isVisible = false
        }
        if (needDraw) {
            holder.editView.visibility = View.VISIBLE
        } else {
            holder.editView.visibility = View.GONE
        }
 
 
        holder.editView.setOnClickListener {
            if (TextUtils.isEmpty(path)) {
                return@setOnClickListener
            }
            startAct<DrawActivity>(mContext) {
                putExtra("id", bean.uri)
                putExtra("path", path)
            }
        }
 
        holder.pic_img.setOnClickListener {
            val paths = ArrayList<String>()
            for (i in data) {
                paths.add(ImageRemotePathProvider.IMAGE_BASE_URL + i.accessPath)
            }
            openImageViewerScheme(mContext as Activity, paths, position)
        }
 
        if (showClear) {
            holder.clear_img.visibility = View.VISIBLE
        } else{
            holder.clear_img.visibility = View.GONE
        }
        holder.clear_img.setOnClickListener {
            data.remove(bean)
            notifyDataSetChanged()
        }
    }
 
    fun updateData(dataList: List<ImageBean>){
        data.clear()
        dataList.forEach { data.add(it) }
        notifyDataSetChanged()
    }
 
    fun notifyData(dataList: List<ImageRef>) {
        dataList.forEach {
            data.add(ImageBean(it.id, it.name))
        }
        notifyDataSetChanged()
    }
 
    fun notifyData() {
        notifyDataSetChanged()
    }
 
    /**
     * 更新绘制过的数据
     */
    fun updateDrawData(bean: ImageDrawData) {
        data.forEach {
            if (it.uri == bean.oldId) {
                it.accessPath = bean.name
                it.uri = bean.id
                notifyItemChanged(data.indexOf(it))
            }
        }
    }
 
    fun clearAndAddData(dataList: List<ImageBean>) {
        data.clear()
        data.addAll(dataList)
        notifyDataSetChanged()
    }
 
    fun addImage(bean: ImageBean) {
        data.add(bean)
//        notifyDataSetChanged()
        notifyItemInserted(data.size - 1)
    }
 
    fun updateImageBean(path: String) {
        if (data.size == 0) {
            return
        }
        val bean = data[data.size - 1]
        bean.accessPath = path
    }
 
    fun notifyItemShowImg(bean: ImageBean) {
        val index = data.indexOf(bean)
        if (index >= 0) {
            notifyItemChanged(index)
        }
    }
 
    fun deleteImage() {
        val index = data.size - 1
        data.remove(data[index])
        notifyItemRemoved(index)
    }
 
    fun deleteImage(position: Int) {
        val bean = data[position]
        data.remove(bean)
        notifyItemRemoved(position)
    }
 
 
    inner class PageViewHolder(itemView: View) : ViewHolder(itemView) {
 
        val takeLayout: View
        val pic_layout: View
        val pic_img: ImageView
        val clear_img: ImageView
 
        val editView: View
 
        init {
            takeLayout = itemView.findViewById(R.id.take_photo_layout)
            pic_layout = itemView.findViewById(R.id.pic_layout)
            pic_img = itemView.findViewById(R.id.pic_img)
            clear_img = itemView.findViewById(R.id.clear_img)
            editView = itemView.findViewById(R.id.draw_img)
        }
    }
 
 
}