SmartTable
github地址:https://github.com/huangyanbin/smartTable
參考:https://juejin.cn/post/6844903549109813255
https://www.jianshu.com/p/bcfe030b77db
最近接到一個app專案的開發,我是.Net開發,剛開始學習Android+java,如在文中有錯誤、不對的地方還請大家指正,
在需求中要用到表格呈現的形式,百度了半天最終決定使用SmartTabel來進行開發,
故記錄下如何使用SmartTable
功能介紹
-
快速配置自動生成表格;
-
自動計算表格寬高;
-
表格列標題組合;
-
表格固定左序列、頂部序列、第一行、列標題、統計行;
-
自動統計,排序(自定義統計規則);
-
表格圖文、序列號、列標題格式化;
-
表格各組成背景、文字、網格、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_
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 = https://www.cnblogs.com/lizongyan/p/new TableData<>("用戶資訊", userList, coId,coName,coAge,coPhone);
//注意:系結資料的方法setData換成了setTableData,不再是List物件而是TableData物件
table.setTableData(tableData);
}
效果圖:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421375.html
標籤:Android
上一篇:對Div中的數字進行數字排序
