具有不同布局的 RecyclerView.ViewHolders 中的 Android DataBinding [英] Android DataBinding in RecyclerView.ViewHolders with different layouts

查看:53
本文介绍了具有不同布局的 RecyclerView.ViewHolders 中的 Android DataBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新项目中使用 androids 数据绑定功能,到目前为止对它非常满意.

I'm trying to use androids databinding feature in new project and so far very happy with it.

但是现在我在我的 recyclerviews 视图中遇到了一个问题.

But now i came across a problem in my recyclerviews viewholder.

我的 viewholder 使用不同的布局(基于创建时的 viewtype)

My viewholder uses different layouts (based on the viewtype when it gets created)

public MediaViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
     switch(viewType){
          case HEADER: 
             int layout = R.layout.item_media_header;
             break;
          case DEFAULT: 
             int layout = R.layout.item_media_default;
             break;
          case SMALL: 
             int layout = R.layout.item_media_small;
             break;
     }
     View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
     return new MediaViewHolder(v);
}

因此所有这 3 个布局都包含相同的视图,只是排列方式不同.所以模型与视图的绑定是相同的.

So all of those 3 layouts have the same views in it, only arranged differently. So the binding of the model to the views is the same.

无论如何,基于android创建的那些布局

Anyways, based on those layouts android creates

  • ItemMediaHeaderBinding
  • ItemMediaDefaultBinding
  • ItemMediaSmallBinding

这很糟糕,因为它会迫使我创建 3 个不同的 ViewHolder 类或通过检查使用的布局来实例化正确的绑定类.

Which sucks since it would force me to create 3 different ViewHolder classes or instantiate the right binding Class by checking which layout is used.

这种情况有最佳实践吗?是否有可能简单地为这三个绑定类创建一个超类,比如ItemMediaBinding".

Is there a best practice for this case? Is there a possibility to simply create a superclass for those three binding classes like "ItemMediaBinding".

提前致谢.

推荐答案

所以,我最终要做的是创建一个 BaseViewHolder.class,创建另一个 ViewHolder扩展这个 BaseViewHolder 的类,所以我的 RecyclerView.Adapter 看起来仍然很干净,因为我最终得到了这样一个 ViewHolder:

So, what I ended up doing, was to create a BaseViewHolder.class, create other ViewHolder classes that extend this BaseViewHolder, so that my RecyclerView.Adapter still looks clean, because I ended up with something like this as a ViewHolder:

public class BaseVH extends RecyclerView.ViewHolder {
    private ViewDataBinding mBinding;

    public BaseVH(ViewDataBinding mBinding) {
        super(mBinding.getRoot());
        this.mBinding = mBinding;
    }

    public void displayPost(final NewsItem post) {
        PostItemViewModel vm = new PostItemViewModel();
        vm.getPost().set(post);
        mBinding.setVariable(BR.viewModel, vm);
    }
}

public class TextPostVH extends BaseVH {
    private final PostTextBinding mBinding;

    public TextPostVH(final PostTextBinding binding) {
        super(binding);
        mBinding = binding;
    }

    //Have a method like this in the BaseVH.class
    @Override
    public void displayPost(final NewsItem post) {
        if (mBinding.getViewModel() == null) {
            mBinding.setViewModel(new PostItemViewModel());
        }
        mBinding.getViewModel().getPost().set(post);
    }
}

所以你可以简单地调用onBindViewHolder:

So you can simply call in the onBindViewHolder:

@Override
public void onBindViewHolder(final BaseVH holder, final int position) {
    //only a ViewHolder containing ads has to be filtered, the others work with displayPost()
    if (holder instanceof AdVH){
        holder.displayPost(mPosts.get(position));
        ((AdVH) holder).displayAd();
    } else {
        holder.displayPost(mPosts.get(position));
    }
}

您可以在 data-tag 的 layouts 中指定 Binding class names:

You can specify the Binding class names in your layouts in the data-tag:

 <data class="PostTextBinding" />

但我认为不允许在不同的布局中使用相同的类名.您可以在 GitHub 上找到我的(示例)项目.

But I don't think that you are allowed to use the same class names in different layouts. You can find my (sample) project at GitHub.

解释了另一种解决方案在 Github 上.他们已经创建了一个(真正)通用的 RecyclerView.Adapter,但我发现这个......此刻对我来说有点太多了.

Another solution is explained here on Github. They have created a (really) universal RecyclerView.Adapter, but I found this... a little too much for me at this moment.

这篇关于具有不同布局的 RecyclerView.ViewHolders 中的 Android DataBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆