Skip to content

Commit

Permalink
支持首尾动态添加数据 addData
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyanbin committed Dec 5, 2017
1 parent e466037 commit 036e846
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 22 deletions.
25 changes: 24 additions & 1 deletion app/src/main/java/com/bin/david/smarttable/NetHttpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
Expand All @@ -29,6 +30,14 @@
public class NetHttpActivity extends AppCompatActivity {

private SmartTable<PM25> table;
private Handler mHandler = new Handler();
private boolean isFrist = true;
private Runnable AddDataRunnable = new Runnable() {
@Override
public void run() {
getData();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -87,13 +96,27 @@ public void onError(Call call, Exception e, int id) {

@Override
public void onResponse(String response, int id) {

Gson gson = new Gson();
Type type = new TypeToken<ArrayList<PM25>>() {}.getType();
List<PM25> pm25List = gson.fromJson(response,type);
table.setData(pm25List);
if(isFrist) {
table.setData(pm25List);
isFrist = false;
}else{
table.addData(pm25List,false);
table.back();
}
mHandler.postDelayed(AddDataRunnable,1000);

}

});
}

@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(AddDataRunnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand Down Expand Up @@ -72,6 +73,24 @@ public class ParseModeActivity extends AppCompatActivity implements View.OnClick
private BaseCheckDialog<TableStyle> chartDialog;
private QuickChartDialog quickChartDialog;
private Map<String,Bitmap> map = new HashMap<>();
private Handler mHandler = new Handler();
private boolean isFrist = true;
private Runnable AddDataRunnable = new Runnable() {
@Override
public void run() {
Random random = new Random();
List<TanBean> tanBeans = TanBean.initDatas();
final List<UserData> testData = new ArrayList<>();
for(int i = 0;i <5; i++) {
int urlSize = tanBeans.size();
UserData userData = new UserData("用户"+i, random.nextInt(70), System.currentTimeMillis()
- random.nextInt(70)*3600*1000*24,true,new ChildData("测试"+i));
userData.setUrl(tanBeans.get(i%urlSize).getUrl());
testData.add(userData);
table.addData(testData,true);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -84,7 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
List<TanBean> tanBeans = TanBean.initDatas();
//测试 从其他地方获取url
int urlSize = tanBeans.size();
for(int i = 0;i <500; i++) {
for(int i = 0;i <50; i++) {
UserData userData = new UserData("用户"+i, random.nextInt(70), System.currentTimeMillis()
- random.nextInt(70)*3600*1000*24,true,new ChildData("测试"+i));
userData.setUrl(tanBeans.get(i%urlSize).getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.Log;

import com.bin.david.form.data.Column;
import com.bin.david.form.data.ColumnInfo;
Expand Down Expand Up @@ -251,6 +252,7 @@ private void drawContent(Canvas canvas) {
}
int right = left + width;
if (left < showRect.right) {
Log.e("li","value"+values.size()+"info.getLineHeightArray"+info.getLineHeightArray().length);
for (int j = 0; j < values.size(); j++) {
String value = values.get(j);
int bottom = (int) (top + info.getLineHeightArray()[j]*config.getZoom());
Expand Down
15 changes: 15 additions & 0 deletions form/src/main/java/com/bin/david/form/core/SmartTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public void setZoom(boolean zoom,int maxScale) {

}

public void addData(List<T> t,boolean isFoot){
if(t != null && t.size() >0) {
int size = tableData.getT().size();
parser.addData(tableData, t,isFoot,config);
measurer.addTableHeight(tableData, size);
}
invalidate();
}

/**
* 滚动到原点
*/
public void end(){
matrixHelper.flingEnd(showRect,tableRect);
}
/**
* 滚动到原点
*/
Expand Down
13 changes: 13 additions & 0 deletions form/src/main/java/com/bin/david/form/core/TableMeasurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,20 @@ public void reSetShowRect(Rect showRect,Rect tableRect){
showRect.right = tableRect.right;
}
}
/**
* 添加table高度
* @param tableData
* @return
*/
public void addTableHeight(TableData<T> tableData,int startPosition){
TableInfo tableInfo = tableData.getTableInfo();
Rect tableRect = tableInfo.getTableRect();
int[] lineArray = tableInfo.getLineHeightArray();
for(int i = startPosition;i<lineArray.length;i++){

tableRect.bottom+= lineArray[i];
}
}



Expand Down
26 changes: 26 additions & 0 deletions form/src/main/java/com/bin/david/form/core/TableParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ public List<Column> parse(TableData<T> tableData, TableConfig config){
return tableData.getColumns();
}

/**
* 添加数据
*/
public void addData(TableData<T> tableData, List<T> addData,boolean isFoot, TableConfig config){

try {
int size = tableData.getT().size();
if(isFoot) {
tableData.getT().addAll(addData);
}else{
tableData.getT().addAll(0,addData);
}
TableInfo tableInfo = tableData.getTableInfo();
tableInfo.addLine(addData.size());
for (Column column : tableData.getChildColumns()) {
column.addData(addData,tableInfo,config,size,isFoot);
}
}catch (NoSuchFieldException e){
throw new TableException(
"NoSuchFieldException :Please check whether field name is correct!");
}catch (IllegalAccessException e){
throw new TableException(
"IllegalAccessException :Please make sure that access objects are allowed!");
}
}


/**
* 排序
Expand Down
105 changes: 90 additions & 15 deletions form/src/main/java/com/bin/david/form/data/Column.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bin.david.form.data;

import android.util.Log;

import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.format.count.DecimalCountFormat;
import com.bin.david.form.data.format.count.ICountFormat;
Expand Down Expand Up @@ -167,7 +169,7 @@ public T getData(Object o) throws NoSuchFieldException, IllegalAccessException {
if (field != null) {
Object child = o;
if (fieldNames.length == 0 || fieldNames.length == 1) {
return getFieldValue(field, o);
return getFieldValue(field, o,true);
}
for (int i = 0; i < fieldNames.length; i++) {
if (child == null) {
Expand All @@ -179,7 +181,7 @@ public T getData(Object o) throws NoSuchFieldException, IllegalAccessException {
return null;
}
if (i == fieldNames.length - 1) {
return getFieldValue(childField, child);
return getFieldValue(childField, child,true);
} else {
field.setAccessible(true);
child = field.get(child);
Expand All @@ -203,7 +205,10 @@ public void fillData(List<Object> objects, TableInfo tableInfo, TableConfig conf
if(countFormat != null){
countFormat.clearCount();
}
if (objects != null && objects.size() > 0) {
if(datas.size() == objects.size()){
return;
}
if (objects.size() > 0) {
int[] lineHeightArray = tableInfo.getLineHeightArray();
Object firstObject = objects.get(0);
Class clazz = firstObject.getClass();
Expand All @@ -216,33 +221,30 @@ public void fillData(List<Object> objects, TableInfo tableInfo, TableConfig conf
Object o = objects.get(k);
Object child = o;
if (o == null) {
datas.add(null);
values.add("");
addData(null,"",true);
setRowHeight(config, lineHeightArray, k,null);
continue;
}
if (fieldNames.length == 0 || fieldNames.length == 1) {
T t = getFieldValue(field, o);
T t = getFieldValue(field, o,true);
setRowHeight(config, lineHeightArray, k,t);
continue;
}
for (int i = 0; i < fieldNames.length; i++) {
if (child == null) {
datas.add(null);
values.add("");
addData(null,"",true);
setRowHeight(config, lineHeightArray, k,null);
break;
}
Class childClazz = child.getClass();
Field childField = childClazz.getDeclaredField(fieldNames[i]);
if (childField == null) {
datas.add(null);
values.add("");
addData(null,"",true);
setRowHeight(config, lineHeightArray, k,null);
break;
}
if (i == fieldNames.length - 1) {
T t = getFieldValue(childField, child);
T t = getFieldValue(childField, child,true);
setRowHeight(config, lineHeightArray, k,t);
} else {
field.setAccessible(true);
Expand Down Expand Up @@ -284,17 +286,91 @@ private void setRowHeight(TableConfig config, int[] lineHeightArray, int positio
}
}

/**
* 填充数据
* @param objects 对象列表
* @param tableInfo 表格信息
* @param config 配置
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/

public void addData(List<Object> objects, TableInfo tableInfo, TableConfig config,int startPosition,boolean isFoot) throws NoSuchFieldException, IllegalAccessException {
if(objects.size()+ startPosition == datas.size()){
return;
}
if (objects.size() > 0) {
int[] lineHeightArray = tableInfo.getLineHeightArray();
Object firstObject = objects.get(0);
Class clazz = firstObject.getClass();
String[] fieldNames = fieldName.split("\\.");
String firstFieldName = fieldNames.length == 0 ? fieldName : fieldNames[0];
Field field = clazz.getDeclaredField(firstFieldName);
if (field != null) {
int size = objects.size();
for (int k = 0; k < size; k++) {
Object o = objects.get(isFoot ? k:(size-1-k));
Object child = o;
if (o == null) {
addData(null,"",isFoot);
setRowHeight(config, lineHeightArray, k+startPosition,null);
continue;
}
if (fieldNames.length == 0 || fieldNames.length == 1) {
T t = getFieldValue(field, o,isFoot);
setRowHeight(config, lineHeightArray, k+startPosition,t);
continue;
}
for (int i = 0; i < fieldNames.length; i++) {
if (child == null) {
addData(null,"",isFoot);
setRowHeight(config, lineHeightArray, k+startPosition,null);
break;
}
Class childClazz = child.getClass();
Field childField = childClazz.getDeclaredField(fieldNames[i]);
if (childField == null) {
addData(null,"",isFoot);
setRowHeight(config, lineHeightArray, k+startPosition,null);
break;
}
if (i == fieldNames.length - 1) {
T t = getFieldValue(childField, child,isFoot);
setRowHeight(config, lineHeightArray, k+startPosition,t);
} else {
field.setAccessible(true);
child = field.get(child);
}
}

}
}
}
}

private void addData(T t,String value,boolean isFoot){
if(isFoot) {
datas.add(t);
values.add(value);
}else {
datas.add(0,t);
values.add(0,value);
}
}


/**
* 反射得到值
*
* @param field 成员变量
* @param o 对象
* @throws IllegalAccessException
*/
private T getFieldValue(Field field, Object o) throws IllegalAccessException {
private T getFieldValue(Field field, Object o,boolean isFoot) throws IllegalAccessException {
field.setAccessible(true);
T t = (T) field.get(o);
datas.add(t);


String value;
if (format != null) {
value = format.format(t);
Expand All @@ -304,8 +380,7 @@ private T getFieldValue(Field field, Object o) throws IllegalAccessException {
if (value.length() > maxValueLength) {
maxValueLength = value.length();
longestValue = value;
}
values.add(value);
} addData(t,value,isFoot);
return t;
}

Expand Down
1 change: 1 addition & 0 deletions form/src/main/java/com/bin/david/form/data/TableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void setT(List<T> t) {
}



public List<Column> getChildColumns() {
return childColumns;
}
Expand Down
Loading

0 comments on commit 036e846

Please sign in to comment.