commit
9d6cd71128
@ -1,60 +1,48 @@ |
||||
package io.legado.app.ui.book.toc |
||||
|
||||
import android.view.LayoutInflater |
||||
import android.content.Context |
||||
import android.view.ViewGroup |
||||
import androidx.paging.PagedListAdapter |
||||
import androidx.recyclerview.widget.DiffUtil |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import io.legado.app.base.adapter.ItemViewHolder |
||||
import io.legado.app.base.adapter.RecyclerAdapter |
||||
import io.legado.app.data.entities.Bookmark |
||||
import io.legado.app.databinding.ItemBookmarkBinding |
||||
import splitties.views.onLongClick |
||||
|
||||
class BookmarkAdapter(val callback: Callback) : PagedListAdapter<Bookmark, BookmarkAdapter.MyViewHolder>(DIFF_CALLBACK) { |
||||
class BookmarkAdapter(context: Context, val callback: Callback) : |
||||
RecyclerAdapter<Bookmark, ItemBookmarkBinding>(context) { |
||||
|
||||
companion object { |
||||
|
||||
@JvmField |
||||
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Bookmark>() { |
||||
override fun areItemsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean = |
||||
oldItem.time == newItem.time |
||||
|
||||
override fun areContentsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean = |
||||
oldItem.time == newItem.time |
||||
&& oldItem.bookUrl == newItem.bookUrl |
||||
&& oldItem.chapterName == newItem.chapterName |
||||
&& oldItem.content == newItem.content |
||||
} |
||||
override fun getViewBinding(parent: ViewGroup): ItemBookmarkBinding { |
||||
return ItemBookmarkBinding.inflate(inflater, parent, false) |
||||
} |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
||||
val binding = |
||||
ItemBookmarkBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
||||
return MyViewHolder(binding) |
||||
override fun convert( |
||||
holder: ItemViewHolder, |
||||
binding: ItemBookmarkBinding, |
||||
item: Bookmark, |
||||
payloads: MutableList<Any> |
||||
) { |
||||
binding.tvChapterName.text = item.chapterName |
||||
binding.tvBookText.text = item.bookText |
||||
binding.tvContent.text = item.content |
||||
} |
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
||||
getItem(position)?.let { |
||||
holder.bind(it, callback) |
||||
} |
||||
override fun registerListener(holder: ItemViewHolder, binding: ItemBookmarkBinding) { |
||||
binding.root.setOnClickListener { |
||||
getItem(holder.layoutPosition)?.let { bookmark -> |
||||
callback.onClick(bookmark) |
||||
} |
||||
|
||||
class MyViewHolder(val binding: ItemBookmarkBinding) : RecyclerView.ViewHolder(binding.root) { |
||||
|
||||
fun bind(bookmark: Bookmark, callback: Callback?) = with(binding) { |
||||
tvChapterName.text = bookmark.chapterName |
||||
tvBookText.text = bookmark.bookText |
||||
tvContent.text = bookmark.content |
||||
itemView.setOnClickListener { |
||||
callback?.onClick(bookmark) |
||||
} |
||||
itemView.onLongClick { |
||||
callback?.onLongClick(bookmark) |
||||
binding.root.onLongClick { |
||||
getItem(holder.layoutPosition)?.let { bookmark -> |
||||
callback.onLongClick(bookmark) |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
interface Callback { |
||||
fun onClick(bookmark: Bookmark) |
||||
fun onLongClick(bookmark: Bookmark) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,82 @@ |
||||
package io.legado.app.ui.main.bookshelf |
||||
|
||||
import android.content.Context |
||||
import android.gesture.GestureOverlayView.ORIENTATION_HORIZONTAL |
||||
import android.util.AttributeSet |
||||
import android.view.MotionEvent |
||||
import android.view.View |
||||
import android.view.ViewConfiguration |
||||
import android.widget.LinearLayout |
||||
import androidx.viewpager2.widget.ViewPager2 |
||||
import io.legado.app.R |
||||
import io.legado.app.ui.main.MainActivity |
||||
import io.legado.app.utils.activity |
||||
import kotlin.math.absoluteValue |
||||
import kotlin.math.sign |
||||
|
||||
class RootView : LinearLayout { |
||||
|
||||
constructor(context: Context) : super(context) |
||||
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) |
||||
|
||||
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop |
||||
private var initialX = 0f |
||||
private var initialY = 0f |
||||
|
||||
private val parentViewPager: ViewPager2? |
||||
get() = (activity as? MainActivity)?.getViewPager() |
||||
private val childViewPager: View? |
||||
get() = findViewById(R.id.view_pager_bookshelf) |
||||
|
||||
private fun canChildScroll(orientation: Int, delta: Float): Boolean { |
||||
val direction = -delta.sign.toInt() |
||||
return when (orientation) { |
||||
0 -> childViewPager?.canScrollHorizontally(direction) ?: false |
||||
1 -> childViewPager?.canScrollVertically(direction) ?: false |
||||
else -> throw IllegalArgumentException() |
||||
} |
||||
} |
||||
|
||||
override fun onInterceptTouchEvent(e: MotionEvent): Boolean { |
||||
handleInterceptTouchEvent(e) |
||||
return super.onInterceptTouchEvent(e) |
||||
} |
||||
|
||||
private fun handleInterceptTouchEvent(e: MotionEvent) { |
||||
val orientation = parentViewPager?.orientation ?: return |
||||
// Early return if child can't scroll in same direction as parent |
||||
if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) { |
||||
return |
||||
} |
||||
|
||||
if (e.action == MotionEvent.ACTION_DOWN) { |
||||
initialX = e.x |
||||
initialY = e.y |
||||
parent.requestDisallowInterceptTouchEvent(true) |
||||
} else if (e.action == MotionEvent.ACTION_MOVE) { |
||||
val dx = e.x - initialX |
||||
val dy = e.y - initialY |
||||
val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL |
||||
// assuming ViewPager2 touch-slop is 2x touch-slop of child |
||||
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f |
||||
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f |
||||
|
||||
if (scaledDx > touchSlop || scaledDy > touchSlop) { |
||||
|
||||
if (isVpHorizontal == (scaledDy > scaledDx)) { |
||||
// Gesture is perpendicular, allow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(false) |
||||
} else { |
||||
// Gesture is parallel, query child if movement in that direction is possible |
||||
if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) { |
||||
// Child can scroll, disallow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(true) |
||||
} else { |
||||
// Child cannot scroll, allow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(false) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,41 +0,0 @@ |
||||
package io.legado.app.ui.widget.recycler |
||||
|
||||
import android.content.Context |
||||
import android.util.AttributeSet |
||||
import android.view.MotionEvent |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import kotlin.math.abs |
||||
|
||||
class RecyclerViewAtViewPager2(context: Context, attrs: AttributeSet?) : |
||||
RecyclerView(context, attrs) { |
||||
|
||||
private var startX: Int = 0 |
||||
private var startY: Int = 0 |
||||
|
||||
override fun dispatchTouchEvent(ev: MotionEvent): Boolean { |
||||
when (ev.action) { |
||||
MotionEvent.ACTION_DOWN -> { |
||||
startX = ev.x.toInt() |
||||
startY = ev.y.toInt() |
||||
parent.requestDisallowInterceptTouchEvent(true) |
||||
} |
||||
MotionEvent.ACTION_MOVE -> { |
||||
val endX = ev.x.toInt() |
||||
val endY = ev.y.toInt() |
||||
val disX = abs(endX - startX) |
||||
val disY = abs(endY - startY) |
||||
if (disX > disY) { |
||||
parent.requestDisallowInterceptTouchEvent(canScrollHorizontally(startX - endX)) |
||||
} else { |
||||
parent.requestDisallowInterceptTouchEvent(canScrollVertically(startY - endY)) |
||||
} |
||||
} |
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent( |
||||
false |
||||
) |
||||
} |
||||
return super.dispatchTouchEvent(ev) |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue