only inflate elements when needed
This commit is contained in:
parent
c181aaaf34
commit
374983e1ae
1 changed files with 112 additions and 98 deletions
|
@ -53,6 +53,8 @@ public class TimeListEdit {
|
|||
private double step;
|
||||
private NumberFormat formatter;
|
||||
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) {
|
||||
this.context = context;
|
||||
|
@ -68,7 +70,7 @@ public class TimeListEdit {
|
|||
}
|
||||
|
||||
private void buildView() {
|
||||
LinearLayout layout = (LinearLayout) view.findViewById(resLayoutId);
|
||||
layout = (LinearLayout) view.findViewById(resLayoutId);
|
||||
|
||||
TextView textlabel = new TextView(context);
|
||||
textlabel.setText(label);
|
||||
|
@ -80,7 +82,34 @@ public class TimeListEdit {
|
|||
TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium);
|
||||
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);
|
||||
View childview = intervals[i] = inflater.inflate(R.layout.timelistedit_element, layout, false);
|
||||
spinners[i] = new SpinnerHelper(childview.findViewById(R.id.timelistedit_time));
|
||||
|
@ -178,35 +207,15 @@ public class TimeListEdit {
|
|||
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() {
|
||||
for (int i = 0; i < 24; i++) {
|
||||
if (i < itemsCount()) {
|
||||
intervals[i].setVisibility(View.VISIBLE);
|
||||
buildInterval(i);
|
||||
} else
|
||||
} else if (i <= inflatedUntil){
|
||||
intervals[i].setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) {
|
||||
finalAdd.setVisibility(View.VISIBLE);
|
||||
|
@ -339,6 +348,11 @@ public class TimeListEdit {
|
|||
}
|
||||
|
||||
private void addItem(int index, int timeAsSeconds, double value1, double value2) {
|
||||
if(itemsCount()>inflatedUntil) {
|
||||
layout.removeView(finalAdd);
|
||||
inflateRow(++inflatedUntil);
|
||||
layout.addView(finalAdd);
|
||||
}
|
||||
try {
|
||||
// shift data
|
||||
for (int i = data1.length(); i > index; i--) {
|
||||
|
|
Loading…
Reference in a new issue