Search code examples
androidandroid-recyclerviewandroid-databindingandroid-architecture-componentsandroid-livedata

How to create 2 columns in a databinded recyclerview adapter in android java


I am aware of what to do normally by means of a GridLayoutManager but as I've been using Google's GithubBrowserSample (Java) but I am unable to replicate the process using this method.

Tried changing the xml's span count but i am using the androidx version and seemingly doesn't play ball

My Main Fragment:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    categoryViewModel = ViewModelProviders.of(this, viewModelFactory).get(CategoryViewModel.class);
    refreshAndObserve();
    CategoryListAdapter cvAdapter = new CategoryListAdapter(dataBindingComponent);
    binding.get().categoryList.setAdapter(cvAdapter);
    adapter = new AutoClearedValue<>(this, cvAdapter);
}

My Adapter:

public class CategoryListAdapter extends DataBoundListAdapter<Category, ListItemCategoryBinding> {
private final androidx.databinding.DataBindingComponent dataBindingComponent;

public CategoryListAdapter(DataBindingComponent dataBindingComponent) {
    this.dataBindingComponent = dataBindingComponent;
}

@Override
protected ListItemCategoryBinding createBinding(ViewGroup parent) {
    ListItemCategoryBinding binding = DataBindingUtil
            .inflate(LayoutInflater.from(parent.getContext()), R.layout.list_item_category,
                    parent, false, dataBindingComponent);
    return binding;
}
  }

Thank you.


Solution

  • This can be done via the XML it seems:

    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2"