Skip to content

Commit

Permalink
Feature: added the two functions for adding and deleting an element.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Nov 29, 2018
1 parent ac2b0ee commit 344e468
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
}
}
open var diffUtil: AdaptiveDiffUtil<VT, M> = MultiDiffUtil()
open var useDiffUtilUpdate = true

protected abstract var typeFactory: VT
protected abstract var dataList: MutableList<M>
Expand Down Expand Up @@ -74,7 +75,11 @@ abstract class AdaptiveAdapter<VT : ViewTypeFactory, M : IVisitable<VT>, VH : Re
open fun appendList(list: MutableList<M>) {
val startIndex = dataList.size
val newList = dataList.toMutableList().apply { addAll(startIndex, list) }
// notifyItemRangeChanged(startIndex, list.size)
updateList { newList }
}

open fun append(item: M) {
val newList = dataList.toMutableList().apply { add(item) }
updateList { newList }
}

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

repeat(endIndex - startIndex + 1) { newList.removeAt(startIndex) }
// notifyDataSetChanged()
updateList { newList }
}

open fun dropAt(index: Int) = dropList(index, index)

open fun clearList() = dropList(0, dataList.size - 1)

private fun updateList(getNewListBlock: () -> MutableList<M>) {
val newList = getNewListBlock()
val res = DiffUtil.calculateDiff(diffUtil.apply {
oldList = dataList
this.newList = newList
}, true)
})

dataList = newList
res.dispatchUpdatesTo(this)
if (useDiffUtilUpdate)
res.dispatchUpdatesTo(this)
else
notifyDataSetChanged()
}
}
10 changes: 7 additions & 3 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DeletableActivity">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN"/>-->
<!--<category android:name="android.intent.category.LAUNCHER"/>-->
<!--</intent-filter>-->
</activity>
</application>

</manifest>
</manifest>
46 changes: 46 additions & 0 deletions sample/src/main/java/com/devrapid/example/DeletableActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.devrapid.example

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.DOWN
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.RIGHT
import androidx.recyclerview.widget.ItemTouchHelper.UP
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.devrapid.example.model.Person
import kotlinx.android.synthetic.main.activity_deletable.recyclerView

class DeletableActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_deletable)

val itemList: MutableList<IExpandVisitor> = mutableListOf(Person("Google"),
Person("Facebook"),
Person("Apple"),
Person("Amazon"),
Person("HTC"),
Person("Banana"),
Person("Grape"),
Person("Airbnb"),
Person("Jieyi"))
val adapter = ExpandAdapter(itemList)

ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(UP or DOWN, LEFT or RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
) = true

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.layoutPosition
adapter.dropList(position, position)
}
}).attachToRecyclerView(recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
}
}
3 changes: 2 additions & 1 deletion sample/src/main/java/com/devrapid/example/ExpandAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import com.devrapid.example.viewtype.MultiTypeFactory
class ExpandAdapter(override var dataList: MutableList<IExpandVisitor>):
AdaptiveAdapter<MultiTypeFactory, IExpandVisitor, AdaptiveViewHolder<MultiTypeFactory, IExpandVisitor>>() {
override var typeFactory: MultiTypeFactory = MultiTypeFactory()
private val originalParentPosition: MutableList<Int> = MutableList(this.dataList.size, { 0 })
// override var useDiffUtilUpdate = false
private val originalParentPosition: MutableList<Int> = MutableList(this.dataList.size) { 0 }

class ExpandDiffUtil(private var oldList: MutableList<IExpandVisitor>,
private var newList: MutableList<IExpandVisitor>): DiffUtil.Callback() {
Expand Down
21 changes: 21 additions & 0 deletions sample/src/main/res/layout/activity_deletable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 344e468

Please sign in to comment.