使用数据绑定回收PagerAdapter中的视图

时间:2018-06-19 14:58:39

标签: android data-binding android-databinding

我一直在尝试在PagerAdapter中实现回收,由于this question,我可以成功实现回收,但是我遇到了使用数据绑定缓存视图的问题。

我已经尝试过这样,保持Stack<View>

class CustomAdapter : PagerAdapter() {
    private var recycledViews: Stack<View> = Stack()

    var items: List<Item> = ArrayList()
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val binding = inflateOrRecycle(container)

        binding.item = items[position]
        binding.handler = this

        container.addView(binding.root)
        return binding.root
    }

    private fun inflateOrRecycle(container: ViewGroup): CustomBinding {
        val inflater = LayoutInflater.from(container.context)

        return if (recycledViews.isEmpty()) {
            CustomBinding.inflate(inflater, container, false)
        } else {
            val view = recycledViews.pop()
            CustomBinding.bind(view)
        }
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val view = `object` as View

        container.removeView(view)
        recycledViews.add(view)
    }
}

但是,每当它第一次尝试使用回收的视图并调用CustomBinding.bind(view)时,它都会崩溃,因为该视图必须具有标签。我已经搜索过了,但是我发现的答案都没有完全解决我的问题。

我也尝试过保留Stack<CustomBinding>,但是问题是我不确定如何处理destroyItem方法。因为如果我这样做:

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    val view = `object` as View

    container.removeView(view)
    recycledViews.add(CustomBinding.bind(view))
}

我仍然会遇到相同的错误。我该如何“回收”像这样的数据绑定对象?或者,如果我自己回收视图,如何将它们转换回绑定对象?

1 个答案:

答案 0 :(得分:1)

我猜你犯了一个简单的错误。 如果我错了,您可以纠正我,我尝试使用此适配器并且它起作用了。

val demoAdapter = object : PagerAdapter() {
        private var recycledViews: Stack<View> = Stack()

        var items: List<String> = ArrayList()
            set(value) {
                field = value
                notifyDataSetChanged()
            }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val binding = inflateOrRecycle(container)


            container.addView(binding.root)
            return binding.root
        }

        private fun inflateOrRecycle(container: ViewGroup): DemoItemBinding {
            val inflater = LayoutInflater.from(container.context)

            return if (recycledViews.isEmpty()) {
                DemoItemBinding.inflate(inflater, container, false)
            } else {
                val view = recycledViews.pop()
                val custBinding = DataBindingUtil.getBinding<DemoItemBinding>(view)
                if(custBinding == null)
                    DemoItemBinding.bind(view)
                else
                    custBinding
            }
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            val view = `object` as View

            container.removeView(view)
            recycledViews.add(view)
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return `object` is View && view.equals(`object`)
        }

        override fun getCount(): Int {
            return 4
        }
    }

我从您的代码中更改的部分是这个

return if (recycledViews.isEmpty()) {
            CustomBinding.inflate(inflater, container, false)
        } else {
            val view = recycledViews.pop()
            CustomBinding.bind(view)
        }

return if (recycledViews.isEmpty()) {
                DemoItemBinding.inflate(inflater, container, false)
            } else {
                val view = recycledViews.pop()
                val custBinding = DataBindingUtil.getBinding<DemoItemBinding>(view)
                if(custBinding == null)
                    DemoItemBinding.bind(view)
                else
                    custBinding
            }

我认为,您正在尝试绑定到已经附加了绑定的视图。因此,它给您一个错误。我所做的是检查是否有以前的绑定,如果有则返回关联的绑定。