私はアンドロイドに非常に慣れていません...ビューグループを処理する方法を誰かに教えてもらえますか?
例えば:
ファイルに線形レイアウトがありmain.xml
ます。ビューを追加できる唯一の方法findViewById
は、線形レイアウトの ID を使用して指定することです。ビューグループのハンドルを取得し、などの機能を実行する汎用メソッドを開発したいgetChildCount()
...
独自のレイアウトサブクラスを作成し、などのいくつかのメソッドをオーバーライドできますonFinishInflate()
。ビューのドキュメントにはそれに関するいくつかの情報があり、それを行う方法についてのチュートリアルがたくさんあります。
これは、レイアウトのすべてのチェック可能なビューを取得するためのチュートリアルの1つで説明していることです。
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}
/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}