The best way to populate views
I am developing a custom layout that has several attributes. Attrs:
<declare-styleable name="CustomLayout">
//other attr...
<attr name="data_set" format="reference"/>
</declare-styleable>
Dataset is just a string array, according to which I fill in my layout, filling in the views:
private Option[] mOptions; // just an array for filing content
public CustomLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context
.obtainStyledAttributes(attrs, R.styleable.CustomLayout);
int arrayId = array.getResourceId(R.styleable._data_set, -1);
if (arrayId != -1) {
array = getResources().obtainTypedArray(arrayId);
mOptions = new Option[array.length()];
for (int index = 0; index < array.length(); index++) {
mOptions[index] = new Option();
mOptions[index].setLabel(array.getString(index));
}
populateViews();
}
array.recycle();
}
private void populateViews() {
if (mOptions.length > 0) {
for (int index = 0; index < mOptions.length; index++) {
final Option option = mOptions[index];
TextView someTextView = (TextView) LayoutInflater.from(getContext())
.inflate(R.layout.some_layout, this, false);
//other initialization stuff
addView(someTextView);
}
}
Where is the best place to fill your views? As I know addView () fires requestLayout () and invalidate (), and this is not the best way to do this for multiple elements, is this not? So what should I do, should I use an adaptive approach?