android表格点击,Android smartTable的简单使用

本文介绍了Android库SmartTable的使用,包括其主要功能,如自动计算表格宽高、支持固定序列和统计行,以及丰富的格式化和事件监听。通过注解或手动配置方式创建表格,展示了两种方式的步骤和示例代码。此外,讨论了两种方式的优缺点,注解方式简便但不支持列动态创建和排序,而基本方式则能实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.smartTable简介

1.简单介绍它的功能:

快速配置自动生成表格;

自动计算表格宽高;

表格列标题组合;

表格固定左序列、顶部序列、第一行、列标题、统计行;

自动统计,排序(自定义统计规则);

表格图文、序列号、列标题格式化;

表格各组成背景、文字、网格、padding等配置;

表格批注;

表格内容、列标题点击事件;

缩放模式和滚动模式;

注解模式;

内容多行显示;

分页模式;

首尾动态添加数据;

丰富的格式化;

支持二维数组展示(用于类似日程表,电影选票等);

导入excel(支持颜色,字体,背景,批注,对齐,图片等基本Excel属性);

表格合并单元(支持注解合并,支持自动合并);

支持其他刷新框架SmartRefreshLayout;

可配置表格最小宽度(小于该宽度自动适配);

支持直接List或数组字段转列;

支持Json数据直接转换成表格;

支持表格网格指定行列显示;

支持自动生成表单。

2.如何使用

引用:添加 JitPack repository 到你的 build 文件

allprojects {

repositories {

...

maven { url 'https://www.jitpack.io' }

}

}

增加依赖

dependencies {

compile 'com.github.huangyanbin:SmartTable:2.0'

}

3.使用方式(两种)

采用注解的形式

基本模式,手动配置行与列

二.两种方式的使用

1.注解方式

步骤一:在布局文件中使用 SmartTable

android:id="@+id/table"

android:layout_width="match_parent"

android:layout_height="match_parent" />

步骤二:定义表格(自定义bean对象)

@SmartTable(name = "销售计划表")

public class UserInfo {

public UserInfo(String city, int name, int count, int restaurant, int ka, int wholesale, int industry, int other) {

this.city = city;

this.name = name;

this.count = count;

this.restaurant = restaurant;

this.ka = ka;

this.wholesale = wholesale;

this.industry = industry;

this.other = other;

}

// name:版块名称,count:目标值,restaurant:餐饮数量,ka:KA数量,wholesale:流通批发数量,industry:工业加工数量,other:其它数量

@SmartColumn(id = 0, name = "部门/渠道", autoMerge = true)

private String city;

@SmartColumn(id = 1, name = "板块")

private int name;

@SmartColumn(id = 2, name = "目标值")

private int count;

@SmartColumn(id = 3, name = "餐饮")

private int restaurant;

@SmartColumn(id = 4, name = "KA")

private int ka;

@SmartColumn(id = 5, name = "流通批发")

private int wholesale;

@SmartColumn(id = 6, name = "工业加工")

private int industry;

@SmartColumn(id = 7, name = "其他")

private int other;

}

步骤三:绑定数据

public class MainActivity extends AppCompatActivity {

private SmartTable table;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

List list = new ArrayList<>();

table = findViewById(R.id.table);

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("乌鲁木齐",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("乌鲁木齐",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("乌鲁木齐",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("乌鲁木齐",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

list.add(new UserInfo("沈阳",100, 150, 50, 240, 1100, 450, 23458));

table.setData(list);

table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));

}

}

实现效果

bcfe030b77db

注解方式效果图.png

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

步骤一:在布局文件中使用 SmartTable

android:id="@+id/table"

android:layout_width="match_parent"

android:layout_height="match_parent" />

步骤二:定义表格(自定义bean对象),与采用注解方式唯一的不同就是不在使用 @SmartTable与 @SmartColumn 进行标注

public class User {

public User(String city, int name, int count, int restaurant, int ka, int wholesale, int industry, int other) {

this.city = city;

this.name = name;

this.count = count;

this.restaurant = restaurant;

this.ka = ka;

this.wholesale = wholesale;

this.industry = industry;

this.other = other;

}

// name:版块名称,count:目标值,restaurant:餐饮数量,ka:KA数量,wholesale:流通批发数量,industry:工业加工数量,other:其它数量

private String city;

private int name;

private int count;

private int restaurant;

private int ka;

private int wholesale;

private int industry;

private int other;

}

步骤三:手动创建列字段

//普通列

Column city = new Column<>("部门/渠道", "city");

Column name = new Column<>("板块", "name");

Column count = new Column<>("目标值", "count");

Column restaurant = new Column<>("餐饮", "restaurant");

Column ka = new Column<>("KA", "ka");

Column wholesale = new Column<>("流通批发", "wholesale");

Column industry = new Column<>("工业加工", "industry");

Column other = new Column<>("其他", "other");

//设置该列当字段相同时自动合并

city.setAutoMerge(true);

步骤四:设置单元格内容

//设置单元格内容

List list = new ArrayList<>();

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("乌鲁木齐", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

list.add(new User("沈阳", 100, 150, 50, 240, 1100, 450, 23458));

步骤五:把数据绑定到 SmartTable 上

//表格数据 datas 是需要填充的数据

TableData tableData = new TableData<>("表格名", list, city, name, count, restaurant, ka, wholesale, industry, other);

//设置数据

table = findViewById(R.id.table);

table.setTableData(tableData);

table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));

3.两种方式的优势和不足

1.注解方式

使用上简单,几行代码就可以创建一个表格

不能实现列的动态创建

不能实现列的排序

2.基本方式

使用上稍稍比注解方式麻烦一点

可以实现列的动态创建(根据服务器返回的列的数量动态创建表格)

可以实现点击列,对列进行升序以及倒序排列

三.smartTable的特性,效果(延用作者本人的文章作参考)

谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值