// //

inflate使用で呼び出したレイアウト内Viewに対するfindViewById


レイアウトのテンプレートを作っておきボタン操作により、
Activityのとあるレイアウト内で切り替え表示ができるようにしたいため、
表示用xmlにテンプレートxmlをincludeした上でInflateを使用しその機能を実装。
(最初にViewFlipperを利用し絶望しかけた…)

このままだとテンプレートxmlのまま各Viewが表示されるのでfindViewByIdで参照を得て、
それぞれにsetText()なりしようとしたら詰んだ。


流れとして
template1.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
	<LinearLayout
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="vertical" > 
	    
	    <TextView
	        android:id="@+id/C"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="none" />
    </LinearLayout>
</merge>


A.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical" >
    
	<include android:id="@+id/B" layout="@layout/template1" />

</LinearLayout>


activity.java

省略
@Override
public void onClick(View view){
	switch(view.getId){
	case R.id.xxxBtn:
		layout.removeAllViews();
		getLayoutInflater().Inflate(R.layout.A, layout);
        
		LinearLayout linear = (LiearLayout)findViewById(R.id.B);
		TextView text = (TextView)linear.findViewById(R.id.C);
		text.setText("change");
		break;
	}
}


上記内容でActivity.javaのLinearLayout linear = (LiearLayout)findViewById(R.id.B);がnull
「include関連で調べた内容と違いがないのになんでできないのか…」と
しばらくの現実逃避の後、頭を捻らせた結果

	switch(view.getId){
	case R.id.xxxBtn:
		layout.removeAllViews();
		View inf = getLayoutInflater().Inflate(R.layout.A, layout);
        
		TextView text = (TextView)inf.findViewById(R.id.C);
		text.setText("change");
	}


これで解決。
Inflate部分に変数を持たせてLinearLayoutを中継せずそのまま対象の参照を取ればいい?
Inflateを利用したことで表示されるようになったから参照できると思ったらそうはいかないみたい。
Inflateを利用するしないでincludeしたものの変更方法に違いが出たという結果…であっているのかな?