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 return footerItems?.size() ?: 0
} }
fun getItem(position: Int): ITEM? = fun getItem(position: Int): ITEM? = items.getOrNull(position)
if (position in 0 until items.size) items[position] else null
fun getItemByLayoutPosition(position: Int): ITEM? { fun getItemByLayoutPosition(position: Int): ITEM? {
val pos = position - getHeaderCount() 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 fun getItems(): List<ITEM> = items

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

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

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

Loading…
Cancel
Save