commit
9d6cd71128
@ -1,60 +1,48 @@ |
|||||||
package io.legado.app.ui.book.toc |
package io.legado.app.ui.book.toc |
||||||
|
|
||||||
import android.view.LayoutInflater |
import android.content.Context |
||||||
import android.view.ViewGroup |
import android.view.ViewGroup |
||||||
import androidx.paging.PagedListAdapter |
import io.legado.app.base.adapter.ItemViewHolder |
||||||
import androidx.recyclerview.widget.DiffUtil |
import io.legado.app.base.adapter.RecyclerAdapter |
||||||
import androidx.recyclerview.widget.RecyclerView |
|
||||||
import io.legado.app.data.entities.Bookmark |
import io.legado.app.data.entities.Bookmark |
||||||
import io.legado.app.databinding.ItemBookmarkBinding |
import io.legado.app.databinding.ItemBookmarkBinding |
||||||
import splitties.views.onLongClick |
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 { |
override fun getViewBinding(parent: ViewGroup): ItemBookmarkBinding { |
||||||
|
return ItemBookmarkBinding.inflate(inflater, parent, false) |
||||||
@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 onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
|
||||||
val binding = |
|
||||||
ItemBookmarkBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
|
||||||
return MyViewHolder(binding) |
|
||||||
} |
} |
||||||
|
|
||||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
override fun convert( |
||||||
getItem(position)?.let { |
holder: ItemViewHolder, |
||||||
holder.bind(it, callback) |
binding: ItemBookmarkBinding, |
||||||
} |
item: Bookmark, |
||||||
|
payloads: MutableList<Any> |
||||||
|
) { |
||||||
|
binding.tvChapterName.text = item.chapterName |
||||||
|
binding.tvBookText.text = item.bookText |
||||||
|
binding.tvContent.text = item.content |
||||||
} |
} |
||||||
|
|
||||||
class MyViewHolder(val binding: ItemBookmarkBinding) : RecyclerView.ViewHolder(binding.root) { |
override fun registerListener(holder: ItemViewHolder, binding: ItemBookmarkBinding) { |
||||||
|
binding.root.setOnClickListener { |
||||||
fun bind(bookmark: Bookmark, callback: Callback?) = with(binding) { |
getItem(holder.layoutPosition)?.let { bookmark -> |
||||||
tvChapterName.text = bookmark.chapterName |
callback.onClick(bookmark) |
||||||
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 { |
interface Callback { |
||||||
fun onClick(bookmark: Bookmark) |
fun onClick(bookmark: Bookmark) |
||||||
fun onLongClick(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) |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
@ -1,67 +1,90 @@ |
|||||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||||
xmlns:tools="http://schemas.android.com/tools" |
xmlns:tools="http://schemas.android.com/tools" |
||||||
|
android:id="@+id/cv_content" |
||||||
android:layout_width="match_parent" |
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content" |
android:layout_height="wrap_content" |
||||||
android:orientation="vertical" |
android:clickable="true" |
||||||
android:paddingTop="8dp" |
android:focusable="true" |
||||||
android:paddingLeft="8dp" |
tools:ignore="UnusedAttribute"> |
||||||
android:paddingRight="8dp"> |
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout |
|
||||||
|
<LinearLayout |
||||||
android:layout_width="match_parent" |
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content"> |
android:layout_height="wrap_content" |
||||||
|
android:orientation="vertical" |
||||||
|
android:paddingTop="8dp" |
||||||
|
android:paddingLeft="8dp" |
||||||
|
android:paddingRight="8dp" |
||||||
|
app:layout_constraintTop_toTopOf="parent"> |
||||||
|
|
||||||
<io.legado.app.ui.widget.image.CoverImageView |
<androidx.constraintlayout.widget.ConstraintLayout |
||||||
android:id="@+id/iv_cover" |
|
||||||
android:layout_width="match_parent" |
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content" |
android:layout_height="wrap_content"> |
||||||
android:layout_margin="8dp" |
|
||||||
android:scaleType="centerCrop" |
<io.legado.app.ui.widget.image.CoverImageView |
||||||
android:src="@drawable/image_cover_default" |
android:id="@+id/iv_cover" |
||||||
android:transitionName="img_cover" |
android:layout_width="match_parent" |
||||||
app:layout_constraintTop_toTopOf="parent" |
android:layout_height="wrap_content" |
||||||
tools:ignore="UnusedAttribute" /> |
android:layout_margin="8dp" |
||||||
|
android:scaleType="centerCrop" |
||||||
|
android:src="@drawable/image_cover_default" |
||||||
|
android:transitionName="img_cover" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:ignore="UnusedAttribute" /> |
||||||
|
|
||||||
|
<io.legado.app.ui.widget.text.BadgeView |
||||||
|
android:id="@+id/bv_unread" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_gravity="right" |
||||||
|
android:includeFontPadding="false" |
||||||
|
app:layout_constraintRight_toRightOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:ignore="RtlHardcoded" /> |
||||||
|
|
||||||
|
<io.legado.app.ui.widget.anima.RotateLoading |
||||||
|
android:id="@+id/rl_loading" |
||||||
|
android:layout_width="22dp" |
||||||
|
android:layout_height="22dp" |
||||||
|
android:layout_gravity="right" |
||||||
|
android:visibility="invisible" |
||||||
|
app:hide_mode="invisible" |
||||||
|
app:layout_constraintRight_toRightOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
app:loading_width="2dp" |
||||||
|
tools:ignore="RtlHardcoded" /> |
||||||
|
|
||||||
<io.legado.app.ui.widget.text.BadgeView |
</androidx.constraintlayout.widget.ConstraintLayout> |
||||||
android:id="@+id/bv_unread" |
|
||||||
android:layout_width="wrap_content" |
<TextView |
||||||
|
android:id="@+id/tv_name" |
||||||
|
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content" |
android:layout_height="wrap_content" |
||||||
android:layout_gravity="right" |
android:layout_marginTop="6dp" |
||||||
|
android:ellipsize="end" |
||||||
|
android:gravity="top|center_horizontal" |
||||||
android:includeFontPadding="false" |
android:includeFontPadding="false" |
||||||
app:layout_constraintRight_toRightOf="parent" |
android:lines="2" |
||||||
app:layout_constraintTop_toTopOf="parent" |
android:text="@string/book_name" |
||||||
tools:ignore="RtlHardcoded" /> |
android:textColor="@color/primaryText" |
||||||
|
android:textSize="12sp" |
||||||
<io.legado.app.ui.widget.anima.RotateLoading |
tools:ignore="RtlHardcoded,RtlSymmetry" /> |
||||||
android:id="@+id/rl_loading" |
|
||||||
android:layout_width="22dp" |
|
||||||
android:layout_height="22dp" |
|
||||||
android:layout_gravity="right" |
|
||||||
android:visibility="invisible" |
|
||||||
app:hide_mode="invisible" |
|
||||||
app:layout_constraintRight_toRightOf="parent" |
|
||||||
app:layout_constraintTop_toTopOf="parent" |
|
||||||
app:loading_width="2dp" |
|
||||||
tools:ignore="RtlHardcoded" /> |
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout> |
</LinearLayout> |
||||||
|
|
||||||
<TextView |
<View |
||||||
android:id="@+id/tv_name" |
android:id="@+id/vw_foreground" |
||||||
android:layout_width="match_parent" |
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content" |
android:layout_height="0dp" |
||||||
android:layout_marginTop="6dp" |
android:background="?android:attr/selectableItemBackground" |
||||||
android:ellipsize="end" |
app:layout_constraintBottom_toBottomOf="parent" |
||||||
android:gravity="top|center_horizontal" |
app:layout_constraintTop_toTopOf="parent" |
||||||
android:includeFontPadding="false" |
app:layout_constraintVertical_bias="0.0" |
||||||
android:lines="2" |
tools:layout_editor_absoluteX="0dp" /> |
||||||
android:text="@string/book_name" |
|
||||||
android:textColor="@color/primaryText" |
</androidx.constraintlayout.widget.ConstraintLayout> |
||||||
android:textSize="12sp" |
|
||||||
tools:ignore="RtlHardcoded,RtlSymmetry" /> |
|
||||||
|
|
||||||
</LinearLayout> |
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in new issue