android:带有数据绑定的generic recyclerview适配器

时间:2017-07-20 12:59:12

标签: android android-recyclerview recycler-adapter

我试图创建一个通用的Recyclerview适配器,如下所示:

BaseRecyclerAdapter:

public class BaseRecyclerAdapter<T, MVH extends BaseViewHolder<T>, EVH extends BaseViewHolder<T>> extends RecyclerView.Adapter<BaseViewHolder<T>> {

    private static final int VIEW_TYPE_EMPTY = 0;
    private static final int VIEW_TYPE_DATA = 1;
    private List<T> list = new ArrayList<>();
    @LayoutRes
    private int emptyViewLayoutResource;
    private Class<EVH> emptyViewHolder;
    @LayoutRes
    private int dataLayoutResource;
    private Class<MVH> dataViewHolder;

    public BaseRecyclerAdapter(int emptyViewLayoutResource, Class<EVH> emptyViewHolder, int dataLayoutResource, Class<MVH> dataViewHolder) {
        this.emptyViewLayoutResource = emptyViewLayoutResource;
        this.emptyViewHolder = emptyViewHolder;
        this.dataLayoutResource = dataLayoutResource;
        this.dataViewHolder = dataViewHolder;
    }

    @Override
    public BaseViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType == VIEW_TYPE_EMPTY ? emptyViewLayoutResource : dataLayoutResource, parent, false);
        try {
            return viewType == VIEW_TYPE_EMPTY ? emptyViewHolder.getConstructor(ViewDataBinding.class).newInstance(binding) : dataViewHolder.getConstructor(View.class).newInstance(binding);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder<T> holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == VIEW_TYPE_EMPTY) {

        } else {
            holder.bind(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 1 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.size() == 0 ? VIEW_TYPE_EMPTY : VIEW_TYPE_DATA;
    }
}

BaseViewHolder:

public class BaseViewHolder<T> extends RecyclerView.ViewHolder {
    private final ViewDataBinding binding;

    public BaseViewHolder(ViewDataBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    public void bind(T t) {
        binding.setVariable(BR.obj, t);
        binding.executePendingBindings();
    }
}

任何人都可以尝试使用此功能并建议我如何初始化此类以将其与RecyclerView一起使用?

1 个答案:

答案 0 :(得分:1)

我想要的是什么:我需要将Model传递给Layout,显然我们需要使用它的属性进行绑定。这就是为什么我想让BaseViewHolder成为通用类。

实际问题:您无法获得通用类的类。意思是,我无法初始化BaseRecyclerAdapter,如下所示:

private BaseRecyclerAdapter1<Model, EmptyViewHolder, ModelViewHolder> baseRecyclerAdapter1 = new BaseRecyclerAdapter1<>(R.layout.layout_empty, EmptyViewHolder<Model>.class, R.layout.list_item, ModelViewHolder<Model>.class);

解决方案:感谢数据绑定。 <variable />标记为您执行类型转换。

public class BaseViewHolder extends RecyclerView.ViewHolder {
    private final ViewDataBinding binding;

    public BaseViewHolder(ViewDataBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    public void bind(Object object) {
        binding.setVariable(BR.obj, object);
        binding.executePendingBindings();
    }
}

所以,现在我可以将BaseRecyclerAdapter初始化为:

private BaseRecyclerAdapter1<Model, EmptyViewHolder, ModelViewHolder> baseRecyclerAdapter1 = new BaseRecyclerAdapter1<>(R.layout.layout_empty, EmptyViewHolder.class, R.layout.list_item, ModelViewHolder.class);

布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="android.view.View" />

        <variable
            name="obj"
            type="com.xxx.Model" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>
</layout>

快乐编码.. !!