【質問】親LinearLayout直下にある全ての子Viewにアクセスする方法について

3,743 views
Skip to first unread message

ka

unread,
Mar 27, 2013, 6:45:24 AM3/27/13
to android-g...@googlegroups.com
ご教授頂きたく存じます。

親LinearLayout直下にある全ての子Viewにアクセスする方法を考えておりますが、親の情報しか取れず、詰まっております。
もし、方法をご存知でしたら、ご教授頂きたく存じます。

こねくり回しているソースは下記となります。

【Javaソース】
view = inflater.inflate(R.layout.reserveheater, container, false);

ViewGroup ViewList = (ViewGroup)view.findViewById(R.id.layout);

for(int i = 0; i < ViewList.getChildCount(); i++){
View ViewChild =  ViewList.getChildAt(i);
if(ViewChild instanceof EditText){
EditText mET = (EditText)ViewChild;
mET.setOnTouchListener(this);
}else if(ViewChild instanceof Spinner){
Spinner mSP = (Spinner)ViewChild;
mSP.setOnItemSelectedListener(this);
}else if(ViewChild instanceof Button){
Button mBT = (Button)ViewChild;
mBT.setOnClickListener(this);
}
}

【xml】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f0ff"
    android:padding="20px"
    >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text1"
            android:background="#f00f"
            />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ff00"
            android:padding="20px"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text2"
                android:background="#f0f0"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button"
                />
    </LinearLayout>
</LinearLayout>


長田努

unread,
Mar 27, 2013, 6:56:01 AM3/27/13
to android-g...@googlegroups.com
kaさん、こんばんは! 長田と申します。

それぞれのwidgetにandroid:idを付与して、親からgetChildCount()分だけgetChildAt()で取得するなどの方法がありますが、いかがでしょうか?

参考: http://t-kashima.hateblo.jp/entry/20110922/1316703113

各widgetにandroid:idが付与されていればfindViewByIdでも取得可能です。

ご希望通りの回答でなければ失礼いたしました。
ご確認、宜しくお願い致します。

長田

2013年3月27日 19:45 ka <gagaha...@yahoo.co.jp>:
> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com
> にメールを送信します。
> このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
> http://groups.google.com/group/android-group-japan?hl=ja
> からこのグループにアクセスしてください。
> その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。
>
>

ka

unread,
Mar 27, 2013, 7:05:40 AM3/27/13
to android-g...@googlegroups.com
長田様

ありがとうございます。

参考サイトはすでに確認済みなのですが、getChildCount()を呼び出すと1が返却され、親のLinearLayoutしか取れていません。

>各widgetにandroid:idが付与されていればfindViewByIdでも取得可能です。 
このやり方はわかっているのですが、たとえば100個登録した場合、一つ一つにリスナー登録などを行わなければならず、
かなり面倒なため、ViewGroup の仕組みを使ってリスナー登録等を出来ないかと検討している次第です。

2013年3月27日水曜日 19時56分01秒 UTC+9 TOsada:
> このグループから退会し、メールの受信を停止するには、android-group-japan+unsubscribe@googlegroups.com
> にメールを送信します。
> このグループに投稿するには、android-group-ja...@googlegroups.com にメールを送信してください。

titoi2

unread,
Mar 27, 2013, 7:37:36 AM3/27/13
to android-g...@googlegroups.com
こんばんは
取れたLinearLayout に対してgetChildxxx をしてみてはいかがでしょうか。


2013年3月27日 20:05 ka <gagaha...@yahoo.co.jp>:
このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com にメールを送信します。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。

ka

unread,
Mar 27, 2013, 8:19:27 AM3/27/13
to android-g...@googlegroups.com
こんばんわ。

長田様、titoi2様有難うございます!!!!
出来ました!!!
本当に有難う御座いました!!!!

2013年3月27日水曜日 20時37分36秒 UTC+9 titoi2:
こんばんは
取れたLinearLayout に対してgetChildxxx をしてみてはいかがでしょうか。



2013年3月27日水曜日 19時56分01秒 UTC+9 TOsada:
> このグループに投稿するには、android-group-jap...@googlegroups.com にメールを送信してください。
> http://groups.google.com/group/android-group-japan?hl=ja
> からこのグループにアクセスしてください。
> その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。
>
>
Reply all
Reply to author
Forward
0 new messages