RecyclerView组件自定义分组

Android 从零开始实现RecyclerView分组及粘性头部效果,《Android面试题及解析》分享给大家 groupList.add(groupItem); } @Override public void buildGroupView(View groupView, GroupItem groupItem) { //构建groupView,通过groupView.findViewById找到内部控件(暂不支持点击事件等),例如 TextView textName = (TextView) groupView.findViewById(R.id.text_name); textName.setText(group 阅读详情

轻松实现类试与 header footer item等分组的Adapter组件。好用方便。

原理: 将每一个Item 从position 转化为section(段) row(列)数据格式,方便定位具体属于那一组的哪一个Item的数据。数据格式如下:

 public static class IndexPath{
        public int row;
        public int section;

        @Override
        public String toString() {
            return "IndexPath [row=" + row + ", section=" + section + "]";
        }
    }

关键点一:
count的计算法,count 从以前的position现在复杂化了,应该包含每一段的header、item、 footer的总和,算法较复杂,这里讲定义为final不让重复写。

  @Override
    final public int getItemCount() {
        int section = getSection();
        int count = 0;
        for (int i = 0; i < section; i++) {
            int header = isGroupHeaderForSection(i)?1:0;
            int footer = isGroupFooterForSection(i)?1:0;
            count = count + getRow(i) + header + footer;
        }
        return count;
    }

关键2:
header 和 footer的动态处理,动态的删减


  /**
     * 该组是否包含HeadView
     * @param section
     * @return
     */
    public boolean isGroupHeaderForSection(int section){
        return false;
    }
    /**
     * 该组是否包含FooterVew
     * @param section
     * @return
     */
    public boolean isGroupFooterForSection(int section){
        return false;
    }

关键3:
将position转化为indexpath的算法

    /**
     * 根据 postion 计算 indexPath
     * @param position
     * @return
     */
    final public IndexPath findIndexPathForPosition(int position){
        position -= getListHeadCount();
        int section = getSection();
        int count = 0;
        int sec = 0;
        for (int i = 0; i < section; i++) {
            int header = isGroupHeaderForSection(i)?1:0;
            int footer = isGroupFooterForSection(i)?1:0;

            count = count + getRow(i) + header + footer;
            if (count > position) {
                sec = i;
                count = count - getRow(i) - header - footer;
                break;
            }
        }

        IndexPath indexPath = new IndexPath();
        indexPath.section = sec;
        indexPath.row = position-count;

        Log.d("demo", "" +indexPath +" po: "+ position +" getRow:"+getRow(indexPath.section));

        return indexPath;
    }

完成上面的情况,就可以进行封装了:
全码:

package com.hujiang.designsupportlibrarydemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by tp on 2016/11/16.
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    public Context mContext;



    public int getSection(){//获取切片
        return 1;
    }

    /**
     * 根据切片section 获取对应的 row item
     * @param section
     * @return
     */
    public abstract int getRow(int section);

    @Override
    final public int getItemCount() {
        int section = getSection();
        int count = 0;
        for (int i = 0; i < section; i++) {
            int header = isGroupHeaderForSection(i)?1:0;
            int footer = isGroupFooterForSection(i)?1:0;
            count = count + getRow(i) + header + footer;
        }
        return count;
    }


    @Override
    final public int getItemViewType(int position) {
        IndexPath indexPath = findIndexPathForPosition(position);
        if (isGroupHeaderForSection(indexPath.section) && indexPath.row == 0 ) {//有头
            return getItemHeaderViewType(indexPath);
        }

        if (isGroupFooterForSection(indexPath.section)) {//有尾
            int row = getRow(indexPath.section);
            int header = isGroupHeaderForSection(indexPath.section)?1:0;
            if (row == indexPath.row - header) {//到尾部了
                return getItemFooterViewType(indexPath);
            }
        }
        return getItemViewType(indexPath);
    }

    /**
     *
     * @param indexPath 改位置的 view type
     * @return
     */
    public int getItemViewType(IndexPath indexPath){
        return 0;
    }

    /**
     *
     * @param indexPath section Footer 位置的 view type
     * @return
     */
    public int getItemFooterViewType(IndexPath indexPath){
        return 1;
    }
    /**
     *
     * @param indexPath  section Header position view type
     * @return
     */
    public int getItemHeaderViewType(IndexPath indexPath){
        return 2;
    }


    /**
     * 根据 postion 计算 indexPath
     * @param position
     * @return
     */
    final public IndexPath findIndexPathForPosition(int position){
        position -= getListHeadCount();
        int section = getSection();
        int count = 0;
        int sec = 0;
        for (int i = 0; i < section; i++) {
            int header = isGroupHeaderForSection(i)?1:0;
            int footer = isGroupFooterForSection(i)?1:0;

            count = count + getRow(i) + header + footer;
            if (count > position) {
                sec = i;
                count = count - getRow(i) - header - footer;
                break;
            }
        }

        IndexPath indexPath = new IndexPath();
        indexPath.section = sec;
        indexPath.row = position-count;

        Log.d("demo", "" +indexPath +" po: "+ position +" getRow:"+getRow(indexPath.section));

        return indexPath;
    }

    /**
     * listview  header 的数量决定 position 的位置
     * @return
     */
    public int getListHeadCount(){
        return 0;
    }


    @Override
    final public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        IndexPath indexPath = findIndexPathForPosition(position);

        int header = isGroupHeaderForSection(indexPath.section)?1:0;

        if (isGroupHeaderForSection(indexPath.section) && indexPath.row == 0) {//有头
            onBindHeaderViewHolder(holder,position,indexPath);
            return;
        }

        if (isGroupFooterForSection(indexPath.section)) {//有尾
            int row = getRow(indexPath.section);
            if (row == indexPath.row - header) {//到尾部了
                onBindFooterViewHolder(holder,position,indexPath);
                return;
            }
        }

        if (isGroupHeaderForSection(indexPath.section)) {//包含header row位置 -1 和 list index 保持一致
            indexPath.row -= 1;
        }

        onBindItemViewHolder(holder,position,indexPath);
    }


    public  void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position,IndexPath indexPath){}
    public  void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position,IndexPath indexPath){}
    public abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position,IndexPath indexPath);

    /**
     * 该组是否包含HeadView
     * @param section
     * @return
     */
    public boolean isGroupHeaderForSection(int section){
        return false;
    }
    /**
     * 该组是否包含FooterVew
     * @param section
     * @return
     */
    public boolean isGroupFooterForSection(int section){
        return false;
    }




    public View getHeaderView(int position,int section,View convertView,ViewGroup parent){
        if (isGroupHeaderForSection(section)) {
            TextView header = null;
            if (convertView!=null) {
                header =(TextView) convertView;
            }else {
                header = new TextView(mContext);
            }
            header.setText("第:"+section+" header");
            return header;
        }
        return null;
    }
    public View getFooterView(int position,int section,View convertView,ViewGroup parent){
        if (isGroupFooterForSection(section)) {
            TextView footer =null;
            if (convertView != null) {
                footer = (TextView) convertView;
            }else {
                footer = new TextView(mContext);
            }
            footer.setText("第:"+section +" footer");
            return footer;
        }
        return null;
    }


    public static class IndexPath{
        public int row;
        public int section;

        @Override
        public String toString() {
            return "IndexPath [row=" + row + ", section=" + section + "]";
        }
    }


}

使用方法:
继承基类adapter 例如:

package com.hujiang.designsupportlibrarydemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hujiang.designsupportlibrarydemo.R;

import java.util.List;

/**
 * Created by tp on 2016/11/16.
 */

public class RecylerGroupAdapter extends BaseRecyclerAdapter {



    private List<String> list;

    public RecylerGroupAdapter(List<String> list) {
        this.list = list;
    }

    @Override
    public int getRow(int section) {
        return  section;
        //return 1;
    }

    @Override
    public int getSection() {
        return  list.size();
    }

    @Override
    public boolean isGroupFooterForSection(int section) {
        return true;
    }

    @Override
    public boolean isGroupHeaderForSection(int section) {
        return  true;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item, parent, false);
        return new DefineViewHolder(view);
    }


    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position, IndexPath indexPath) {
        IndexPath path =  findIndexPathForPosition(position);
        if (holder instanceof DefineViewHolder) {
            ((DefineViewHolder)holder).setData(list.get(indexPath.row));
        }
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position, IndexPath indexPath) {
        if (holder instanceof DefineViewHolder) {
            ((DefineViewHolder)holder).setData("第:"+indexPath+" 段 - header");
        }
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position, IndexPath indexPath) {
        if (holder instanceof DefineViewHolder) {
            ((DefineViewHolder)holder).setData("第:"+indexPath+" 段 - Footer");
        }
    }

    static class DefineViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;

        public DefineViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }

        public void setData(String data) {
            tvTitle.setText(data);
        }

    }
}

关键方法:

 @Override
    public int getRow(int section) {
        return  section;
        //return 1;
    }

    @Override
    public int getSection() {
        return  list.size();
    }
RecyclerView 实现分组列表 RecyclerView 实现分组列表 ,通过RecycleView ,实现listview 嵌套gridview效果,listview中嵌套listview效果等。 立即下载

相关推荐

从寄存器配置到G值:一份给STM32开发者的SC7A20加速度数据换算保姆级指南

本文为STM32开发者提供SC7A20三轴加速度传感器的数据换算保姆级指南,详细解析从寄存器配置到G值转换的全过程。涵盖量程选择、输出值换算原理及优化代码实现,帮助开发者快速掌握加速度数据的物理意义转换技巧,提升运动感知系统开发效率。

weixin_29172963的博客 179

RecyclerView实现分组效果,多种实现方式

RecyclerView实现分组效果

唐诺 3781

Go语言集成Ollama本地大模型:gollama库实战指南

在当今的AI应用开发中,大型语言模型(LLM)的集成已成为关键技术。其核心原理是通过API调用,将自然语言处理能力嵌入到各类应用中。从技术价值看,本地化部署的LLM解决了数据隐私、网络延迟和持续成本三大痛点,尤其适合处理敏感数据或对实时性要求高的场景。在工程实践中,开发者常使用专门的客户端库来简化与模型服务的交互,例如通过封装REST API、提供类型安全接口和流式处理支持,从而提升开发效率和代码健壮性。具体到Go语言生态,结合Ollama这一流行的本地大模型运行工具,可以实现完全离线、低延迟的AI功能集成

weixin_28728443的博客 623

Android实现RecyclerView分组显示功能完整示例(自定义组头和Item布局)

这样,我们就实现了RecyclerView的分组功能。在GroupAdapter中,我们重写了getItemCount()、getItemViewType()、onCreateViewHolder()和onBindViewHolder()方法,使其能够支持分组。在Activity或Fragment中,我们创建了RecyclerView和GroupAdapter,并将其设置为RecyclerView的Adapter,从而实现了分组功能。 在这个示例中,我们使用了RelativeLayout布局来包含Recy

wh445306 4076

Python验证码识别神器ddddocr:5分钟搞定网站登录自动化(附完整代码)

本文详细介绍了如何使用Python开源库ddddocr快速实现验证码识别,结合Selenium完成网站登录自动化。ddddocr作为轻量级OCR工具,支持多种验证码类型,识别速度快且准确率高,特别适合自动化测试和爬虫开发。文章包含完整代码示例,从环境配置到实战应用,帮助开发者5分钟内实现验证码破解。

sprite的博客 807

从CentOS迁移到openEuler 24.03 LTS:手把手教你部署Java+MySQL+Redis+Nginx全栈环境

本文详细指导如何从CentOS迁移到openEuler 24.03 LTS,并部署Java+MySQL+Redis+Nginx全栈环境。涵盖系统配置、软件安装、性能优化及环境验证,助力企业级用户高效完成服务器迁移与部署。

weixin_26938645的博客 400

Android开发技巧——使用RecyclerView实现分组列表

有一个多月没写原创博客了,今天来介绍一下使用RecyclerView来实现分组列表。之所以使用RecyclerView,主要原因还是因为项目开发中使用ExpandableListView无法实现设计师所需要的分割线。 本篇内容偏简单,主要是讲如何集成使用。这一使用场景还是相对常见而实用的,较适合初学者进阶。 实现原因及效果 ...

weixin_34018169的博客 3788

从零开始:用7353张VOC/YOLO格式混凝土缺陷数据集训练你的第一个检测模型

本文详细介绍了如何使用7353张VOC/YOLO格式的混凝土缺陷数据集训练首个目标检测模型。从环境配置、数据解析到YOLOv5模型训练与优化,提供了完整的实战指南,特别针对数据不平衡问题给出了解决方案,助力开发者快速掌握混凝土缺陷识别技术。

weixin_42651281的博客 267

Android使用RecyclerView实现日期分组以及时间轴显示

【代码】Android使用RecyclerView实现日期分组以及时间轴显示。

qq_31481093的博客 1639

别再让OCV拖慢你的芯片!手把手教你用set_timing_derate优化STA时序(附CPPR实战)

本文深入探讨了芯片设计中OCV(On-Chip Variation)对时序分析的影响,并提供了使用`set_timing_derate`优化STA时序的实战指南。通过分阶段derate设置和CPPR技术,有效减少时序违例,提升芯片性能。文章结合TSMC 7nm工艺案例,详细解析了OCV建模与优化策略,助力工程师实现时序收敛。

weixin_42525005的博客 309

基于毫米波生物感知雷达+STM32设计的独居老人居家监护系统(微信小程序)(192)

当前基于STM32设计的独居老人居家监护系统为独居老人提供一种简便、实时的健康监护服务。该系统通过集成多种传感器和功能模块,能够实时监测老人的生理指标、姿态以及室内环境状况,并通过微信小程序远程显示数据,当出现异常情况时及时发出报警提醒。

1万+

Android 从零开始实现RecyclerView分组及粘性头部效果

基于RecyclerView实现列表分组和粘性头部的效果

AnliaLee的博客 6643

【Go语言实战】(2) Gin+Vue 电子商城

618来!!不用纠结去哪里剁手了!直接自己安排一个商场不香吗??

面向生活编程 4837

Android日期分组,android日历选择按月分组并用recyclerview展示

今天分享一个日历选择控件,可以定义日期可选、选择范围、按月分组展示。这个日历无非就是把每个日期的数据通过系统的日历查询出来,然后用recyclerview展示即可,数据模型里可以定义哪些可选以及选定状态等等。思路就是这样了,先看看效果:日历效果首先定义好数据来源,即从系统的calendar获取日期列表,这里因为是要按月分组,所以我选择用一个key为月份的时间戳value为对应月份的所有日期list...

weixin_29083475的博客 911

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Android日历选择按月分组并用recyclerview展示

今天分享一个日历选择控件,可以定义日期可选、选择范围、按月分组展示。这个日历无非就是把每个日期的数据通过系统的日历查询出来,然后用recyclerview展示即可,数据模型里可以定义哪些可选以及选定状态等等。思路就是这样了,先看看效果: 首先定义好数据来源,即从系统的calendar获取日期列表,这里因为是要按月分组,所以我选择用一个key为月份的时间戳value为对应月份的所有日期list的map来接收查询出来的日历,在展示的时候把map转为分组的list即可: import android.annot

xiaoerye的专栏 1507

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Android可收缩伸展的Expandable分组RecyclerView

Android可收缩伸展的Expandable分组RecyclerViewAndroid上的可收缩伸展的Expandable RecyclerView第三方开源项目很多,但是在自己的项目引用了这些github上的类似项目后,发现问题和Bug很多,不稳定,无奈之下,自己在RecyclerView基础上写了一个相对稳定、可以收缩伸展Expandable分组RecyclerView。代码:import

Zhang Phil 5657

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

RecyclerView实现多布局

实现一个类似于拨号页面的效果 public class PhoneActivity extends AppCompatActivity { String[] arr = {"android.permission.READ_CONTACTS"}; private RecyclerView recyclerView; private String label = ""; ...

alaleidd的博客 241

写给初学者的 HarmonyOS 教程 -- ArkTS 容器组件(List)

使用列表可以轻松高效地显示结构化、可滚动的信息。通过在 List 组件中按垂直或者水平方向线性排列子组件 ListItemGroup 或 ListItem,为列表中的行或列提供单个视图,或使用ForEach 迭代一组行或列,或混合任意数量的单个视图和 ForEach 结构,构建一个列表。

Fucking Android & Compose & Harmony & Spring Boot ... 7622

RecyclerView 展示同日期下的数据信息

RecyclerView 展示同日期下的数据信息概述:最近有一个业务需求,ListView根据日期进行展示,但是ListView还是没有RecyclerView好用,好处我就不再详述了,没用过的可以百度一下,很简单,很方便!效果:主要代码:GroupItemHolder.javapublic class GroupItemHolder extends NormalItemHolder { pu

吴唐人的博客 5774

Android-Timeline-View

Android-Timeline-View 项目地址:akshaykale/Android-Timeline-View  简介:Android timeline to display swiping cards in recyclerview, grouped by date 更多:作者   提 Bug    标签:   Android timeline to display hori...

未来很长,但我会努力的走下去。 1471

【亲测免费】 Stickiness Unleashed: Android RecyclerView 的精英插件 —— StickyHeaders

在Android开发领域,追求高效与用户体验至上的今天,ShamylZakariya的StickyHeaders项目无疑是一颗璀璨之星。基于Java编程语言,此项目巧妙地为RecyclerView添加了粘性头部功能,让滚动列表中的分类标签固定显示,从而极大地提升了应用的导航体验。 ## 核心功能揭秘 StickyHeaders的核心魅力在于其直观且实用的设计——实现RecylerView中段落式...

gitblog_01296的博客 540

RecycleView得使用之----强大的 BaseRecyclerViewAdapterHelper BaseQuickAdapter 使用以及注意事项!

参考:HTTPS://blog.csdn.net/qq_37293612/article/details/54943910 参考:https://blog.csdn.net/u011324501/article/details/53899542 Demo 参考: 1. 带Header的Item https://github.com/CymChad/BaseRecyclerViewAdapter...

追梦天涯33的博客 1865

收集的一些备用资料(1)

#收集的一些备用资料 - **分享一个recycleview辅助类,以后不用为上下拉刷新,加载不同条目,条目分组而烦恼了** [链接](http://droidyue.com/blog/2016/05/09/baserecyclerviewadapterhelper/) - **一个自定义RecyclerView,支持添加header、上拉刷新、下拉加载** 这里的

csdnzzj255的博客 372

开源项目 `expandable-recycler-view` 常见问题解决方案

`expandable-recycler-view` 是一个用于 Android 平台的开源项目,旨在提供可扩展和可折叠的 RecyclerView 适配器。该项目的主要编程语言是 Java,适用于需要展示分组数据的 Android 应用开发。通过使用该库,开发者可以轻松实现列表项的展开和折叠功能,支持多种视图类型,从而提升用户体验。 ## 新手使用注意事项及解决方案 ### 1. 依赖库的正...

gitblog_00397的博客 863

RecyclerView的使用(3)之添加Header和Footer

转自:http://blog.csdn.net/leejizhou/article/details/50742544 RecyclerView虽然作为ListView的替代者有着较好的性能提升,但是ListView的一些常用功能却没有提供,比如我们平时会经常用到的addHeaderView,addFooterView,既然RecyclerView没有提供这个方法,我们应该如何为列表添加头

2293
上一篇: Ijkplayer编译 过程
下一篇: Android Service用法总结和生命周期详解
doubleping
博客等级 码龄18年 23粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址