Android的recyclerview点击item后背景平移到选中的item
时间: 2023-08-01 15:15:22 浏览: 179
Android RecyclerView item选中放大被遮挡问题详解
要实现RecyclerView点击item后背景平移到选中的item,可以通过自定义RecyclerView的ItemDecoration来实现。以下是实现的步骤:
1. 在res/drawable文件夹下创建一个selector文件,用来设置RecyclerView item的背景色变化。例如,创建一个名为background_item.xml的文件,代码如下:
```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark" />
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</selector>
```
这里定义了三个状态,分别是按下状态、选中状态和普通状态,分别设置了不同的背景颜色。
2. 在RecyclerView的Adapter中,设置ItemView的背景为上述selector文件,例如:
```kotlin
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
view.setBackgroundResource(R.drawable.background_item)
return ViewHolder(view)
}
```
3. 在RecyclerView的ItemDecoration中,定义一个变量mSelectedPosition,用于记录当前选中的item位置。在item点击事件中,更新mSelectedPosition变量的值,并调用RecyclerView的invalidateItemDecorations()方法刷新ItemDecoration,例如:
```kotlin
class CustomItemDecoration : RecyclerView.ItemDecoration() {
private val mPaint = Paint()
private val mOffset = 20 // 偏移量
private val mTopLeftRectF = RectF()
private val mBottomRightRectF = RectF()
private var mSelectedPosition = RecyclerView.NO_POSITION // 当前选中的item位置
init {
mPaint.color = Color.WHITE
mPaint.isAntiAlias = true
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
// 计算矩形区域
val left = child.left - params.leftMargin - mOffset
val top = child.top - params.topMargin - mOffset
val right = child.right + params.rightMargin + mOffset
val bottom = child.bottom + params.bottomMargin + mOffset
mTopLeftRectF.set(left.toFloat(), top.toFloat(), child.left.toFloat(), child.top.toFloat())
mBottomRightRectF.set(child.right.toFloat(), child.bottom.toFloat(), right.toFloat(), bottom.toFloat())
// 绘制背景
if (parent.getChildAdapterPosition(child) == mSelectedPosition) {
c.drawRoundRect(mTopLeftRectF, 10f, 10f, mPaint)
c.drawRoundRect(mBottomRightRectF, 10f, 10f, mPaint)
}
}
}
fun setSelectedPosition(position: Int) {
if (mSelectedPosition != position) {
mSelectedPosition = position
recyclerView?.invalidateItemDecorations()
}
}
}
```
这里的setSelectedPosition方法用于更新mSelectedPosition变量的值,并调用RecyclerView的invalidateItemDecorations()方法刷新ItemDecoration。
4. 在RecyclerView的item点击事件中,调用上述ItemDecoration的setSelectedPosition方法更新选中的item位置,例如:
```kotlin
recyclerView.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
when (e.action) {
MotionEvent.ACTION_UP -> {
val child = rv.findChildViewUnder(e.x, e.y)
if (child != null) {
val position = rv.getChildAdapterPosition(child)
customItemDecoration.setSelectedPosition(position)
// 处理item点击事件
onItemClick(position)
return true
}
}
}
return false
}
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
})
```
这里的onItemClick方法用于处理item点击事件,具体实现可根据实际需求进行编写。
这样,点击RecyclerView的item时就能实现背景平移效果并且选中的item背景颜色不会消失了。
阅读全文