only inflate elements when needed

This commit is contained in:
AdrianLxM 2017-10-11 07:59:41 +02:00
parent c181aaaf34
commit 374983e1ae

View file

@ -53,6 +53,8 @@ public class TimeListEdit {
private double step; private double step;
private NumberFormat formatter; private NumberFormat formatter;
private Runnable save; private Runnable save;
private LinearLayout layout;
private int inflatedUntil = -1;
public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, double step, NumberFormat formatter, Runnable save) { public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, double step, NumberFormat formatter, Runnable save) {
this.context = context; this.context = context;
@ -68,7 +70,7 @@ public class TimeListEdit {
} }
private void buildView() { private void buildView() {
LinearLayout layout = (LinearLayout) view.findViewById(resLayoutId); layout = (LinearLayout) view.findViewById(resLayoutId);
TextView textlabel = new TextView(context); TextView textlabel = new TextView(context);
textlabel.setText(label); textlabel.setText(label);
@ -80,7 +82,34 @@ public class TimeListEdit {
TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium); TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium);
layout.addView(textlabel); layout.addView(textlabel);
for (int i = 0; i < 24; i++) { for (int i = 0; i < 24 && i < itemsCount(); i++) {
inflateRow(i);
inflatedUntil = i;
}
// last "plus" to append new interval
finalAdd = new ImageView(context);
finalAdd.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom);
illp.gravity = Gravity.CENTER;
layout.addView(finalAdd);
finalAdd.setLayoutParams(illp);
finalAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
callSave();
log();
fillView();
}
});
fillView();
}
private void inflateRow(int i) {
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
View childview = intervals[i] = inflater.inflate(R.layout.timelistedit_element, layout, false); View childview = intervals[i] = inflater.inflate(R.layout.timelistedit_element, layout, false);
spinners[i] = new SpinnerHelper(childview.findViewById(R.id.timelistedit_time)); spinners[i] = new SpinnerHelper(childview.findViewById(R.id.timelistedit_time));
@ -178,35 +207,15 @@ public class TimeListEdit {
layout.addView(childview); layout.addView(childview);
} }
// last "plus" to append new interval
finalAdd = new ImageView(context);
finalAdd.setImageResource(R.drawable.add);
LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom);
illp.gravity = Gravity.CENTER;
layout.addView(finalAdd);
finalAdd.setLayoutParams(illp);
finalAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0);
callSave();
log();
fillView();
}
});
fillView();
}
private void fillView() { private void fillView() {
for (int i = 0; i < 24; i++) { for (int i = 0; i < 24; i++) {
if (i < itemsCount()) { if (i < itemsCount()) {
intervals[i].setVisibility(View.VISIBLE); intervals[i].setVisibility(View.VISIBLE);
buildInterval(i); buildInterval(i);
} else } else if (i <= inflatedUntil){
intervals[i].setVisibility(View.GONE); intervals[i].setVisibility(View.GONE);
} }
}
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) { if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
finalAdd.setVisibility(View.VISIBLE); finalAdd.setVisibility(View.VISIBLE);
@ -339,6 +348,11 @@ public class TimeListEdit {
} }
private void addItem(int index, int timeAsSeconds, double value1, double value2) { private void addItem(int index, int timeAsSeconds, double value1, double value2) {
if(itemsCount()>inflatedUntil) {
layout.removeView(finalAdd);
inflateRow(++inflatedUntil);
layout.addView(finalAdd);
}
try { try {
// shift data // shift data
for (int i = data1.length(); i > index; i--) { for (int i = data1.length(); i > index; i--) {