Is there an addHeaderView equivalent for RecyclerView?

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

    First - extends RecyclerView.Adapter
    
    public class MenuAdapter extends RecyclerView.Adapter {
    

    After - Override the method getItemViewTpe ***More Important

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

    method onCreateViewHolder

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);
        View header = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_header_item, parent, false);
        Log.d("onCreateViewHolder", String.valueOf(viewType));
    
        if (viewType == 0) {
            return new MenuLeftHeaderViewHolder(header, onClickListener);
        } else {
            return new MenuLeftViewHolder(view, onClickListener);
        }
    }
    

    method onBindViewHolder

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            MenuHeaderViewHolder menuHeaderViewHolder = (MenuHeaderViewHolder) holder;
            menuHeaderViewHolder.mTitle.setText(sMenuTitles[position]);
            menuHeaderViewHolder.mImage.setImageResource(sMenuImages[position]);
        } else {
            MenuViewHolder menuLeftViewHolder = (MenuLeftViewHolder) holder;
            menuViewHolder.mTitle.setText(sMenuTitles[position]);
            menuViewHolder.mImage.setImageResource(sMenuImages[position]);
        }
    }
    

    in finish implements the ViewHolders class static

    public static class MenuViewHolder extends RecyclerView.ViewHolder 
    
    public static class MenuLeftHeaderViewHolder extends RecyclerView.ViewHolder 
    

提交回复
热议问题