【发布时间】:2021-06-22 15:22:35
【问题描述】:
我有一个 RecyclerView,它显示一个延伸到其父 ViewHolder 之外的 Button。我在按钮上添加了一个 clickListener 来显示一个 toast。如果单击 Button 并且单击的是 Button 父 ViewHolder 的区域,则显示 toast,但如果单击按钮但在其父 ViewHolder 之外,则不再显示 toast。
这是我目前拥有的
回收站视图:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:focusedByDefault="false"
android:scrollbars="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/indicator"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:reverseLayout="true"
app:stackFromEnd="true" />
物品视图:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="15dp"
android:layout_height="match_parent"
android:clipChildren="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clipToPadding="false"
android:elevation="0dp"
android:scaleY="-1.0">
<View
android:id="@+id/vertical"
android:layout_width="1dp"
android:layout_height="match_parent"
android:alpha="0.25"
android:background="#ffffff"
android:visibility="gone"
android:layout_marginVertical="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/annotation"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
android:scaleY="-1.0"
android:elevation="100dp"
android:text="TEST ANNOTATION"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
适配器:
class ChartAdapter(
private val dataSet: MutableList<Float>,
private val context: Context
) : RecyclerView.Adapter<ChartAdapter.ViewHolder>() {
private var scaleFactor: Float = 1.0f
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val layout: ConstraintLayout = view.findViewById(R.id.layout)
val vertical: View = view.findViewById(R.id.vertical)
val annotation: View = view.findViewById(R.id.annotation)
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(
LayoutInflater.from(viewGroup.context)
.inflate(R.layout.layout_quadrant, viewGroup, false)
)
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.layout.layoutParams = ConstraintLayout.LayoutParams(
(39 * this.scaleFactor).roundToInt(),
ConstraintLayout.LayoutParams.MATCH_PARENT
)
ConstraintSet().apply {
clone(viewHolder.layout)
applyTo(viewHolder.layout)
}
viewHolder.annotation.scaleX = this.scaleFactor
viewHolder.annotation.scaleY = this.scaleFactor * -1
viewHolder.vertical.visibility = when {
position % 5 == 0 || position == dataSet.size - 1 -> View.VISIBLE
else -> View.GONE
}
viewHolder.annotation.visibility = when (position) {
15 -> View.VISIBLE
else -> View.GONE
}
viewHolder.annotation.setOnClickListener {
Toast.makeText(context, "annotation clicked!", Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount() = dataSet.size
fun setScaleFactor(scaleFactor: Float) {
this.scaleFactor = scaleFactor
notifyDataSetChanged()
}
}
如您所见,我尝试将 android:clickable="false"、android:focusable="false" 和 android:focusableInTouchMode="false" 添加到 Item 布局和 RecyclerView 以防止它拦截点击操作,但不幸的是,父级总是一直拦截点击防止按钮被点击。
我也在 ViewHolder 上试过这个,但没有运气
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val layout: ConstraintLayout = view.findViewById(R.id.layout)
val vertical: View = view.findViewById(R.id.vertical)
val annotation: View = view.findViewById(R.id.annotation)
init {
view.layoutParams = WindowManager.LayoutParams().apply {
height = WindowManager.LayoutParams.MATCH_PARENT;
width = WindowManager.LayoutParams.WRAP_CONTENT;
flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
}
view.isClickable = false
view.isFocusable = false
view.isFocusableInTouchMode = false
val itemTouchListener = View.OnTouchListener { v, event -> false }
view.setOnTouchListener(itemTouchListener)
}
}
【问题讨论】:
-
我需要问为什么。为什么按钮“扩展其父级”。你为什么要添加所有那些可聚焦/可点击/等。为什么使用触摸监听器(而不是点击监听器)。这么多问题......你到底想在这里完成什么。
-
由于您的按钮将延伸到父布局之外,是什么阻止您更改父布局的宽度以包裹内容?
标签: android android-recyclerview