RecyclerView分组之跨列分组

需求

需求分析:

如上图所示,这是一个展示年级及学期的界面。年级分为三组高中、初中和小学。因学期太多若按一个一个控件的写既繁琐又不够优雅,所以我采用 RecyclerView配合分组来实现效果。
注意:我这边是使用一个 RecyclerView和一个 List,然后Adapter使用getItemViewType将填充视图分为标题和内容两个 ItemView,通过Bean中的 type 来决定加载哪个视图。
因为是三列的列表,所以在插入 item 时,标题 item 会和内容 item 在同一行,顾在插入标题 item 的时候让它跨三列即可独占一行,具体实现如下。

实现过程:

xml相关文件


    
        
    
    

    

标题 item.xml


    
        
    
    

        

            

        

    


内容 item.xml



    

        

        

        

    

activity.xml

bean

public class GradeBean {

    private int gradeId;
    private String gradeName;
    private boolean gradeType;

    public GradeBean() {
    }

    public GradeBean(int gradeId, String gradeName, boolean gradeType) {
        this.gradeId = gradeId;
        this.gradeName = gradeName;
        this.gradeType = gradeType;
    }

    public int getGradeId() {
        return gradeId;
    }

    public void setGradeId(int gradeId) {
        this.gradeId = gradeId;
    }

    public String getGradeName() {
        return gradeName;
    }

    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }

    public boolean isGradeType() {
        return gradeType;
    }

    public void setGradeType(boolean gradeType) {
        this.gradeType = gradeType;
    }

    @Override
    public String toString() {
        return "GradeBean{" +
                "gradeId=" + gradeId +
                ", gradeName='" + gradeName + '\'' +
                ", gradeType=" + gradeType +
                '}';
    }
}

adapter

public class GradeAdapter extends RecyclerView.Adapter {

    private LayoutInflater layoutInflater;
    private List beanList;
    private OnItemClickListener listener;


    public GradeAdapter(Context context) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        beanList = new ArrayList<>();
    }

    public interface OnItemClickListener {
        void onItemClick(GradeBean gradeBean);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    /**
     * item 分类
     */
    public enum ItemType {
        LEVEL,  //年级级别
        GRADE   //年级
    }

    /**
     * 设置每个 item 类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        final GradeBean bean = beanList.get(position);
        if (bean.isGradeType()) {
            return ItemType.LEVEL.ordinal();    //级别
        }else {
            return ItemType.GRADE.ordinal();    //年级
        }
    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding;
        if (viewType == ItemType.LEVEL.ordinal()) {
            binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_level, parent, false);
        }else {
            binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_grade_number, parent, false);
        }
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        final GradeBean bean = beanList.get(position);
        holder.getBinding().setVariable(BR.gradeBean, bean);
        holder.getBinding().executePendingBindings();

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(bean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList == null ? 0 : beanList.size();
    }

    public void addAll(List beans) {
        beanList.clear();
        beanList.addAll(beans);
        notifyDataSetChanged();
    }
}

activity

public class GradeActivity extends BaseActivity {

    ActivityGradeBinding binding;
    private List gradeBeanList;
    private GradeAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_grade);

        gradeBeanList = new ArrayList<>();
        gradeBeanList.add(new GradeBean(1, "小学", true));
        gradeBeanList.add(new GradeBean(2, "一年级 上", false));
        gradeBeanList.add(new GradeBean(3, "一年级 下", false));

        gradeBeanList.add(new GradeBean(4, "二年级 上", false));
        gradeBeanList.add(new GradeBean(5, "二年级 下", false));

        gradeBeanList.add(new GradeBean(6, "三年级 上", false));
        gradeBeanList.add(new GradeBean(7, "三年级 下", false));

        gradeBeanList.add(new GradeBean(8, "四年级 上", false));
        gradeBeanList.add(new GradeBean(9, "四年级 下", false));

        gradeBeanList.add(new GradeBean(10, "五年级 上", false));
        gradeBeanList.add(new GradeBean(11, "五年级 下", false));

        gradeBeanList.add(new GradeBean(12, "六年级 上", false));
        gradeBeanList.add(new GradeBean(13, "六年级 下", false));

        gradeBeanList.add(new GradeBean(14, "初中", true));
        gradeBeanList.add(new GradeBean(15, "七年级 上", false));
        gradeBeanList.add(new GradeBean(16, "七年级 下", false));

        gradeBeanList.add(new GradeBean(17, "八年级 上", false));
        gradeBeanList.add(new GradeBean(18, "八年级 下", false));

        gradeBeanList.add(new GradeBean(19, "九年级 上", false));
        gradeBeanList.add(new GradeBean(20, "九年级 下", false));

        gradeBeanList.add(new GradeBean(21, "高中", true));
        gradeBeanList.add(new GradeBean(22, "高一 上", false));
        gradeBeanList.add(new GradeBean(23, "高一 下", false));

        gradeBeanList.add(new GradeBean(24, "高二 上", false));
        gradeBeanList.add(new GradeBean(25, "高二 下", false));

        gradeBeanList.add(new GradeBean(26, "高三 上", false));
        gradeBeanList.add(new GradeBean(27, "高三 下", false));

        GridLayoutManager gridLayoutManager = new GridLayoutManager(GradeActivity.this, 3, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (gradeBeanList.get(position).isGradeType()) {
                    return 3;   //如果是标题 item 则跨三列
                } else {
                    return 1;   //如果是内容 item 则不夸咧
                }

            }
        });
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.gradeRv.setLayoutManager(gridLayoutManager);
        adapter = new GradeAdapter(GradeActivity.this);
        adapter.addAll(gradeBeanList);
        binding.gradeRv.setAdapter(adapter);

        adapter.setOnItemClickListener(new GradeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(GradeBean gradeBean) {
                Log.e("onItemClick", "onItemClick: " + gradeBean.getGradeName());
            }
        });

    }
}

你可能感兴趣的:(RecyclerView分组之跨列分组)