recyclerView中的所有图像都是方形的。我必须给recyclerView提供拐角半径,但是内部的物品是正方形的。
我试着给回收视图做个造型,但没能实现。
有没有一种方法可以在android中先循环第一项和最后一项?
round_recycler.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<stroke android:width="1dp"
android:color="@android:color/transparent"
/>
<padding android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp"
/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
只是我必须先舍入最后一项或RecylerView,我也尝试clipOutline真符合我的item_row_layout
item_row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="item"
type="planet.beyond.domain.models.RecentBO" />
</data>
<planet.beyond.gallerycleanarchitecture.utis.AspectRatioImageView
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ivPhoto"
imagePath="@{item.imageUrl}"
android:layout_width="90dp"
android:clipToOutline="true"
android:layout_height="90dp"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
tools:src="@tools:sample/avatars" />
</layout>
发布于 2022-01-09 12:01:03
由于您使用的是Kotlin和DataBinding,执行此类操作的一个简单方法是映射项目的集合,以确定哪个是第一项,哪些是最后一项。
执行这个问题的方法有很多种,为了简单起见,我将提供一个包装类来说明我如何处理这个问题:
data class RecentBOItem(
val data: RecentBO,
val isFirst: Boolean,
val isLast: Boolean
)
让视图模型执行如下操作:
suspend fun getSomething(): List<RecentBOItem> {
val recentBos = getRecentBOsFromNetwork()
val lastIndex = recentBos.lastIndex
return recent.withIndex().map { (index, recentBo) ->
RecentBOItem(
data = recentBo,
isFirst = index == 0,
isLast = index == lastIndex
)
}
}
然后为基于@BindingAdapter
和isLast
标志设置圆角形状的RecentBOItem
提供一个isFirst
。(即将合适的R.drawable
设置为背景)
发布于 2022-01-09 12:35:20
我给你两种方法,你可以根据你需要的来选择
val isFirst: Boolean = false,
val isLast: Boolean = false
在您的onBindViewHolder
中,根据您以前为项定义的变量执行操作。
class SampleAdapter: RecyclerView.Adapter<SampleAdapter.ViewHolder>() {
// I consider you are using DiffUtil, change the implementation based on your adapter structure
companion object {
private const val VIEW_TYPE_FIRST = -1
private const val VIEW_TYPE_MIDDLE = 0
private const val VIEW_TYPE_LAST = 1
}
override fun getItemViewType(position: Int): Int {
return when {
position == 0 -> VIEW_TYPE_FIRST
position == differ.currentList.size - 1 -> VIEW_TYPE_LAST
else -> VIEW_TYPE_MIDDLE
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return when {
VIEW_TYPE_FIRST -> ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_first_row_layout, parent, false))
VIEW_TYPE_LAST -> ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_last_row_layout, parent, false))
else -> ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_middle_row_layout, parent, false))
}
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val viewType = getItemViewType(position)
holder.itemView.apply {
when (viewType) {
VIEW_TYPE_FIRST -> loadFirstItem(this)
VIEW_TYPE_MIDDLE -> loadMiddleItem(this, position)
VIEW_TYPE_LAST -> loadLastItem(this)
}
}
}
private fun loadFirstItem(itemView: View) {
val item = differ.currentList[0]
// your code...
}
private fun loadMiddleItem(itemView: View, position: Int) {
val item = differ.currentList[position]
// your code...
}
private fun loadLastItem(itemView: View) {
val item = differ.currentList.last()
// your code...
}
}
发布于 2022-01-09 10:49:06
您可以在RecyclerView
的Adapter
's viewType
中使用onCreateViewHolder
方法。您可以重写getItemViewType(int position)
方法并按位置返回不同的“类型”。在onCreateViewHolder
方法中,您可以膨胀不同的布局或对视图应用不同的设置。
检查此链接:https://blog.mindorks.com/recyclerview-multiple-view-types-in-android
https://stackoverflow.com/questions/70640349
复制相似问题