preface

I wrote two articles about Android forms before, which were liked by many students. Some students came up with new requirements. Due to personal energy limited, can only increase personal more practical part of the function. Here are two new features.

Attach the addresses of the previous two articles

Nice Android table framework

Use the beautiful Android table framework 2

Array or List columns

The title of this feature is a bit tricky, for example:

For example, we are familiar with the class schedule. A student has 7 days a week, and there are 3 daytime periods in each day: morning, afternoon and evening. Each time period has specific Lesson. Do the students like isFav in this course? There is the LessonPoint of the course below the course. We want to use a table to show the timetable of all the students in a class.

Of course, the real need may not be so many levels, ha ha, the product can not be so abnormal here. This is just to verify that multilevel can be displayed correctly. The effect is as follows:

Annotation model

Students in class

  @SmartTable(name = "Class schedule")
  public class CollegeStudent {
    @SmartColumn(id = 1,name ="Name")
    private String name;

    @SmartColumn(type = ColumnType.ArrayChild)
    private List<Week> weeks;
    }
      
Copy the code

week

  public class Week {
    @SmartColumn(id=2,name ="Week")
    private String name;
    @SmartColumn(type = ColumnType.ArrayChild)
    private List<Time> times; }...Copy the code

time

public class DayTime {
    @SmartColumn(id=3,name ="Time")
    private String time;
    @SmartColumn(type = ColumnType.ArrayChild)
    private List<Lesson> lessons;
Copy the code

The @SmartColumn annotation has a type of type, ColumnType.ArrayChild means to drill into each of the objects inside to retrieve the annotation and parse it.

Columntype.arrayown (List

or int[]) It also parses each object, but does not continue to query for annotations on the object itself.

Pivots support arrays as well as lists.

Normal mode

      Column<String> studentNameColumn = new Column<>("Name"."name");
        ArrayColumn<String> weekNameColumn = new ArrayColumn<>("Week"."weeks.name");
        ArrayColumn<String> timeNameColumn = new ArrayColumn<>("Time"."weeks.times.time");
        ArrayColumn<String> lessonNameColumn = new ArrayColumn<>("Course"."weeks.times.lessons.name");

        ArrayColumn<String> pointNameColumn = new ArrayColumn<>("Knowledge point"."weeks.times.lessons.lessonPoints.name");
       ArrayColumn<Boolean> lessonFavColumn = new ArrayColumn<>("Do you like it?"."weeks.times.lessons.isFav");
        TableData<CollegeStudent> tableData = new TableData<>("Class schedule",students,studentNameColumn,
                weekNameColumn,timeNameColumn,lessonNameColumn,pointNameColumn,lessonFavColumn);
        table.setTableData(tableData);
Copy the code

The normal mode is as simple as replacing columns with ArrayColumn. You don’t need any notes.

Due to time constraints, the array List function does not support dynamic loading, automatic merge function. A class can display at most one List(single layer). There will be time to fill in later.Copy the code
Json data becomes a table

In many cases, the data columns we need to display are not fixed and need to be displayed according to real-time data. At this time, we will definitely think of using Map key-value to receive and convert the data into columns. It can be parsed just by passing in a List. Here is a common Json example:

I pulled json data from the Internet.

 String json="{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"links\":[{\"name\":\"Google\" ,\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"h ttp://www.SoSo.com\"},{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.b aidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"},{\"name\":\"Google\",\"url\":\"http://www.google.com\"}, {\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"},{\"name\":\"Go ogle\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url \":\"http://www.SoSo.com\"}]}";
        
Copy the code

As before, set the table data:

   MapTableData tableData = MapTableData.create("Form Name",JsonHelper.jsonToMapList(json));
   table.setTableData(tableData);
Copy the code

Then call MapTableData static method create to create MapTableData table data class, JsonHelper is assisting Json into Map collection. The table generated below:

Provides formatted data so that null data can display the specified text, provides an interceptor to determine whether the column needs to be intercepted without being displayed. Json parsing also supports multiple levels of query lists.

Automatic merging of cells

This one looks at the picture

Auto merge not enabled

Enable auto merge renderings:

Annotation model
   @SmartColumn(id =1,name = "Name",autoMerge = true)
    private String name;
    @SmartColumn(id=2,name="Age"autoMerge = true,maxMergeCount = 3)
    private int age;
    
Copy the code
Normal mode
    nameColumn.setAutoMerge(true);
    ageColumn.setAutoMerge(true);
    ageColumn.setMaxMergeCount(3);
Copy the code

SetAutoMerge means to enable automatic merge, setMaxMergeCount sets the maximum number of merges, or int if not set. The merge rule is based on whether values are equal after format.

Set the table minimum

A lot of times maybe we only have two or three columns, it’s ugly not to spread across the screen,

 table.getConfig().setMinTableWidth(screenWith); // Set the minimum widthCopy the code

Isn’t it more beautiful? The width is automatically expanded in proportion to the previous scale.

other
To speed up the show

If the columns are single rows and the font size of the columns is the same, you can set column column.setfast (true) to speed up the display without double-counting the width and height of each column. Especially with large amounts of data, tests showed that it was three times faster.

Demo code:

They are in the demo in the above example, https://github.com/huangyanbin/smartTable, and finally thank you for your support.Copy the code