mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-31 12:01:54 +02:00
Postpone creating text until view is requested
Wait until the measurement is actually to be shown in the gui until creating the spanned text. Cache it so that it doesn't have to be recalculated. Drop the first hidden column that contained the id. Instead implement Adapter.getItemId and use the supplied id in onClickListenerRow. Speeds up the initial loading of the table.
This commit is contained in:
@@ -31,7 +31,6 @@ import android.support.v4.app.Fragment;
|
|||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
import android.text.SpannedString;
|
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@@ -109,6 +108,9 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
tableDataView = (ListView) tableView.findViewById(R.id.tableDataView);
|
tableDataView = (ListView) tableView.findViewById(R.id.tableDataView);
|
||||||
tableHeaderView = (LinearLayout) tableView.findViewById(R.id.tableHeaderView);
|
tableHeaderView = (LinearLayout) tableView.findViewById(R.id.tableHeaderView);
|
||||||
|
|
||||||
|
tableDataView.setAdapter(new ListViewAdapter());
|
||||||
|
tableDataView.setOnItemClickListener(new onClickListenerRow());
|
||||||
|
|
||||||
optionMenu = (ImageView) tableView.findViewById(R.id.optionMenu);
|
optionMenu = (ImageView) tableView.findViewById(R.id.optionMenu);
|
||||||
|
|
||||||
measurementsList = new ArrayList<>();
|
measurementsList = new ArrayList<>();
|
||||||
@@ -192,15 +194,12 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
@Override
|
@Override
|
||||||
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList)
|
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList)
|
||||||
{
|
{
|
||||||
tableDataView.setAdapter(new ListViewAdapter(new ArrayList<HashMap<Integer, Spanned>>())); // delete all data in the table with an empty adapter array list
|
|
||||||
|
|
||||||
if (scaleMeasurementList.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final int maxSize = 25;
|
final int maxSize = 25;
|
||||||
|
|
||||||
int subpageCount = (int)Math.ceil(scaleMeasurementList.size() / (double)maxSize);
|
final int subpageCount = (int)Math.ceil(scaleMeasurementList.size() / (double)maxSize);
|
||||||
|
if (selectedSubpageNr >= subpageCount) {
|
||||||
|
selectedSubpageNr = Math.max(0, subpageCount - 1);
|
||||||
|
}
|
||||||
|
|
||||||
subpageView.removeAllViews();
|
subpageView.removeAllViews();
|
||||||
|
|
||||||
@@ -216,7 +215,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
moveSubpageLeft.setEnabled(selectedSubpageNr > 0);
|
moveSubpageLeft.setEnabled(selectedSubpageNr > 0);
|
||||||
subpageView.addView(moveSubpageLeft);
|
subpageView.addView(moveSubpageLeft);
|
||||||
|
|
||||||
for (int i=0; i<subpageCount; i++) {
|
for (int i = 0; i < subpageCount; i++) {
|
||||||
TextView subpageNrView = new TextView(tableView.getContext());
|
TextView subpageNrView = new TextView(tableView.getContext());
|
||||||
subpageNrView.setOnClickListener(new onClickListenerSubpageSelect());
|
subpageNrView.setOnClickListener(new onClickListenerSubpageSelect());
|
||||||
subpageNrView.setText(Integer.toString(i+1));
|
subpageNrView.setText(Integer.toString(i+1));
|
||||||
@@ -225,6 +224,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
|
|
||||||
subpageView.addView(subpageNrView);
|
subpageView.addView(subpageNrView);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subpageView.getChildCount() > 1) {
|
if (subpageView.getChildCount() > 1) {
|
||||||
TextView selectedSubpageNrView = (TextView) subpageView.getChildAt(selectedSubpageNr + 1);
|
TextView selectedSubpageNrView = (TextView) subpageView.getChildAt(selectedSubpageNr + 1);
|
||||||
if (selectedSubpageNrView != null) {
|
if (selectedSubpageNrView != null) {
|
||||||
@@ -247,6 +247,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
|
|
||||||
tableHeaderView.removeAllViews();
|
tableHeaderView.removeAllViews();
|
||||||
|
|
||||||
|
ArrayList<MeasurementView> visibleMeasurements = new ArrayList<>();
|
||||||
for (MeasurementView measurement : measurementsList) {
|
for (MeasurementView measurement : measurementsList) {
|
||||||
measurement.updatePreferences(prefs);
|
measurement.updatePreferences(prefs);
|
||||||
|
|
||||||
@@ -259,50 +260,16 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
headerIcon.getLayoutParams().height = pxImageDp(20);
|
headerIcon.getLayoutParams().height = pxImageDp(20);
|
||||||
|
|
||||||
tableHeaderView.addView(headerIcon);
|
tableHeaderView.addView(headerIcon);
|
||||||
|
|
||||||
|
visibleMeasurements.add(measurement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<HashMap<Integer, Spanned>> dataRowList = new ArrayList<>();
|
ListViewAdapter adapter = (ListViewAdapter) tableDataView.getAdapter();
|
||||||
|
|
||||||
int displayCount = 0;
|
final int startOffset = maxSize * selectedSubpageNr;
|
||||||
|
final int endOffset = Math.min(startOffset + maxSize + 1, scaleMeasurementList.size());
|
||||||
for (int i = (maxSize * selectedSubpageNr); i < scaleMeasurementList.size(); i++) {
|
adapter.setMeasurements(visibleMeasurements, scaleMeasurementList.subList(startOffset, endOffset), maxSize);
|
||||||
ScaleMeasurement scaleMeasurement = scaleMeasurementList.get(i);
|
|
||||||
|
|
||||||
ScaleMeasurement prevScaleMeasurement = null;
|
|
||||||
if (i < scaleMeasurementList.size() - 1) {
|
|
||||||
prevScaleMeasurement = scaleMeasurementList.get(i + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<Integer, Spanned> dataRow = new HashMap<>();
|
|
||||||
|
|
||||||
int columnNr = 0;
|
|
||||||
dataRow.put(columnNr++, new SpannedString(Long.toString(scaleMeasurement.getId())));
|
|
||||||
|
|
||||||
for (MeasurementView measurement : measurementsList) {
|
|
||||||
measurement.loadFrom(scaleMeasurement, prevScaleMeasurement);
|
|
||||||
|
|
||||||
if (measurement.isVisible()) {
|
|
||||||
SpannableStringBuilder text = new SpannableStringBuilder();
|
|
||||||
text.append(measurement.getValueAsString());
|
|
||||||
text.append("\n");
|
|
||||||
measurement.appendDiffValue(text);
|
|
||||||
|
|
||||||
dataRow.put(columnNr++, text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dataRowList.add(dataRow);
|
|
||||||
|
|
||||||
displayCount++;
|
|
||||||
|
|
||||||
if (maxSize <= displayCount) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tableDataView.setAdapter(new ListViewAdapter(dataRowList));
|
|
||||||
tableDataView.setOnItemClickListener(new onClickListenerRow());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int pxImageDp(float dp) {
|
private int pxImageDp(float dp) {
|
||||||
@@ -311,14 +278,11 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
|
|
||||||
private class onClickListenerRow implements AdapterView.OnItemClickListener {
|
private class onClickListenerRow implements AdapterView.OnItemClickListener {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long click_id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
LinearLayout dataRow = (LinearLayout)view;
|
|
||||||
TextView idTextView = (TextView) dataRow.getChildAt(0);
|
|
||||||
int id = Integer.parseInt(idTextView.getText().toString());
|
|
||||||
|
|
||||||
Intent intent = new Intent(tableView.getContext(), DataEntryActivity.class);
|
Intent intent = new Intent(tableView.getContext(), DataEntryActivity.class);
|
||||||
intent.putExtra(DataEntryActivity.EXTRA_ID, id);
|
intent.putExtra(DataEntryActivity.EXTRA_ID, (int)id);
|
||||||
startActivityForResult(intent, 1); }
|
startActivityForResult(intent, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void importTable() {
|
private void importTable() {
|
||||||
@@ -480,69 +444,102 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
|
|
||||||
private class ListViewAdapter extends BaseAdapter {
|
private class ListViewAdapter extends BaseAdapter {
|
||||||
|
|
||||||
private ArrayList<HashMap<Integer, Spanned>> dataList;
|
private List<MeasurementView> visibleMeasurements;
|
||||||
private LinearLayout row;
|
private List<ScaleMeasurement> scaleMeasurements;
|
||||||
|
private int measurementsToShow = 0;
|
||||||
|
|
||||||
public ListViewAdapter(ArrayList<HashMap<Integer, Spanned>> list) {
|
private Spanned[][] stringCache;
|
||||||
super();
|
|
||||||
this.dataList = list;
|
private ArrayList<HashMap<Integer, Spanned>> dataList;
|
||||||
|
|
||||||
|
public void setMeasurements(List<MeasurementView> visibleMeasurements,
|
||||||
|
List<ScaleMeasurement> scaleMeasurements,
|
||||||
|
int maxSize) {
|
||||||
|
this.visibleMeasurements = visibleMeasurements;
|
||||||
|
this.scaleMeasurements = scaleMeasurements;
|
||||||
|
measurementsToShow = Math.min(scaleMeasurements.size(), maxSize);
|
||||||
|
|
||||||
|
stringCache = new Spanned[measurementsToShow][visibleMeasurements.size()];
|
||||||
|
|
||||||
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
return dataList.size();
|
return measurementsToShow;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getItem(int position) {
|
public Object getItem(int position) {
|
||||||
return dataList.get(position);
|
return scaleMeasurements.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(int position) {
|
public long getItemId(int position) {
|
||||||
return 0;
|
return scaleMeasurements.get(position).getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View getView(int position, View convertView, ViewGroup parent) {
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
if (dataList.isEmpty()) {
|
// Create entries in stringCache if needed
|
||||||
return convertView;
|
if (stringCache[position][0] == null) {
|
||||||
}
|
ScaleMeasurement measurement = scaleMeasurements.get(position);
|
||||||
|
ScaleMeasurement prevMeasurement = null;
|
||||||
|
if (position + 1 < scaleMeasurements.size()) {
|
||||||
|
prevMeasurement = scaleMeasurements.get(position + 1);
|
||||||
|
}
|
||||||
|
|
||||||
if (convertView == null) {
|
for (int i = 0; i < visibleMeasurements.size(); ++i) {
|
||||||
row = new LinearLayout(getContext());
|
visibleMeasurements.get(i).loadFrom(measurement, prevMeasurement);
|
||||||
convertView = row;
|
|
||||||
|
|
||||||
for (int i = 0; i< dataList.get(0).size(); i++) {
|
SpannableStringBuilder string = new SpannableStringBuilder();
|
||||||
TextView column = new TextView(getContext());
|
string.append(visibleMeasurements.get(i).getValueAsString());
|
||||||
column.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
|
string.append("\n");
|
||||||
column.getLayoutParams().width = 0;
|
visibleMeasurements.get(i).appendDiffValue(string);
|
||||||
column.setGravity(Gravity.CENTER);
|
|
||||||
|
|
||||||
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_XLARGE &&
|
stringCache[position][i] = string;
|
||||||
(getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_LARGE) {
|
|
||||||
column.setTextSize(COMPLEX_UNIT_DIP, 9);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == 0) {
|
|
||||||
column.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
row.addView(column);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LinearLayout convView = (LinearLayout)convertView;
|
// Create view if needed
|
||||||
|
LinearLayout row;
|
||||||
|
if (convertView == null) {
|
||||||
|
row = new LinearLayout(getContext());
|
||||||
|
|
||||||
HashMap<Integer, Spanned> map = dataList.get(position);
|
final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
|
||||||
|
final boolean isSmallScreen =
|
||||||
|
screenSize != Configuration.SCREENLAYOUT_SIZE_XLARGE
|
||||||
|
&& screenSize != Configuration.SCREENLAYOUT_SIZE_LARGE;
|
||||||
|
|
||||||
for (int i = 0; i < map.size(); i++) {
|
for (int i = 0; i < visibleMeasurements.size(); ++i) {
|
||||||
TextView column = (TextView)convView.getChildAt(i);
|
TextView column = new TextView(getContext());
|
||||||
column.setText(map.get(i));
|
column.setLayoutParams(new LinearLayout.LayoutParams(
|
||||||
|
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
|
||||||
|
column.getLayoutParams().width = 0;
|
||||||
|
column.setGravity(Gravity.CENTER);
|
||||||
|
|
||||||
|
if (isSmallScreen) {
|
||||||
|
column.setTextSize(COMPLEX_UNIT_DIP, 9);
|
||||||
|
}
|
||||||
|
row.addView(column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
row = (LinearLayout) convertView;
|
||||||
}
|
}
|
||||||
|
|
||||||
return convertView;
|
// Fill view with data
|
||||||
|
for (int i = 0; i < visibleMeasurements.size(); ++i) {
|
||||||
|
TextView column = (TextView) row.getChildAt(i);
|
||||||
|
column.setText(stringCache[position][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasStableIds() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user