-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathViewPagerAdapter.kt
More file actions
78 lines (62 loc) · 1.95 KB
/
ViewPagerAdapter.kt
File metadata and controls
78 lines (62 loc) · 1.95 KB
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
package com.reactnativepagerview
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView.Adapter
import java.util.*
class ViewPagerAdapter() : Adapter<ViewPagerViewHolder>() {
private val childrenViews: ArrayList<View> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewPagerViewHolder {
return ViewPagerViewHolder.create(parent)
}
override fun onBindViewHolder(holder: ViewPagerViewHolder, index: Int) {
val container: FrameLayout = holder.container
val child = getChildAt(index)
holder.setIsRecyclable(false)
if (container.childCount > 0) {
container.removeAllViews()
}
if (child.parent != null) {
(child.parent as FrameLayout).removeView(child)
}
container.addView(child)
}
override fun onViewRecycled(holder: ViewPagerViewHolder) {
super.onViewRecycled(holder)
// Clean up the holder's container to prevent memory leaks
holder.container.removeAllViews()
}
override fun getItemCount(): Int {
return childrenViews.size
}
fun addChild(child: View, index: Int) {
childrenViews.add(index, child)
notifyItemInserted(index)
}
fun getChildAt(index: Int): View {
return childrenViews[index]
}
fun removeChild(child: View) {
val index = childrenViews.indexOf(child)
if(index > -1) {
removeChildAt(index)
}
}
fun removeAll() {
for (index in 1..childrenViews.size) {
val child = childrenViews[index-1]
if (child.parent?.parent != null) {
(child.parent.parent as ViewGroup).removeView(child.parent as View)
}
}
val removedChildrenCount = childrenViews.size
childrenViews.clear()
notifyItemRangeRemoved(0, removedChildrenCount)
}
fun removeChildAt(index: Int) {
if (index >= 0 && index < childrenViews.size) {
childrenViews.removeAt(index)
notifyItemRemoved(index)
}
}
}