From 344e4685e56463e97d8b07fe1389c155bbb91179 Mon Sep 17 00:00:00 2001 From: Jieyi Date: Thu, 29 Nov 2018 12:06:22 +0900 Subject: [PATCH] Feature: added the two functions for adding and deleting an element. --- .../adaptiverecyclerview/AdaptiveAdapter.kt | 17 +++++-- sample/src/main/AndroidManifest.xml | 10 ++-- .../com/devrapid/example/DeletableActivity.kt | 46 +++++++++++++++++++ .../com/devrapid/example/ExpandAdapter.kt | 3 +- .../main/res/layout/activity_deletable.xml | 21 +++++++++ 5 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 sample/src/main/java/com/devrapid/example/DeletableActivity.kt create mode 100644 sample/src/main/res/layout/activity_deletable.xml diff --git a/adaptiverecyclerview/src/main/java/com/devrapid/adaptiverecyclerview/AdaptiveAdapter.kt b/adaptiverecyclerview/src/main/java/com/devrapid/adaptiverecyclerview/AdaptiveAdapter.kt index 1c072fc..8ec0fdf 100644 --- a/adaptiverecyclerview/src/main/java/com/devrapid/adaptiverecyclerview/AdaptiveAdapter.kt +++ b/adaptiverecyclerview/src/main/java/com/devrapid/adaptiverecyclerview/AdaptiveAdapter.kt @@ -37,6 +37,7 @@ abstract class AdaptiveAdapter, VH : Re } } open var diffUtil: AdaptiveDiffUtil = MultiDiffUtil() + open var useDiffUtilUpdate = true protected abstract var typeFactory: VT protected abstract var dataList: MutableList @@ -74,7 +75,11 @@ abstract class AdaptiveAdapter, VH : Re open fun appendList(list: MutableList) { 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 } } @@ -86,10 +91,11 @@ abstract class AdaptiveAdapter, 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) { @@ -97,9 +103,12 @@ abstract class AdaptiveAdapter, VH : Re val res = DiffUtil.calculateDiff(diffUtil.apply { oldList = dataList this.newList = newList - }, true) + }) dataList = newList - res.dispatchUpdatesTo(this) + if (useDiffUtilUpdate) + res.dispatchUpdatesTo(this) + else + notifyDataSetChanged() } } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index e5a6a0c..c945e4c 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -12,10 +12,14 @@ - + + + + + + - - \ No newline at end of file + diff --git a/sample/src/main/java/com/devrapid/example/DeletableActivity.kt b/sample/src/main/java/com/devrapid/example/DeletableActivity.kt new file mode 100644 index 0000000..7ba5413 --- /dev/null +++ b/sample/src/main/java/com/devrapid/example/DeletableActivity.kt @@ -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 = 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 + } +} diff --git a/sample/src/main/java/com/devrapid/example/ExpandAdapter.kt b/sample/src/main/java/com/devrapid/example/ExpandAdapter.kt index 6076a9b..b0dc9ec 100644 --- a/sample/src/main/java/com/devrapid/example/ExpandAdapter.kt +++ b/sample/src/main/java/com/devrapid/example/ExpandAdapter.kt @@ -12,7 +12,8 @@ import com.devrapid.example.viewtype.MultiTypeFactory class ExpandAdapter(override var dataList: MutableList): AdaptiveAdapter>() { override var typeFactory: MultiTypeFactory = MultiTypeFactory() - private val originalParentPosition: MutableList = MutableList(this.dataList.size, { 0 }) + // override var useDiffUtilUpdate = false + private val originalParentPosition: MutableList = MutableList(this.dataList.size) { 0 } class ExpandDiffUtil(private var oldList: MutableList, private var newList: MutableList): DiffUtil.Callback() { diff --git a/sample/src/main/res/layout/activity_deletable.xml b/sample/src/main/res/layout/activity_deletable.xml new file mode 100644 index 0000000..b8ac857 --- /dev/null +++ b/sample/src/main/res/layout/activity_deletable.xml @@ -0,0 +1,21 @@ + + + + +