Android 具有多个项类型的DataBoundListAdapter
我正在使用来自android架构组件的DataBoundListAdapter 我需要增强它以支持多种项目类型。有人已经做了吗? 我的问题是如何在createBinding过程中找到项目类型,因为我在那里没有可用的项目位置,但我需要它来获取项目类型,以便能够基于它膨胀正确的布局Android 具有多个项类型的DataBoundListAdapter,android,android-recyclerview,android-architecture-components,Android,Android Recyclerview,Android Architecture Components,我正在使用来自android架构组件的DataBoundListAdapter 我需要增强它以支持多种项目类型。有人已经做了吗? 我的问题是如何在createBinding过程中找到项目类型,因为我在那里没有可用的项目位置,但我需要它来获取项目类型,以便能够基于它膨胀正确的布局 @Override protected ChatMessageItemBinding createBinding(ViewGroup parent) { MyItemBinding binding
@Override
protected ChatMessageItemBinding createBinding(ViewGroup parent) {
MyItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.my_item, parent,
false, dataBindingComponent);
return binding;
}
查看创建绑定的源代码,我可以看到创建绑定只是来自
onCreateViewHolder
的一个调用,其中包含您需要的信息-viewType:Int
对您来说,最简单的选项是重写该方法在您自己的适配器中所做的操作,以传递所需的类型信息
@Override
DataBoundViewHolder<V> onCreateViewHolder(ViewGroup parent, int viewType) {
//Note: no call to super
V binding = createBindingByType(parent, viewType) //this is a new method
return DataBoundViewHolder(binding)
}
private ChatMessageItemBinding createBindingByType(ViewGroup parent, int viewType) {
@LayoutRes int layout;
switch(viewType) {
case ...:
layout = R.layout.my_item;
break;
...
}
return DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), layout, parent, false, dataBindingComponent);
}
@Override
protected ChatMessageItemBinding createBinding(ViewGroup parent) {
throw new RuntimeException("This method should not be called with MyAdapter");
}
@覆盖
DataBoundViewHolder onCreateViewHolder(视图组父级,int-viewType){
//注意:没有呼叫超级用户
V binding=createBindingByType(父,viewType)//这是一个新方法
返回DataBoundViewHolder(绑定)
}
私有ChatMessageItemBinding createBindingByType(视图组父级,int-viewType){
@版式布局;
开关(视图类型){
案例…:
布局=R.layout.my_项目;
打破
...
}
返回DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())、布局、父级、false、dataBindingComponent);
}
@凌驾
受保护的ChatMessageItemBinding createBinding(视图组父级){
抛出新的RuntimeException(“不应使用MyAdapter调用此方法”);
}
重用相同的类,我认为其中一种方法可以是忽略DataBoundListAdapter
,直接实现ListAdapter
,如下所示:
/**
* A RecyclerView adapter for [Repo] class.
*/
class RepoListAdapterFor2Types(
private val dataBindingComponent: DataBindingComponent,
appExecutors: AppExecutors,
private val showFullName: Boolean,
private val repoClickCallback: ((Repo, viewType: Int) -> Unit)?
) : ListAdapter<Repo, DataBoundViewHolder<RepoItemBinding>>(
AsyncDifferConfig.Builder<Repo>(
object : DiffUtil.ItemCallback<Repo>() {
override fun areItemsTheSame(oldItem: Repo, newItem: Repo): Boolean {
return oldItem.owner == newItem.owner
&& oldItem.name == newItem.name
}
override fun areContentsTheSame(oldItem: Repo, newItem: Repo): Boolean {
return oldItem.description == newItem.description
&& oldItem.stars == newItem.stars
}
})
.setBackgroundThreadExecutor(appExecutors.diskIO())
.build()
) {
override fun getItemViewType(position: Int): Int {
val repo = getItem(position)
//this is just for the example
return when (repo.id) {
1 -> 1
else -> 2
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<RepoItemBinding> {
val binding = createBinding(parent, viewType)
return DataBoundViewHolder(binding)
}
override fun onBindViewHolder(holder: DataBoundViewHolder<RepoItemBinding>, position: Int) {
holder.binding.repo = getItem(position)
holder.binding.executePendingBindings()
}
private fun createBinding(parent: ViewGroup, viewType: Int): RepoItemBinding {
@LayoutRes val layout = when (viewType) {
1 -> R.layout.repo_item_type_1
else -> R.layout.repo_item_type_2
}
val binding = DataBindingUtil.inflate<RepoItemBinding>(
LayoutInflater.from(parent.context),
layout,
parent,
false,
dataBindingComponent
)
binding.showFullName = showFullName
binding.root.setOnClickListener {
binding.repo?.let {
repoClickCallback?.invoke(it, viewType)
}
}
return binding
}
}
/**
*[Repo]类的RecyclerView适配器。
*/
2类型的类repolistAdapter(
专用val数据绑定组件:数据绑定组件,
上诉人:上诉人,
private val showFullName:Boolean,
private val repoClickCallback:((回购,视图类型:Int)->单位)?
):ListAdapter(
AsyncDifferConfig.Builder(
对象:DiffUtil.ItemCallback(){
覆盖项相同(旧项:回购,新项:回购):布尔值{
返回oldItem.owner==newItem.owner
&&oldItem.name==newItem.name
}
覆盖内容相同(旧项:回购,新项:回购):布尔值{
返回oldItem.description==newItem.description
&&oldItem.stars==newItem.stars
}
})
.setBackgroundThreadExecutor(appExecutors.diskIO())
.build()
) {
覆盖getItemViewType(位置:Int):Int{
val repo=getItem(位置)
//这只是一个例子
返回时间(回购id){
1 -> 1
其他->2
}
}
覆盖onCreateViewHolder(父级:ViewGroup,viewType:Int):DataBoundViewHolder{
val binding=createBinding(父级,视图类型)
返回DataBoundViewHolder(绑定)
}
覆盖onBindViewHolder(holder:DataBoundViewHolder,位置:Int){
holder.binding.repo=getItem(位置)
holder.binding.executePendingBindings()文件
}
private-fun-createBinding(父级:ViewGroup,viewType:Int):RepoItemBinding{
@LayoutRes val layout=when(视图类型){
1->R.layout.repo\u项目类型\u 1
else->R.layout.repo\u项目类型\u 2
}
val binding=DataBindingUtil.inflate(
LayoutFlater.from(父上下文),
布局,
父母亲
假,,
数据绑定组件
)
binding.showFullName=showFullName
binding.root.setOnClickListener{
绑定、回购?让我们{
repoClickCallback?.invoke(it,viewType)
}
}
返回绑定
}
}
如果您需要处理完全不同的项目,请使用不同的东西。您将无法拥有2个diff回调,因此您可能需要编写自己的ListAdapter
实现