ViewHolder
在动画过程中重叠?我有一个LinearLayout
视图组,其中TextView
有2个ViewHolder
。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/topText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="18sp" />
<TextView
android:id="@+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/world"
android:textSize="20sp" />
</LinearLayout>
topText
始终可见,而bottomText
可以基于布尔标志在VISIBLE
和GONE
之间切换。
class FooVh(itemView: View): RecyclerView.ViewHolder(itemView) {
private val bottomText = itemView.bottomText
fun bind(expanded:Boolean){
bottomText.visibility = if(expanded) View.VISIBLE else View.GONE
}
}
这是我的适配器类
class MyAdapter : RecyclerView.Adapter<FooVh>() {
private var items: List<Boolean> = listOf()
fun update(newItems: List<Boolean>) {
items = newItems.toList()
notifyItemChanged(0, items[0])
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FooVh {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_foo, parent, false)
return FooVh(v)
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(vh: FooVh, position: Int) {
vh.bind(items[position])
}
override fun onBindViewHolder(vh: FooVh, position: Int, payloads: MutableList<Any>) {
if (payloads.isEmpty()) {
super.onBindViewHolder(vh, position, payloads)
} else {
vh.bind(items[position])
}
}
}