Android tables are not used very often, but I made a list of SmartTable and thought Android mobile tables should look like this. The address is github.com/huangyanbin… “, has been put on Github no attention, recently some students said that it is quite good, why did not update. I want to write this article to sell. It’s worth it! I will introduce them one by one by function points:
Attached are other articles:
Use the beautiful Android table framework 2
Use the beautiful Android table framework 3
How to generate a table
<com.bin.david.form.core.SmartTable
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
Copy the code
This can be annotated with @smarttable and @SmartColumn fields
@SmartTable(name="User Information List")
public class UserInfo {
@SmartColumn(id =1,name = "Name")
private String name;
@SmartColumn(id=2,name="Age") private int age; . } List<UserInfo> list = new ArrayList<>(); . table = (SmartTable<UserInfo>) findViewById(R.id.table); table.setData(list);Copy the code
OK, this is the simplest annotated version. Let’s take a look at the normal version of the powerful. Just create the columns that need to be displayed and set the fields that need to be parsed, assuming userinfo.parent-name, just parent-name.
final Column<String> nameColumn = new Column<>("Name"."name");
final Column<Integer> ageColumn = new Column<>("Age"."age"); . tableData = new TableData<>("Test",list,nameColumn,ageColumn...) ; table.setTableData(tableData);Copy the code
beautify
Certainly some people say, this function, hehe. Come on, let’s sit down and start showing off the rich features. The interface is not beautiful, look here, format the content background:
table.getConfig().setContentBackgroundFormat(new BaseBackgroundFormat<CellInfo>() {
@Override
public int getBackGroundColor() {
return ContextCompat.getColor(AnnotationModeActivity.this,R.color.content_bg);
}
@Override
public boolean isDraw(CellInfo cellInfo) {
returncellInfo.position%2 ==0; }});Copy the code
Formatting data
We find the time column pretty ugly and want to format the time column
final IFormat<Long> format = new IFormat<Long>() {
@Override
public String format(Long aLong) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(aLong);
return calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH); }}; final Column<Long> timeColumn = new Column<>("Time"."time",format);
Copy the code
Also support table text, serial number, column title formatting; The table is composed of background, text, grid, padding and other configuration, you can refer to the demo;
Check this column, we want to show the checked icon
int size = DensityUtils.dp2px(this,15); Column<Boolean> checkColumn = new Column<>"Tick"."isCheck",new ImageResDrawFormat<Boolean>(size,size) {
@Override
protected Context getContext() {
return AnnotationModeActivity.this;
}
@Override
protected int getResourceID(Boolean isCheck, String value, int position) {
if(isCheck){
return R.mipmap.check;
}
return0; }});Copy the code
Provides support for text, multi-line text, text and icon combinations (up, down, left and right)
SetShowCount (true), which columns need to be counted, and setAutoCount (true), which columns need to be counted
tableData.setShowCount(true);
nameColumn.setAutoCount(true);
Copy the code
But this does not meet real needs, demand is often very bad. So a statistical interface is provided. The following is an example of the maximum time for statistics:
timeColumn.setAutoCount(true);
timeColumn.setCountFormat(new ICountFormat<Long, Long>() {
private long maxTime;
@Override
public void count(Long aLong) {
if(aLong > maxTime){
maxTime = aLong;
}
}
@Override
public Long getCount() {
return maxTime;
}
@Override
public String getCountString() {
return format.format(maxTime);
}
@Override
public void clearCount() { maxTime =0; }});Copy the code
Sometimes we need a title combination, so here’s how to play it:
Column groupColumn = new Column("Combination",nameColumn,ageColumn);
TableData<UserInfo> tableData = new TableData<>("User table",userInfos,groupColumn,timeColumn,checkColumn);
Copy the code
Dynamic effect
Fixed specified column and X – ordinal column, Y – ordinal column, column header, statistics row. You can turn it on on demand, and the combination is really great
//固定指定列
timeColumn.setFixed(true); Table.getconfig ().setfixedySequence ()true); Table.getconfig ().setfixedXSequence ()true); Table.getconfig ().setFixedCountrow ()true); Table.getconfig ().setFixedTitle()true);
Copy the code
The zoom
Of course, it must be zoomed in and out
table.setZoom(true); // You can set the maximum and minimum zoom valuessetZoom(boolean zoom,float maxZoom,float minZoom);
Copy the code
The event
Comment and click events
table.setOnColumnClickListener();
MultiLineBubbleTip<Column> tip = new MultiLineBubbleTip<Column>(this,R.mipmap.round_rect,R.mipmap.triangle,fontStyle) {
@Override
public boolean isShowTip(Column column, int position) {
if(column == nameColumn){
return true;
}
return false;
}
@Override
public String[] format(Column column, int position) {
UserInfo data = testData.get(position);
String[] strings = {"Remark"."Name:"+data.getName(),"Age:"+data.getAge()};
returnstrings; }};Copy the code
The sorting
You can sort by setting the column and then the column condition.
Table. SetSortColumn (ageColumn,false);
Copy the code
If you still don’t feel satisfied, you can define your own sorting rules
ageColumn.setComparator(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
returno1- o2; }});Copy the code
Support for two-dimensional arrays
You can use ArrayTableData instead of TableData. You can happily use two-dimensional arrays, and you don’t even have to set the column headings. In this way, some applications like ballot and seat selection can be realized.
String[] week = {"Day"."一"."二"."Three"."Four"."Five"."Six"}; The Integer [] [] infos = {,1,2,1,1,0,1,1,0,1,1,2,3 {0}, {4,2,1,1,0,1,1,0,1,1,2,2,3}, ,1,1,0,1,4,0,1,1,2,2,0,3,2,0,1,2,4,1,0,1,3,0,1,1 {2}, {2}, {0,1,2,4,1,0,1,4,0,1,1,2,2}, {1,0,1,3,2,2,0,1,2,1,1,0,4}, ,1,2,4,0,1,2,1,1,0,1,1,0 {3}}; ArrayTableData<Integer> tableData = ArrayTableData.create("Schedule",week,infos,new IDrawFormat<Integer>(){
@Override
public int measureWidth(Column<Integer> column, TableConfig config) {
return DensityUtils.dp2px(ArrayModeActivity.this,50);
}
@Override
public int measureHeight(Column<Integer> column, int position, TableConfig config) {
return DensityUtils.dp2px(ArrayModeActivity.this,50);
}
@Override
public void draw(Canvas c, Column<Integer> column, Integer integer, String value, Rect rect, int position, TableConfig config) {
Paint paint = config.getPaint();
int color;
switch (integer) {case 1:
color =R.color.github_con_1;
break;
case 2:
color =R.color.github_con_2;
break;
case 3:
color =R.color.github_con_3;
break;
case 4:
color =R.color.github_con_4;
break;
default:
color =R.color.github_con_0;
break; } paint.setStyle(Paint.Style.FILL); paint.setColor(ContextCompat.getColor(ArrayModeActivity.this, color)); c.drawRect(rect.left+5,rect.top+5,rect.right-5,rect.bottom-5,paint); }});Copy the code
other
There are many more features, including dynamic adding of beginning and end data, paging, formatting fonts, backgrounds, and more. I won’t go through them here.
- Dynamic data addition at the beginning and end is supported
AddData SmartTable. AddData (List
T, Boolean isFoot) dynamically.
- Set a single grid background
In this way, different backgrounds for a single grid are added to SmartTable. There are five IBackgroundFormat styles in TableConfig. DrawBackground: Boolean isDraw(T T) drawBackground: the default is to draw the entire background, of course you can define the IBackgroundFormat using other shapes.
- Set a single grid font
Due to support to a single grid background support, font color also need to adjust according to the background, so it also supports a single grid font Settings, IBackgroundFormat has int getTextColor(T T), you just need to rewrite it, according to the needs of different colors.
- paging
In the client side too much data experience is not good, so the development of paging mode, in the case of no annotation, only need to use PageTableData paging TableData class to replace the previous TableData TableData class, using PageTableData setPageSize method to set the number of pages. The paging is done. If you use annotations, add a pageSize attribute to the @Smarttable annotation element. SetData returns a PageTableData object that you can use for other Settings.
- other
Add notifyDataChanged to SmartTable to reparse the calculation layout;
Provides the back method to fling to the origin.
Making the address
Github.com/huangyanbin…