强力推荐一款 android 自动生成表格框架 smartTable,它有着非常强大的功能,而且日趋完善,能实现很多功能,而且使用也十分简单。
简单介绍它的功能:
具体使用方法不再赘述,可以去 GitHub 去查看:https://github.com/huangyanbin/smartTable
由于smartTable中没有实现选中行的功能,现在让我们来手动实现,效果图在文章最后,实现思路:
smartTable 生成的表格数据是按列逐个创建出来的,每一列可以建立一个监听事件,获得被点击的行索引,再根据行索引去其他列找到对应的记录,即可拼接出完整的行记录。
添加 JitPack repository 到你的 Project build 文件
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
在 app build 文件中增加依赖
dependencies {
compile 'com.github.huangyanbin:SmartTable:2.2.0'
}
public class User {
private String name;
private Integer age;
private Boolean operation; //用于记录是否被选中的状态
public User(String name, Integer age, boolean operation){
this.name = name;
this.age = age;
this.operation = operation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getOperation() {
return operation;
}
public void setOperation(Boolean operation) {
this.operation = operation;
}
}
public class MainActivity extends AppCompatActivity {
private SmartTable table;
Column name;
Column age;
Column operation;
List name_selected = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
table = (SmartTable)findViewById(R.id.table);
List codeList = new ArrayList();
codeList.add(new User("user_01",20,false));
codeList.add(new User("user_02",21,false));
codeList.add(new User("user_03",22,false));
codeList.add(new User("user_04",22,false));
codeList.add(new User("user_05",21,false));
codeList.add(new User("user_06",21,false));
codeList.add(new User("user_07",23,false));
codeList.add(new User("user_08",20,false));
name = new Column<>("姓名", "name");
name.setOnColumnItemClickListener(new OnColumnItemClickListener() {
@Override
public void onClick(Column column, String value, String bool, int position) {
Toast.makeText(MainActivity.this,"点击了"+value,Toast.LENGTH_SHORT).show();
if(operation.getDatas().get(position)){
showName(position, false);
operation.getDatas().set(position,false);
}else{
showName(position, true);
operation.getDatas().set(position,true);
}
table.refreshDrawableState(); //不要忘记刷新表格,否则选中效果会延时一步
table.invalidate();
}
});
age = new Column<>("年龄", "age");
age.setOnColumnItemClickListener(new OnColumnItemClickListener() {
@Override
public void onClick(Column column, String value, Integer bool, int position) {
// Toast.makeText(CodeListActivity.this,"点击了"+value,Toast.LENGTH_SHORT).show();
if(operation.getDatas().get(position)){
showName(position, false);
operation.getDatas().set(position,false);
}else{
showName(position, true);
operation.getDatas().set(position,true);
}
table.refreshDrawableState();
table.invalidate();
}
});
int size = DensityUtils.dp2px(this,30);
operation = new Column<>("勾选", "operation", new ImageResDrawFormat(size,size) { //设置"操作"这一列以图标显示 true、false 的状态
@Override
protected Context getContext() {
return MainActivity.this;
}
@Override
protected int getResourceID(Boolean isCheck, String value, int position) {
if(isCheck){
return R.mipmap.check; //将图标提前放入 app/res/mipmap 目录下
}
return R.mipmap.unselect_check;
}
});
operation.setComputeWidth(40);
operation.setOnColumnItemClickListener(new OnColumnItemClickListener() {
@Override
public void onClick(Column column, String value, Boolean bool, int position) {
// Toast.makeText(CodeListActivity.this,"点击了"+value,Toast.LENGTH_SHORT).show();
if(operation.getDatas().get(position)){
showName(position, false);
operation.getDatas().set(position,false);
}else{
showName(position, true);
operation.getDatas().set(position,true);
}
table.refreshDrawableState();
table.invalidate();
}
});
final TableData tableData = new TableData<>("测试标题",codeList, operation, name, age);
table.getConfig().setShowTableTitle(false);
table.setTableData(tableData);
// table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
table.getConfig().setMinTableWidth(1024); //设置表格最小宽度
FontStyle style = new FontStyle();
style.setTextSize(30);
table.getConfig().setContentStyle(style); //设置表格主题字体样式
table.getConfig().setColumnTitleStyle(style); //设置表格标题字体样式
table.getConfig().setContentCellBackgroundFormat(new BaseCellBackgroundFormat() { //设置隔行变色
@Override
public int getBackGroundColor(CellInfo cellInfo) {
if(cellInfo.row%2 ==1) {
return ContextCompat.getColor(MainActivity.this, R.color.tableBackground); //需要在 app/res/values 中添加 #d4d4d4
}else{
return TableConfig.INVALID_COLOR;
}
}
});
table.getConfig().setMinTableWidth(1024); //设置最小宽度
}
/**
* 收集所有被勾选的姓名记录到 name_selected 集合中,并实时更新
* @param position 被选择记录的行数,根据行数用来找到其他列中该行记录对应的信息
* @param selectedState 当前的操作状态:选中 || 取消选中
*/
public void showName(int position, boolean selectedState){
List rotorIdList = name.getDatas();
if(position >-1){
String rotorTemp = rotorIdList.get(position);
if(selectedState && !name_selected.contains(rotorTemp)){ //当前操作是选中,并且“所有选中的姓名的集合”中没有该记录,则添加进去
name_selected.add(rotorTemp);
}else if(!selectedState && name_selected.contains(rotorTemp)){ // 当前操作是取消选中,并且“所有选中姓名的集合”总含有该记录,则删除该记录
name_selected.remove(rotorTemp);
}
}
for(String s:name_selected){
System.out.print(s + " -- ");
}
}
}