• Android smartTable的简单使用


    一.smartTable简介

    1.简单介绍它的功能:

    具体使用方法不再赘述,可以去 GitHub 去查看:https://github.com/huangyanbin/smartTable

    2.添加依赖: 

    build.gradle (Project:***)

    1. allprojects {
    2. repositories {
    3. google()
    4. jcenter()
    5. *
    6. *
    7. *
    8. maven { url 'https://www.jitpack.io' }
    9. }
    10. }

     build.gradle (Module:app)

    1. dependencies {
    2. *
    3. *
    4. *
    5. // SmartTable-依赖
    6. implementation 'com.github.huangyanbin:SmartTable:2.0'
    7. }

    3.使用方式(两种)

    • 采用注解的形式
    • 基本模式,手动配置行与列

    二.两种方式的使用

    1.注解方式

    • 步骤一:在布局文件中使用 SmartTable
    1. bin.david.form.core.SmartTable
    2. android:id="@+id/table"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"/>
    • 步骤二:定义表格(自定义bean对象)
    1. **
    2. * 注解方法
    3. * */
    4. @SmartTable(name = "销售计划表")
    5. public class UserInfo {
    6. public UserInfo(String city, int name, int count, int restaurant, int ka, int wholesale, int industry, int other) {
    7. this.city = city;
    8. this.name = name;
    9. this.count = count;
    10. this.restaurant = restaurant;
    11. this.ka = ka;
    12. this.wholesale = wholesale;
    13. this.industry = industry;
    14. this.other = other;
    15. }
    16. /**
    17. * name:版块名称,count:目标值,restaurant:餐饮数量,
    18. * ka:KA数量,wholesale:流通批发数量,industry:工业加工数量,
    19. * other:其它数量
    20. * */
    21. @SmartColumn(id = 0, name = "部门/渠道", autoMerge = true)
    22. private String city;
    23. @SmartColumn(id = 1, name = "板块")
    24. private int name;
    25. @SmartColumn(id = 2, name = "目标值")
    26. private int count;
    27. @SmartColumn(id = 3, name = "餐饮")
    28. private int restaurant;
    29. @SmartColumn(id = 4, name = "KA")
    30. private int ka;
    31. @SmartColumn(id = 5, name = "流通批发")
    32. private int wholesale;
    33. @SmartColumn(id = 6, name = "工业加工")
    34. private int industry;
    35. @SmartColumn(id = 7, name = "其他")
    36. private int other;
    37. }
    • 步骤三:绑定数据 

     

    1. /**
    2. * 初始化-注解方法
    3. * */
    4. private void initAnnotation(){
    5. smartTable = findViewById(R.id.table);
    6. List list = new ArrayList<>();
    7. list.add(new UserInfo("朝阳区",100, 150, 50, 240, 1100, 450, 23458));
    8. list.add(new UserInfo("朝阳区",100, 150, 50, 240, 1100, 450, 23458));
    9. list.add(new UserInfo("朝阳区",100, 150, 50, 240, 1100, 450, 23458));
    10. list.add(new UserInfo("朝阳区",100, 150, 50, 240, 1100, 450, 23458));
    11. list.add(new UserInfo("绿园区",100, 150, 50, 240, 1100, 450, 23458));
    12. list.add(new UserInfo("绿园区",100, 150, 50, 240, 1100, 450, 23458));
    13. list.add(new UserInfo("绿园区",100, 150, 50, 240, 1100, 450, 23458));
    14. list.add(new UserInfo("绿园区",100, 150, 50, 240, 1100, 450, 23458));
    15. list.add(new UserInfo("宽城区",100, 150, 50, 240, 1100, 450, 23458));
    16. list.add(new UserInfo("宽城区",100, 150, 50, 240, 1100, 450, 23458));
    17. list.add(new UserInfo("宽城区",100, 150, 50, 240, 1100, 450, 23458));
    18. list.add(new UserInfo("宽城区",100, 150, 50, 240, 1100, 450, 23458));
    19. smartTable.setData(list);
    20. // 设置表格标题名称文字样式
    21. smartTable.getConfig().setTableTitleStyle(new FontStyle(80,Color.CYAN));
    22. // 设置表格标题文字样式
    23. smartTable.getConfig().setColumnTitleStyle(new FontStyle(70,Color.GREEN));
    24. // 设置表格主体内容文字样式
    25. smartTable.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
    26. }

     实现效果:

     

    2.基本方式,手动创建行与列

    • 步骤一:在布局文件中使用 SmartTable
    1. bin.david.form.core.SmartTable
    2. android:id="@+id/table"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"/>
    • 步骤二:定义表格(自定义bean对象),与采用注解方式唯一的不同就是不在使用 @SmartTable与 @SmartColumn 进行标注
    1. /**
    2. * 基本方式
    3. * */
    4. public class User {
    5. public User(String city, int name, int count, int restaurant, int ka, int wholesale, int industry, int other) {
    6. this.city = city;
    7. this.name = name;
    8. this.count = count;
    9. this.restaurant = restaurant;
    10. this.ka = ka;
    11. this.wholesale = wholesale;
    12. this.industry = industry;
    13. this.other = other;
    14. }
    15. /**
    16. * name:版块名称,count:目标值,restaurant:餐饮数量,
    17. * ka:KA数量,wholesale:流通批发数量,industry:工业加工数量,
    18. * other:其它数量
    19. * */
    20. private String city;
    21. private int name;
    22. private int count;
    23. private int restaurant;
    24. private int ka;
    25. private int wholesale;
    26. private int industry;
    27. private int other;
    28. }
    • 步骤三:手动创建列字段
    1. //普通列
    2. Column<String> city = new Column<>("部门/渠道", "city");
    3. Column<Integer> name = new Column<>("板块", "name");
    4. Column<Integer> count = new Column<>("目标值", "count");
    5. Column<Integer> restaurant = new Column<>("餐饮", "restaurant");
    6. Column<Integer> ka = new Column<>("KA", "ka");
    7. Column<Integer> wholesale = new Column<>("流通批发", "wholesale");
    8. Column<Integer> industry = new Column<>("工业加工", "industry");
    9. Column<Integer> other = new Column<>("其他", "other");
    10. //设置该列当字段相同时自动合并
    11. city.setAutoMerge(true);
    • 步骤四:设置单元格内容
    1. //设置单元格内容
    2. List list = new ArrayList<>();
    3. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    4. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    5. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    6. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    7. list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));
    8. list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));
    9. list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));
    10. list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));
    11. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    12. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    13. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    14. list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));
    • 步骤五:把数据绑定到 SmartTable 上

     

    1. //表格数据 datas 是需要填充的数据
    2. TableData<User> tableData = new TableData<>("表格名", list, city, name, count, restaurant, ka, wholesale, industry, other);
    3. //设置数据
    4. smartTable = findViewById(R.id.table);
    5. // 设置表格主标题
    6. smartTable.setTableData(tableData);
    7. // 设置表格标题名称文字样式
    8. smartTable.getConfig().setTableTitleStyle(new FontStyle(80,Color.CYAN));
    9. // 设置表格标题文字样式
    10. smartTable.getConfig().setColumnTitleStyle(new FontStyle(70,Color.RED));
    11. // 设置表格主体内容文字样式
    12. smartTable.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));

     实现效果:

     

    注解的其他功能

    • @SmartTable:表格注解,用于生成表格

    可用属性:

    • name:表格名
    • count:是否显示统计行
    • pageSize:页数量
    • currentPage:当前页
      目测没什么用,可能还没找到技巧,先记录下........
    • @SmartColumn列,用于注解列
    • name:列标题
    • id:列排列位置(id越小,位置越靠前)
    • parent:父列名称(不设置则没有父列)
    • align:列对其方式,默认居中
    • type:设置是否查询下一级,有 ColumnType.Own,ColumnType.Child,两个值可以设置,假设 UserInfo 有个属性是 Family family 对象,你想解析 faily 对象的属性 monther,father 两个属性,则需要设置 Child,并在 monther,father 下添加相对应的注解@SmartColumn,否则只解析到 Family,默认是 Own。
    • autoMerge:设置是否自动合并,假设你返回的数据格式化之后 该列附近数据有相同,则会自动合并成一个单元格,默认不开启合并。
    • maxMergeCount:合并最大数量
    • autoCount:是否开启统计,table 开启显示统计行,设置 autoCount 为 true,则该列可以自动统计,默认为 false。
    • fixed:是否固定该列, fixed 设置为 true,该列滚动到最左边时,可以自动固定住。

    基本方法介绍

    • Column 类的常用方法
    • setAutoCount(boolean isAutoCount):设置自动排序(默认升序)
    • isReverseSort:是否是反序排列
    • setComparator:设置排序比较
    • setCountFormat:统计格式化
    • OnColumnItemClickListener:列内容点击事件
    • setFixed:滑动到表格左边时固定列
    • setTextAlign:设置开启自动合并
    • setMaxMergeCount:设置开启最大数量
    • setDrawFormat:设置绘制样式格式化
    • setFormat:设置文字格式化
    • TableData 类常用方法
    • setSortColumn:设置排序列
    • settitleDrawFormat:设置列标题格式化
    • setXSequenceFormat:设置顶部序列号格式化
    • setYSequenceFormat:设置左边序列号格式化
    • setShowCount:设置是否显示统计
    • setTitleDrawFormat:设置列标题绘制格式化
    • setXSequenceFormat :设置 X 序号行文字格式化
    • setYSequenceFormat :设置 Y 序号行文字格式化
    • setUserCellRange(List userCellRange) :设置添加自定义合并规则

    TableConfig 类常用方法

    • setContentStyle :设置内容文字样式
    • setYSequenceStyle :设置左边序列文字样式
    • setXSequenceStyle :设置顶部序列文字样式
    • setColumnTitleStyle :设置列标题文字样式
    • setTableTitleStyle :设置表格标题文字样式
    • setCountStyle :设置统计行样式
    • setColumnTitleGridStyle :设置列标题网格样式
    • setGridStyle :设置内容网格样式
    • setVerticalPadding :设置网格列 padding
    • setHorizontalPadding :设置网格行 padding
    • setYSequenceBackgroundColor :设置左序列背景
    • setXSequenceBackgroundColor :设置横序行背景
    • setColumnTitleBackgroundColor :设置列标题背景
    • setContentBackgroundColor :设置内容背景
    • setCountBackgroundColor :设置统计行背景
    • setFixedYSequence :固定左侧
    • setFixedXSequence :固定顶部
    • setFixedTitle :固定列标题
    • setFixedCountRow :固定统计行
    • setColumnTitleVerticalPadding :列标题上下 padding
    • setColumnTitleHorizontalPadding :增加列标题左右 padding
    • setSequenceGridStyle :序列网格样式
    • columnTitleGridStyle :列标题网格样式
    • setShowXSequence :设置是否显示顶部序号列
    • setShowYSequence :设置是否显示左侧序号列
    • setShowTableTitle :设置是否显示表格标题
    • isShowColumnTitle :设置是否显示列标题
    • setMinTableWidth :设置表格最小宽度

    SmartTable 类的常用方法

    • setOnColumnClickListener :设置列标题点击事件
    • setSortColumn :设置排序列
    • setZoom(boolean zoom,float maxZoom,float minZoom) :设置是否开启缩放
    • addData(List t, boolean isFoot) :添加新数据
    • setSelectFormat :设置选中 Cell 样式
    • notifyDataChanged :重新计算布局


     

     

     

     

     

     

  • 相关阅读:
    Web3.0热浪之下 门萨Mensa生态震撼来袭
    如何在Python中实现安全的密码存储与验证
    分布式组件 nacos使用
    零成本体验美国云服务器,更方便的体验和选择
    使用apose.pdf批量导出图片
    奇舞周刊第 506 期: Nest 实现扫码登录
    如何训练聊天机器人面对复杂的语言环境和需求?
    方向介绍:基于深度学习的轨迹预测
    进制转换算法(通用,极简)
    苍穹外卖-01
  • 原文地址:https://blog.csdn.net/qq_19688207/article/details/126097046