Android recyclerView数据绑定适配器数据

时间:2016-11-11 08:21:38

标签: android data-binding android-recyclerview

我正在使用Recycler View实施adapter及其各自的Android Data Binding。问题是,在我看到的所有教程中,他们使用Collection View model类来初始化适配器的数据,如下所示:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

private ArrayList<ViewModel> items;

public RecyclerViewAdapter(ArrayList<ViewModel> items) {
    this.items = items;
}

.....
}

但我要传递给Recycler View的数据是我Collection的{​​{1}} records

database

我该怎么做?

提前致谢!

修改

li_item.xml

private ArrayList<Record> items;

ADAPTER

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="report"
        type="viewmodel.ReportVM"/>
</data>

....

<TextView
        android:id="@+id/tv_report_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="18sp"
        android:paddingBottom="6dp"
        android:text="@{report.name}"
        tools:text="Report 1"
        />

....

模型视图

    public class ReportRVAdapter extends RecyclerView.Adapter<ReportRVAdapter.ReportViewHolder> {

private List<ReportDb> data;


public ReportRVAdapter(final List<ReportDb> reportData) {
    this.data = reportData;
}

@Override
public ReportRVAdapter.ReportViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.li_item, parent, false);
    return new ReportViewHolder(v);
}

@Override
public void onBindViewHolder(final ReportViewHolder holder, final int position) {
    ReportDb item = data.get(position);

    holder.getBinding().setVariable(BR.name, item.getName());
    holder.getBinding().setVariable(BR.contractor, item.getContractor());

    //make binding happen immediately
    holder.getBinding().executePendingBindings();
}

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


public class ReportViewHolder extends RecyclerView.ViewHolder {
    private ViewDataBinding binding;

    public ReportViewHolder(final View rowView) {
        super(rowView);

        binding = DataBindingUtil.bind(rowView);
    }

    public ViewDataBinding getBinding() {
        return binding;
    }
}
}

2 个答案:

答案 0 :(得分:4)

您可以在RecyclerView中使用任何集合,这取决于您如何将值与View(TextView,ImageView)绑定。 例如: -

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            final String name = mDataset.get(position);
            holder.txtHeader.setText(mDataset.get(position));
}

在上面的示例中,它取决于您,如果您使用Map,则需要使用key获取值。对于set,可以使用相同的get方法。

答案 1 :(得分:0)

最后我意识到这不是:

self.vertece_set = set() # Init somewhere else ;)

if key not in self.vertece_set:
    # add a new key to the array
    self.vertece_set.add(key)
    # create a vertex object to store dependency information
    self.verteces.append(Vertex(key, children))

我应该写这个:

holder.getBinding().setVariable(BR.name, item.getName());
holder.getBinding().setVariable(BR.contractor, item.getContractor());

//make binding happen immediately
holder.getBinding().executePendingBindings();

和我的POJO:

holder.getBinding().setReport(new ReportVM(item));