Implement multiple ViewHolder types in RecycleView adapter

后端 未结 9 1702
迷失自我
迷失自我 2020-12-13 16:34

It\'s maybe a discussion not a question.

Normal way to implement multiple types

As you know, if we want to implement multiple types in RecyclerView

9条回答
  •  无人及你
    2020-12-13 17:03

    It might not be the answer you're expecting but here is an example using Epoxy, which really makes your life easier:

    First you define your models:

    @EpoxyModelClass(layout = R.layout.header_view_model)
    public abstract class HeaderViewModel extends EpoxyModel {
    
        @EpoxyAttribute
        String title;
    
        @Override
        public void bind(TextView view) {
            super.bind(view);
            view.setText(title);
        }
    
    }
    
    @EpoxyModelClass(layout = R.layout.drink_view_model)
    public abstract class DrinkViewModel extends EpoxyModel {
    
        @EpoxyAttribute
        Drink drink;
    
        @EpoxyAttribute
        Presenter presenter;
    
        @Override
        public void bind(View view) {
            super.bind(view);
    
            final TextView title = view.findViewById(R.id.title);
            final TextView description = view.findViewById(R.id.description);
    
            title.setText(drink.getTitle());
            description.setText(drink.getDescription());
            view.setOnClickListener(v -> presenter.drinkClicked(drink));
        }
    
        @Override
        public void unbind(View view) {
            view.setOnClickListener(null);
            super.unbind(view);
        }
    
    }
    
    @EpoxyModelClass(layout = R.layout.food_view_model)
    public abstract class FoodViewModel extends EpoxyModel {
    
        @EpoxyAttribute
        Food food;
    
        @EpoxyAttribute
        Presenter presenter;
    
        @Override
        public void bind(View view) {
            super.bind(view);
    
            final TextView title = view.findViewById(R.id.title);
            final TextView description = view.findViewById(R.id.description);
            final TextView calories = view.findViewById(R.id.calories);
    
            title.setText(food.getTitle());
            description.setText(food.getDescription());
            calories.setText(food.getCalories());
            view.setOnClickListener(v -> presenter.foodClicked(food));
        }
    
        @Override
        public void unbind(View view) {
            view.setOnClickListener(null);
            super.unbind(view);
        }
    
    }
    

    Then you define your Controller:

    public class DrinkAndFoodController extends Typed2EpoxyController, List> {
    
        @AutoModel
        HeaderViewModel_ drinkTitle;
    
        @AutoModel
        HeaderViewModel_ foodTitle;
    
        private final Presenter mPresenter;
    
        public DrinkAndFoodController(Presenter presenter) {
            mPresenter = presenter;
        }
    
        @Override
        protected void buildModels(List drinks, List foods) {
            if (!drinks.isEmpty()) {
                drinkTitle
                        .title("Drinks")
                        .addTo(this);
                for (Drink drink : drinks) {
                    new DrinkViewModel_()
                            .id(drink.getId())
                            .drink(drink)
                            .presenter(mPresenter)
                            .addTo(this);
                }
            }
    
            if (!foods.isEmpty()) {
                foodTitle
                        .title("Foods")
                        .addTo(this);
                for (Food food : foods) {
                    new FoodViewModel_()
                            .id(food.getId())
                            .food(food)
                            .presenter(mPresenter)
                            .addTo(this);
                }
            }
        }
    }
    

    Initialize your Controller:

    DrinkAndFodController mController = new DrinkAndFoodController(mPresenter);
    mController.setSpanCount(1);
    
    final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
    layoutManager.setSpanSizeLookup(mController.getSpanSizeLookup());
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(mController.getAdapter());
    

    And finally you can add your data as easily as this:

    final List drinks = mManager.getDrinks();
    final List foods = mManager.getFoods();
    mController.setData(drinks, foods);
    

    You'll have a list thats looks like:

    Drinks
    Drink 1
    Drink 2
    Drink 3
    ...
    Foods
    Food1
    Food2
    Food3
    Food4
    ...
    

    For more informations you can check the wiki.

提交回复
热议问题