Android smartTable的簡(jiǎn)單使用

一.smartTable簡(jiǎn)介

1.簡(jiǎn)單介紹它的功能:

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

  • 快速配置自動(dòng)生成表格;
  • 自動(dòng)計(jì)算表格寬高;
  • 表格列標(biāo)題組合;
  • 表格固定左序列、頂部序列、第一行、列標(biāo)題、統(tǒng)計(jì)行;
  • 自動(dòng)統(tǒng)計(jì),排序(自定義統(tǒng)計(jì)規(guī)則);
  • 表格圖文、序列號(hào)、列標(biāo)題格式化;
  • 表格各組成背景、文字、網(wǎng)格、padding等配置;
  • 表格批注;
  • 表格內(nèi)容、列標(biāo)題點(diǎn)擊事件;
  • 縮放模式和滾動(dòng)模式;
  • 注解模式;
  • 內(nèi)容多行顯示;
  • 分頁(yè)模式;
  • 首尾動(dòng)態(tài)添加數(shù)據(jù);
  • 豐富的格式化;
  • 支持二維數(shù)組展示(用于類似日程表,電影選票等);
  • 導(dǎo)入excel(支持顏色,字體,背景,批注,對(duì)齊,圖片等基本Excel屬性);
  • 表格合并單元(支持注解合并,支持自動(dòng)合并);
  • 支持其他刷新框架SmartRefreshLayout;
  • 可配置表格最小寬度(小于該寬度自動(dòng)適配);
  • 支持直接List或數(shù)組字段轉(zhuǎn)列;
  • 支持Json數(shù)據(jù)直接轉(zhuǎn)換成表格;
  • 支持表格網(wǎng)格指定行列顯示;
  • 支持自動(dòng)生成表單。

2.如何使用

  • 引用:添加 JitPack repository 到你的 build 文件
allprojects {
        repositories {
            ...
            maven { url 'https://www.jitpack.io' }
        }
    }
  • 增加依賴
dependencies {
            compile 'com.github.huangyanbin:SmartTable:2.0'
    }

3.使用方式(兩種)

  • 采用注解的形式
  • 基本模式,手動(dòng)配置行與列

二.兩種方式的使用

1.注解方式

  • 步驟一:在布局文件中使用 SmartTable
<com.bin.david.form.core.SmartTable
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  • 步驟二:定義表格(自定義bean對(duì)象)
@SmartTable(name = "銷售計(jì)劃表")
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:目標(biāo)值,restaurant:餐飲數(shù)量,ka:KA數(shù)量,wholesale:流通批發(fā)數(shù)量,industry:工業(yè)加工數(shù)量,other:其它數(shù)量
    @SmartColumn(id = 0, name = "部門/渠道", autoMerge = true)
    private String city;
    @SmartColumn(id = 1, name = "板塊")
    private int name;
    @SmartColumn(id = 2, name = "目標(biāo)值")
    private int count;
    @SmartColumn(id = 3, name = "餐飲")
    private int restaurant;
    @SmartColumn(id = 4, name = "KA")
    private int ka;
    @SmartColumn(id = 5, name = "流通批發(fā)")
    private int wholesale;
    @SmartColumn(id = 6, name = "工業(yè)加工")
    private int industry;
    @SmartColumn(id = 7, name = "其他")
    private int other;
}
  • 步驟三:綁定數(shù)據(jù)
public class MainActivity extends AppCompatActivity {

    private SmartTable table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<UserInfo> list = new ArrayList<>();
        table = findViewById(R.id.table);
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",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("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));
        list.add(new UserInfo("沈陽(yáng)",100, 150, 50, 240, 1100, 450, 23458));

        table.setData(list);
        table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
    }
}
  • 實(shí)現(xiàn)效果


    注解方式效果圖.png

2.基本方式,手動(dòng)創(chuàng)建行與列

  • 步驟一:在布局文件中使用 SmartTable
<com.bin.david.form.core.SmartTable
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  • 步驟二:定義表格(自定義bean對(duì)象),與采用注解方式唯一的不同就是不在使用 @SmartTable與 @SmartColumn 進(jìn)行標(biāo)注
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:目標(biāo)值,restaurant:餐飲數(shù)量,ka:KA數(shù)量,wholesale:流通批發(fā)數(shù)量,industry:工業(yè)加工數(shù)量,other:其它數(shù)量
    private String city;
    private int name;
    private int count;
    private int restaurant;
    private int ka;
    private int wholesale;
    private int industry;
    private int other;
}
  • 步驟三:手動(dòng)創(chuàng)建列字段
//普通列
Column<String> city = new Column<>("部門/渠道", "city");
Column<Integer> name = new Column<>("板塊", "name");
Column<Integer> count = new Column<>("目標(biāo)值", "count");
Column<Integer> restaurant = new Column<>("餐飲", "restaurant");
Column<Integer> ka = new Column<>("KA", "ka");
Column<Integer> wholesale = new Column<>("流通批發(fā)", "wholesale");
Column<Integer> industry = new Column<>("工業(yè)加工", "industry");
Column<Integer> other = new Column<>("其他", "other");
//設(shè)置該列當(dāng)字段相同時(shí)自動(dòng)合并
city.setAutoMerge(true);
  • 步驟四:設(shè)置單元格內(nèi)容
//設(shè)置單元格內(nèi)容
List<User> list = new ArrayList<>();
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 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("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
list.add(new User("沈陽(yáng)", 100, 150, 50, 240, 1100, 450, 23458));
  • 步驟五:把數(shù)據(jù)綁定到 SmartTable 上
//表格數(shù)據(jù) datas 是需要填充的數(shù)據(jù)
TableData<User> tableData = new TableData<>("表格名", list, city, name, count, restaurant, ka, wholesale, industry, other);

//設(shè)置數(shù)據(jù)
table = findViewById(R.id.table);
table.setTableData(tableData);
table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));

3.兩種方式的優(yōu)勢(shì)和不足

1.注解方式

  • 使用上簡(jiǎn)單,幾行代碼就可以創(chuàng)建一個(gè)表格
  • 不能實(shí)現(xiàn)列的動(dòng)態(tài)創(chuàng)建
  • 不能實(shí)現(xiàn)列的排序

2.基本方式

  • 使用上稍稍比注解方式麻煩一點(diǎn)
  • 可以實(shí)現(xiàn)列的動(dòng)態(tài)創(chuàng)建(根據(jù)服務(wù)器返回的列的數(shù)量動(dòng)態(tài)創(chuàng)建表格)
  • 可以實(shí)現(xiàn)點(diǎn)擊列,對(duì)列進(jìn)行升序以及倒序排列

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

謝謝?。?!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容