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

查看:315
本文介绍了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.

我的视图持有者使用不同的布局(基于创建时的视图类型)

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,创建其他扩展该BaseViewHolderViewHolder类,这样我的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:

@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标签中的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.

解释了另一种解决方案

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天全站免登陆