Is there an addHeaderView equivalent for RecyclerView?

后端 未结 19 1651
独厮守ぢ
独厮守ぢ 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:38

    Probably http://alexzh.com/tutorials/multiple-row-layouts-using-recyclerview/ will help. It uses only RecyclerView and CardView. Here is an adapter:

    public class DifferentRowAdapter extends RecyclerView.Adapter {
        private List mList;
        public DifferentRowAdapter(List list) {
            this.mList = list;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view;
            switch (viewType) {
                case CITY_TYPE:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false);
                    return new CityViewHolder(view);
                case EVENT_TYPE:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);
                    return new EventViewHolder(view);
            }
            return null;
        }
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            CityEvent object = mList.get(position);
            if (object != null) {
                switch (object.getType()) {
                    case CITY_TYPE:
                        ((CityViewHolder) holder).mTitle.setText(object.getName());
                        break;
                    case EVENT_TYPE:
                        ((EventViewHolder) holder).mTitle.setText(object.getName());
                        ((EventViewHolder) holder).mDescription.setText(object.getDescription());
                        break;
                }
            }
        }
        @Override
        public int getItemCount() {
            if (mList == null)
                return 0;
            return mList.size();
        }
        @Override
        public int getItemViewType(int position) {
            if (mList != null) {
                CityEvent object = mList.get(position);
                if (object != null) {
                    return object.getType();
                }
            }
            return 0;
        }
        public static class CityViewHolder extends RecyclerView.ViewHolder {
            private TextView mTitle;
            public CityViewHolder(View itemView) {
                super(itemView);
                mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
            }
        }
        public static class EventViewHolder extends RecyclerView.ViewHolder {
            private TextView mTitle;
            private TextView mDescription;
            public EventViewHolder(View itemView) {
                super(itemView);
                mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
                mDescription = (TextView) itemView.findViewById(R.id.descriptionTextView);
            }
        }
    }
    

    And here's an entity:

    public class CityEvent {
        public static final int CITY_TYPE = 0;
        public static final int EVENT_TYPE = 1;
        private String mName;
        private String mDescription;
        private int mType;
        public CityEvent(String name, String description, int type) {
            this.mName = name;
            this.mDescription = description;
            this.mType = type;
        }
        public String getName() {
            return mName;
        }
        public void setName(String name) {
            this.mName = name;
        }
        public String getDescription() {
            return mDescription;
        }
        public void setDescription(String description) {
            this.mDescription = description;
        }
        public int getType() {
            return mType;
        }
        public void setType(int type) {
            this.mType = type;
        }
    }
    

提交回复
热议问题