Android 使用数据绑定回收PagerAdapter中的视图
我一直在尝试在我的PagerAdapter中实现回收,我可以成功地做到这一点,但我遇到了一个问题,即使用数据绑定缓存视图 我试过这样做,保持一个Android 使用数据绑定回收PagerAdapter中的视图,android,data-binding,android-databinding,Android,Data Binding,Android Databinding,我一直在尝试在我的PagerAdapter中实现回收,我可以成功地做到这一点,但我遇到了一个问题,即使用数据绑定缓存视图 我试过这样做,保持一个堆栈: 我仍然会得到同样的错误。如何“回收”这样的数据绑定对象?或者,如果我回收视图本身,如何将它们转换回绑定对象?我猜您犯了一个简单的错误。 你可以纠正我,如果我错了,我尝试了这个适配器,它的工作 val demoAdapter = object : PagerAdapter() { private var recycledViews:
堆栈
:
我仍然会得到同样的错误。如何“回收”这样的数据绑定对象?或者,如果我回收视图本身,如何将它们转换回绑定对象?我猜您犯了一个简单的错误。 你可以纠正我,如果我错了,我尝试了这个适配器,它的工作
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
}
}
到
返回if(recycledViews.isEmpty()){
拆卸绑定。充气(充气机、容器、假)
}否则{
val view=recycledViews.pop()
val custBinding=DataBindingUtil.getBinding(视图)
if(custBinding==null)
DemoItemBinding.bind(视图)
其他的
保管
}
我想,您试图绑定到一个已经附加了绑定的视图。因此,它给了你一个错误。我所做的是检查以前的任何绑定,如果有,请返回关联的绑定。很抱歉测试此绑定的延迟。它工作得很好!
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
val view = `object` as View
container.removeView(view)
recycledViews.add(CustomBinding.bind(view))
}
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
}