Skip to content

Commit

Permalink
Fix bug in getting indexes when section item is not visible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianzhu Qiao committed Nov 28, 2020
1 parent 1f76c32 commit 0992d6c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
28 changes: 18 additions & 10 deletions form/src/main/java/com/feiyilin/form/FormItemSection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ open class FormItemSection(private val visible: Boolean=true): FormItem() {
_itemsVisible.remove(item)
if (update) {
adapter?.activity?.runOnUiThread {
val start = adapter?.indexOf(this) ?: -1
val start = adapter?.startOfSection(this) ?: -1
if (start != -1) {
adapter?.notifyItemRemoved(start + index)
}
Expand Down Expand Up @@ -173,11 +173,17 @@ open class FormItemSection(private val visible: Boolean=true): FormItem() {
if (after.section != this) {
return false
}
val index = items.indexOf(after)
var index = items.indexOf(after)
if (index == -1) {
return false
if (after == this) {
index = 0
} else {
return false
}
} else {
index += 1
}
return add(index + 1, item, update)
return add(index, item, update)
}
/**
* add an item at given position
Expand Down Expand Up @@ -207,7 +213,7 @@ open class FormItemSection(private val visible: Boolean=true): FormItem() {
_itemsVisible.add(count, item)
if (update) {
adapter.activity?.runOnUiThread {
val start = adapter.indexOf(this)
val start = adapter.startOfSection(this)
if (start != -1) {
adapter.notifyItemInserted(start + itemsVisible.indexOf(item))
}
Expand Down Expand Up @@ -253,11 +259,13 @@ open class FormItemSection(private val visible: Boolean=true): FormItem() {
val offset = update(item)
if (offset != -1) {
adapter?.activity?.runOnUiThread {
val index = adapter?.indexOf(this) ?: -1
if (item.hidden) {
adapter?.notifyItemRemoved(index + offset)
} else {
adapter?.notifyItemInserted(index + offset)
val index = adapter?.startOfSection(this) ?: -1
if (index != -1) {
if (item.hidden) {
adapter?.notifyItemRemoved(index + offset)
} else {
adapter?.notifyItemInserted(index + offset)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion form/src/main/java/com/feiyilin/form/FormRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ open class FormRecyclerAdapter(
fun update() {
activity?.runOnUiThread {
sections.forEach {
it.adapter = this
it.update()
}
notifyDataSetChanged()
Expand Down Expand Up @@ -732,7 +733,7 @@ open class FormRecyclerAdapter(
* If section is visible, it return the index of its first item; otherwise, return the index of
* its first child if the section is visible.
*/
protected fun startOfSection(section: FormItemSection): Int {
internal fun startOfSection(section: FormItemSection): Int {
var index = 0
for (sec in sections) {
if (sec == section) {
Expand Down
32 changes: 22 additions & 10 deletions form/src/main/java/com/feiyilin/form/FormSwipeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ abstract class FormSwipeHelper: ItemTouchHelper.SimpleCallback(ItemTouchHelper.D
swipedDirection = direction

if (swipedPosition >= 0) {
val item = getFormItem(swipedPosition)
var item: FormItem? = null
if (viewHolder is FormViewHolder) {
item = viewHolder.item
}
val actions = if (swipedDirection == ItemTouchHelper.LEFT) {
item.trailingSwipe
item?.trailingSwipe
} else {
item.leadingSwipe
item?.leadingSwipe
}
if (isDestructive(actions)) {
if (actions != null && isDestructive(actions)) {
onActionClicked(swipedPosition, actions[0])
}
}
Expand All @@ -64,15 +67,17 @@ abstract class FormSwipeHelper: ItemTouchHelper.SimpleCallback(ItemTouchHelper.D
viewHolder: RecyclerView.ViewHolder
): Int {
var flag = super.getMovementFlags(recyclerView, viewHolder)
val position = viewHolder.adapterPosition
val item = getFormItem(position)
if (item.leadingSwipe.isEmpty()) {
var item : FormItem? = null
if (viewHolder is FormViewHolder) {
item = viewHolder.item
}
if (item?.leadingSwipe?.isEmpty() == true) {
flag = flag xor ItemTouchHelper.Callback.makeFlag(
ItemTouchHelper.ACTION_STATE_SWIPE,
ItemTouchHelper.RIGHT
)
}
if (item.trailingSwipe.isEmpty()) {
if (item?.trailingSwipe?.isEmpty() == true) {
flag = flag xor ItemTouchHelper.Callback.makeFlag(
ItemTouchHelper.ACTION_STATE_SWIPE,
ItemTouchHelper.LEFT
Expand Down Expand Up @@ -216,9 +221,13 @@ abstract class FormSwipeHelper: ItemTouchHelper.SimpleCallback(ItemTouchHelper.D
val position = viewHolder.adapterPosition
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && position == swipeingPosition) {
val itemView = viewHolder.itemView

if (dX < 0) {
// swipe to left
val actions = getFormItem(position).trailingSwipe
var actions: List<FormSwipeAction> = listOf()
if (viewHolder is FormViewHolder) {
actions = viewHolder.item?.trailingSwipe ?: listOf()
}
if (actions.isNotEmpty()) {
if (!isDestructive(actions)) {
val minSwipedOffset = -recyclerView.width.toFloat()
Expand All @@ -240,7 +249,10 @@ abstract class FormSwipeHelper: ItemTouchHelper.SimpleCallback(ItemTouchHelper.D
}
if (dX > 0) {
// swipe to right
val actions = getFormItem(position).leadingSwipe
var actions: List<FormSwipeAction> = listOf()
if (viewHolder is FormViewHolder) {
actions = viewHolder.item?.leadingSwipe ?: listOf()
}
if (actions.isNotEmpty()) {
if (!isDestructive(actions)) {
val maxSwipedOffset = recyclerView.width.toFloat()
Expand Down

0 comments on commit 0992d6c

Please sign in to comment.