feat: 优化代码

pull/173/head
kunfei 5 years ago
parent d1a0be793b
commit 848fdb6a6d
  1. 5
      app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  3. 2
      app/src/main/res/menu/change_cover.xml
  4. 2
      app/src/main/res/menu/change_source.xml

@ -290,12 +290,11 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
return footerItems?.size() ?: 0
}
fun getItem(position: Int): ITEM? =
if (position in 0 until items.size) items[position] else null
fun getItem(position: Int): ITEM? = items.getOrNull(position)
fun getItemByLayoutPosition(position: Int): ITEM? {
val pos = position - getHeaderCount()
return if (pos in 0 until items.size) items[pos] else null
return items.getOrNull(pos)
}
fun getItems(): List<ITEM> = items

@ -125,11 +125,19 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
private var isMoved = false
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
if (getItem(srcPosition)?.order == getItem(targetPosition)?.order) {
for ((index, item) in getItems().withIndex()) {
item.order = index + 1
if (srcItem != null && targetItem != null) {
if (srcItem.order == targetItem.order) {
for ((index, item) in getItems().withIndex()) {
item.order = index + 1
}
} else {
val pos = srcItem.order
srcItem.order = targetItem.order
targetItem.order = pos
}
}
isMoved = true

@ -6,7 +6,7 @@
<item
android:id="@+id/menu_stop"
android:title="@string/stop"
android:icon="@drawable/ic_stop_black_24dp"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />

@ -14,7 +14,7 @@
<item
android:id="@+id/menu_stop"
android:title="@string/stop"
android:icon="@drawable/ic_stop_black_24dp"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />

Loading…
Cancel
Save