github地址:https://github.com/huangyanbin/smartTable
參考:https://juejin.cn/post/6844903549109813255
https://www.jianshu.com/p/bcfe030b77db
最近接到一個app項目的開發,我是.Net開發,剛開始學習Android+java,如在文中有錯誤、不對的地方還請大家指正。
在需求中要用到表格呈現的形式,百度了半天最終決定使用SmartTabel來進行開發。
故記錄下如何使用
功能介紹
-
快速配置自動生成表格;
-
-
表格列標題組合;
-
表格固定左序列、頂部序列、第一行、列標題、統計行;
-
自動統計,排序(自定義統計規則);
-
表格圖文、序列號、列標題格式化;
-
表格各組成背景、文字、網格、padding等配置;
-
表格批注;
-
表格內容、列標題點擊事件;
-
縮放模式和滾動模式;
-
注解模式;
-
內容多行顯示;
-
分頁模式;
-
首尾動態添加數據;
-
豐富的格式化;
-
支持二維數組展示(用於類似日程表,電影選票等);
-
導入excel(支持顏色,字體,背景,批注,對齊,圖片等基本Excel屬性);
-
表格合並單元(支持注解合並,支持自動合並);
-
支持其他刷新框架SmartRefreshLayout;
-
可配置表格最小寬度(小於該寬度自動適配);
-
支持直接List或數組字段轉列;
-
支持Json數據直接轉換成表格;
-
支持表格網格指定行列顯示;
-
支持自動生成表單。
如何使用
我使用的Android Studio 版本:Android Studio Arctic Fox | 2020.3.1 Patch 4
-
引用:添加 JitPack 到你的 build.gradle(Project: ***) 文件和settings.gradle文件
repositories {
maven { url 'https://www.jitpack.io' }
}
-
增加依賴 build.gradle(Module: *** .app)
dependencies {
implementation 'com.github.huangyanbin:SmartTable:2.2.0'
}
引用完后,在頂部會有一個提示,如下。點擊“Sync Now”進行同步,或者點擊這個小象的圖標也可以。
使用方式(兩種)
-
采用注解的形式
-
基本模式,手動配置行與列
1.注解方式
-
步驟一:在布局文件(.xml)中使用 SmartTable
<com.bin.david.form.core.SmartTable
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent" />
-
步驟二:定義表格(新建自定義User對象)
import com.bin.david.form.annotation.SmartColumn;
import com.bin.david.form.annotation.SmartTable;
//@SmartTable表格注解 @SmartColumn字段注解
@SmartTable(name="用戶信息") //表格標題
public class User {
private int Id;
@SmartColumn(id =0,name = "姓名") //id排序,值越小越靠前。name列名
private String Name;
@SmartColumn(id =1,name = "年齡")
private int Age;
@SmartColumn(id =2,name = "手機號")
private String Phone;
public User(int id,String name,int age,String phone){
this.Id = id;
this.Name = name;
this.Age = age;
this.Phone = phone;
}
}
-
步驟三:綁定數據
//MainActivity文件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<User> userList = new ArrayList<>();
userList.add(new User(0,"Lisa",26,"260"));
userList.add(new User(1,"Nana",25,"250"));
userList.add(new User(2,"Mia",24,"240"));
com.bin.david.form.core.SmartTable table = findViewById(R.id.table);
table.setData(userList);
}
效果圖:
2.基本方式,手動創建行與列
-
步驟一:在布局文件(.xml)中添加 SmartTable控件。同方式一
-
步驟二:User對象,注意這塊注解已全部去掉。
public class User {
private int Id;
private String Name;
private int Age;
private String Phone;
public User(int id,String name,int age,String phone){
this.Id = id;
this.Name = name;
this.Age = age;
this.Phone = phone;
}
}
-
步驟三:
MainActivity文件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//region 給User對象添加數據
List<User> userList = new ArrayList<>();
userList.add(new User(0,"Lisa",26,"260"));
userList.add(new User(1,"Nana",25,"250"));
userList.add(new User(2,"Mia",24,"240"));
//endregion
//region 聲明表格列
Column<String> coId = new Column<>("編號", "Id"); //注意,這里的“Id”要和User中字段名一致
//一致是因為需要用字段名來解析List對象
Column<String> coName = new Column<>("姓名", "Name");
Column<String> coAge = new Column<>("年齡", "Age");
Column<String> coPhone = new Column<>("手機號", "Phone");
//endregion
com.bin.david.form.core.SmartTable table = findViewById(R.id.table);
table.setZoom(true,1,0.5f); //開啟縮放功能
table.getConfig().setShowXSequence(false); //去掉表格頂部字母
table.getConfig().setShowYSequence(false); //去掉左側數字
//TableData對象,包含了(表格標題,數據源,列1,列2,列3,列4....好多列)
TableData<User> tableData = new TableData<>("用戶信息", userList, coId,coName,coAge,coPhone);
//注意:綁定數據的方法setData換成了setTableData。不再是List對象而是TableData對象
table.setTableData(tableData);
}
效果圖: