Is there an addHeaderView equivalent for RecyclerView?

后端 未结 19 1630
独厮守ぢ
独厮守ぢ 2020-11-21 23:35

I\'m looking for an equivalent to addHeaderView for a recycler view. Basically I want to have an image with 2 buttons be added as a header to the listview. Is there a differ

19条回答
  •  爱一瞬间的悲伤
    2020-11-22 00:26

    I made an implementation based on @hister's one for my personal purposes, but using inheritance.

    I hide the implementation details mechanisms (like add 1 to itemCount, subtract 1 from position) in an abstract super class HeadingableRecycleAdapter, by implementing required methods from Adapter like onBindViewHolder, getItemViewType and getItemCount, making that methods final, and providing new methods with hidden logic to client:

    • onAddViewHolder(RecyclerView.ViewHolder holder, int position),
    • onCreateViewHolder(ViewGroup parent),
    • itemCount()

    Here are the HeadingableRecycleAdapter class and a client. I left the header layout a bit hard-coded because it fits my needs.

    public abstract class HeadingableRecycleAdapter extends RecyclerView.Adapter {
    
        private static final int HEADER_VIEW_TYPE = 0;
    
        @LayoutRes
        private int headerLayoutResource;
        private String headerTitle;
        private Context context;
    
        public HeadingableRecycleAdapter(@LayoutRes int headerLayoutResourceId, String headerTitle, Context context) {
            this.headerLayoutResource = headerLayoutResourceId;
            this.headerTitle = headerTitle;
            this.context = context;
        }
    
        public Context context() {
            return context;
        }
    
        @Override
        public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == HEADER_VIEW_TYPE) {
                return new HeaderViewHolder(LayoutInflater.from(context).inflate(headerLayoutResource, parent, false));
            }
            return onCreateViewHolder(parent);
        }
    
        @Override
        public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            int viewType = getItemViewType(position);
            if (viewType == HEADER_VIEW_TYPE) {
                HeaderViewHolder vh = (HeaderViewHolder) holder;
                vh.bind(headerTitle);
            } else {
                onAddViewHolder(holder, position - 1);
            }
        }
    
        @Override
        public final int getItemViewType(int position) {
            return position == 0 ? 0 : 1;
        }
    
        @Override
        public final int getItemCount() {
            return itemCount() + 1;
        }
    
        public abstract int itemCount();
    
        public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent);
    
        public abstract void onAddViewHolder(RecyclerView.ViewHolder holder, int position);
    
    }
    
    
    
    @PerActivity
    public class IngredientsAdapter extends HeadingableRecycleAdapter {
        public static final String TITLE = "Ingredients";
        private List itemList;
    
    
        @Inject
        public IngredientsAdapter(Context context) {
            super(R.layout.layout_generic_recyclerview_cardified_header, TITLE, context);
        }
    
        public void setItemList(List itemList) {
            this.itemList = itemList;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
            return new ViewHolder(LayoutInflater.from(context()).inflate(R.layout.item_ingredient, parent, false));
        }
    
        @Override
        public void onAddViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            vh.bind(itemList.get(position));
        }
    
        @Override
        public int itemCount() {
            return itemList == null ? 0 : itemList.size();
        }
    
        private String getQuantityFormated(double quantity, String measure) {
            if (quantity == (long) quantity) {
                return String.format(Locale.US, "%s %s", String.valueOf(quantity), measure);
            } else {
                return String.format(Locale.US, "%.1f %s", quantity, measure);
            }
        }
    
    
        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_ingredient)
            TextView txtIngredient;
    
            ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
    
            void bind(Ingredient ingredient) {
                String ingredientText = ingredient.getIngredient();
                txtIngredient.setText(String.format(Locale.US, "%s %s ", getQuantityFormated(ingredient.getQuantity(),
                        ingredient.getMeasure()), Character.toUpperCase(ingredientText.charAt(0)) +
                        ingredientText
                                .substring(1)));
            }
        }
    }
    

提交回复
热议问题