Skip to content

Commit 344e468

Browse files
committed
Feature: added the two functions for adding and deleting an element.
1 parent ac2b0ee commit 344e468

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

adaptiverecyclerview/src/main/java/com/devrapid/adaptiverecyclerview/AdaptiveAdapter.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
3737
}
3838
}
3939
open var diffUtil: AdaptiveDiffUtil<VT, M> = MultiDiffUtil()
40+
open var useDiffUtilUpdate = true
4041

4142
protected abstract var typeFactory: VT
4243
protected abstract var dataList: MutableList<M>
@@ -74,7 +75,11 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
7475
open fun appendList(list: MutableList<M>) {
7576
val startIndex = dataList.size
7677
val newList = dataList.toMutableList().apply { addAll(startIndex, list) }
77-
// notifyItemRangeChanged(startIndex, list.size)
78+
updateList { newList }
79+
}
80+
81+
open fun append(item: M) {
82+
val newList = dataList.toMutableList().apply { add(item) }
7883
updateList { newList }
7984
}
8085

@@ -86,20 +91,24 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
8691
val newList = dataList.toMutableList()
8792

8893
repeat(endIndex - startIndex + 1) { newList.removeAt(startIndex) }
89-
// notifyDataSetChanged()
9094
updateList { newList }
9195
}
9296

97+
open fun dropAt(index: Int) = dropList(index, index)
98+
9399
open fun clearList() = dropList(0, dataList.size - 1)
94100

95101
private fun updateList(getNewListBlock: () -> MutableList<M>) {
96102
val newList = getNewListBlock()
97103
val res = DiffUtil.calculateDiff(diffUtil.apply {
98104
oldList = dataList
99105
this.newList = newList
100-
}, true)
106+
})
101107

102108
dataList = newList
103-
res.dispatchUpdatesTo(this)
109+
if (useDiffUtilUpdate)
110+
res.dispatchUpdatesTo(this)
111+
else
112+
notifyDataSetChanged()
104113
}
105114
}

sample/src/main/AndroidManifest.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
<activity android:name=".MainActivity">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN"/>
15-
1615
<category android:name="android.intent.category.LAUNCHER"/>
1716
</intent-filter>
1817
</activity>
18+
<activity android:name=".DeletableActivity">
19+
<!--<intent-filter>-->
20+
<!--<action android:name="android.intent.action.MAIN"/>-->
21+
<!--<category android:name="android.intent.category.LAUNCHER"/>-->
22+
<!--</intent-filter>-->
23+
</activity>
1924
</application>
20-
21-
</manifest>
25+
</manifest>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.devrapid.example
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.recyclerview.widget.ItemTouchHelper
6+
import androidx.recyclerview.widget.ItemTouchHelper.DOWN
7+
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
8+
import androidx.recyclerview.widget.ItemTouchHelper.RIGHT
9+
import androidx.recyclerview.widget.ItemTouchHelper.UP
10+
import androidx.recyclerview.widget.LinearLayoutManager
11+
import androidx.recyclerview.widget.RecyclerView
12+
import com.devrapid.example.model.Person
13+
import kotlinx.android.synthetic.main.activity_deletable.recyclerView
14+
15+
class DeletableActivity : AppCompatActivity() {
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
setContentView(R.layout.activity_deletable)
19+
20+
val itemList: MutableList<IExpandVisitor> = mutableListOf(Person("Google"),
21+
Person("Facebook"),
22+
Person("Apple"),
23+
Person("Amazon"),
24+
Person("HTC"),
25+
Person("Banana"),
26+
Person("Grape"),
27+
Person("Airbnb"),
28+
Person("Jieyi"))
29+
val adapter = ExpandAdapter(itemList)
30+
31+
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(UP or DOWN, LEFT or RIGHT) {
32+
override fun onMove(
33+
recyclerView: RecyclerView,
34+
viewHolder: RecyclerView.ViewHolder,
35+
target: RecyclerView.ViewHolder
36+
) = true
37+
38+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
39+
val position = viewHolder.layoutPosition
40+
adapter.dropList(position, position)
41+
}
42+
}).attachToRecyclerView(recyclerView)
43+
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
44+
recyclerView.adapter = adapter
45+
}
46+
}

sample/src/main/java/com/devrapid/example/ExpandAdapter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import com.devrapid.example.viewtype.MultiTypeFactory
1212
class ExpandAdapter(override var dataList: MutableList<IExpandVisitor>):
1313
AdaptiveAdapter<MultiTypeFactory, IExpandVisitor, AdaptiveViewHolder<MultiTypeFactory, IExpandVisitor>>() {
1414
override var typeFactory: MultiTypeFactory = MultiTypeFactory()
15-
private val originalParentPosition: MutableList<Int> = MutableList(this.dataList.size, { 0 })
15+
// override var useDiffUtilUpdate = false
16+
private val originalParentPosition: MutableList<Int> = MutableList(this.dataList.size) { 0 }
1617

1718
class ExpandDiffUtil(private var oldList: MutableList<IExpandVisitor>,
1819
private var newList: MutableList<IExpandVisitor>): DiffUtil.Callback() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical">
8+
9+
<androidx.recyclerview.widget.RecyclerView
10+
android:id="@+id/recyclerView"
11+
android:layout_width="0dp"
12+
android:layout_height="0dp"
13+
android:layout_marginBottom="8dp"
14+
android:layout_marginEnd="8dp"
15+
android:layout_marginStart="8dp"
16+
android:layout_marginTop="8dp"
17+
app:layout_constraintBottom_toBottomOf="parent"
18+
app:layout_constraintEnd_toEndOf="parent"
19+
app:layout_constraintStart_toStartOf="parent"
20+
app:layout_constraintTop_toTopOf="parent"/>
21+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)