ViewHolder高度变化会导致动画期间的视图重叠

时间:2018-11-24 03:58:36

标签: android android-recyclerview

如何防止ViewHolder在动画过程中重叠?

RecyclerView animation

我有一个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可以基于布尔标志在VISIBLEGONE之间切换。

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])
        }
    }
}

0 个答案:

没有答案