私はカスタムレイアウトを開発していますが、属性はほとんどありません。 ATTRS:ビューへの入力に最適な方法
<declare-styleable name="CustomLayout"> //other attr... <attr name="data_set" format="reference"/> </declare-styleable>
データセットは、私はビューへの移入によって、私のレイアウトを埋めるそれによれば、単なる文字列の配列です:
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);
}
}
ビューへの移入のために最高の場所ですか!私はaddView()がrequestLayout()とinvalidate()をトリガすることを知っています。これは複数の項目に対してこれを行う最良の方法ではありませんか?だから私はアダプターベースのアプローチを使うべきですか?
ListViewのようなものを実装しても、私はビューの内部をonMeasure()にしていますが、それは現在の実装を最適化しません。あるいは、他のアプローチをお勧めしますか? –