如何使用数据绑定在Recyclerview中创建2列

时间:2019-04-02 12:59:59

标签: android android-recyclerview android-databinding

我正在使用具有干净架构的MVVM中的数据绑定应用Recyclerview。如何应用layoutManager制作两列网格?

我已经使用layoutManager和spancount在xml中创建列,但是这种方式不起作用。我看过很多教程,其中人们没有完全使用recyclerview数据绑定。

public class StoreHomeAdapter extends RecyclerView.Adapter<StoreHomeAdapter.GenericViewHolder> {

private int layoutId;
private List<StoreHomeFields> storeNames;
private StoreHomeViewModel viewModel;

public StoreHomeAdapter(@LayoutRes int layoutId, StoreHomeViewModel viewModel) {
    this.layoutId = layoutId;
    this.viewModel = viewModel;
}

private int getLayoutIdForPosition(int position) {
    return layoutId;
}

@Override
public int getItemCount() {
    return storeNames == null ? 0 : storeNames.get(0).getResult().length;
}

public GenericViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType, parent, false);

    return new GenericViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull GenericViewHolder holder, int position) {
    holder.bind(viewModel, position);
}

@Override
public int getItemViewType(int position) {
    return getLayoutIdForPosition(position);
}

public void setStoreHomes(List<StoreHomeFields> mStoreNames) {
    this.storeNames = mStoreNames;
}

class GenericViewHolder extends RecyclerView.ViewHolder {
    final ViewDataBinding binding;

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

    void bind(StoreHomeViewModel viewModel, Integer position) {
      //  viewModel.fetchStoreHomeImagesAt(position);
        binding.setVariable(BR.viewModel, viewModel);
        binding.setVariable(BR.position, position);
        binding.executePendingBindings();
    }

}

这是ViewModel类

public class StoreHomeViewModel extends  ViewModel {

   private StoreHomeAdapter adapter;

   public void init() {
        storeHomeFields = new StoreHomeModel();
        adapter = new StoreHomeAdapter(R.layout.store_home_item, this);
    }

    public StoreHomeAdapter getAdapter() {
        return adapter;
    }
    public ArrayAdapter getCityAdapter() {
        return cityAdapter;
    }

    public void setStoreNamesInAdapterObj(List<StoreHomeFields> mStores) {
        this.adapter.setStoreHomes(mStores);
        this.adapter.notifyDataSetChanged();
    }

 }

这是xml

<android.recyclerview.widget.RecyclerView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:setAdapter="@{model.getAdapter()}"
        app:spanCount="2"
        apps:orientation="vertical" />

2 个答案:

答案 0 :(得分:1)

我发现要做的唯一方法是使用绑定变量在oncreateview中实例化布局,如下所示:

class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if(logs.get("acc") >= 0.99):
            print("Reached 99% accuracy so cancelling training!")
            self.model.stop_training = True

布局

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    binding =  DataBindingUtil.inflate(inflater, R.layout.yout_fragment,container,false)
    binding.gridView.layoutManager = GridLayoutManager(context,2)

    return binding.root
}

答案 1 :(得分:0)

该解决方案非常简单,请在您的xml中设置 <com.google.android.material.chip.Chip android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" tools:visibility="visible" tools:text="Leichter Genuss" />

app:spanCount="2"