나는 다음과 같은 XML 레이아웃이 있습니다갖는 문제
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:background="#ffffff" android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_gravity="center">
<TextView android:text="Title1" android:id="@+id/title1"
android:padding="5dip" android:background="#005555"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/panel1"
android:visibility="gone" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_margin="2dip" android:text="Item1"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item2"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item3"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_marginTop="2dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_gravity="center">
<TextView android:text="Title2" android:id="@+id/title2"
android:padding="5dip" android:background="#005555"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/panel2"
android:visibility="gone" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_margin="2dip" android:text="Item1"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item2"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item3"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_marginTop="2dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_gravity="center">
<TextView android:text="Title3" android:id="@+id/title3"
android:padding="5dip" android:background="#005555"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/panel3"
android:visibility="gone" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_margin="2dip" android:text="Item1"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item2"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_margin="2dip" android:text="Item3"
android:padding="5dip" android:background="#777777"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
을 그리고 비록 반복에 의해, "제목 2"및 "TITLE3" "TITLE1"TextViews에 대한 클릭 리스너를 설정하려는 onclick을 해고되지 않는 몇 가지 이유를 들어
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MyActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPanelWrappers = new ArrayList<LinearLayout>();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup rootLayout = (ViewGroup)inflater.inflate(R.layout.main, null);
for(int i = 0; i < rootLayout.getChildCount(); i++) {
View rootChild = rootLayout.getChildAt(i);
if(rootChild instanceof LinearLayout) {
LinearLayout panelWrapper = (LinearLayout)rootChild;
mPanelWrappers.add(panelWrapper);
for(int j = 0; j < panelWrapper.getChildCount(); j++) {
View wrapperChild = panelWrapper.getChildAt(j);
if(wrapperChild instanceof TextView) {
Log.d(TAG, "Setting on-click listener for " + wrapperChild.getId());
if(wrapperChild == findViewById(R.id.title1)) {
Log.d(TAG, "Found title1");
} else {
Log.d(TAG, "Not title1");
}
wrapperChild.setOnClickListener(this);
}
break;
}
}
}
}
@Override
public void onClick(View panelTitle) {
Log.d(TAG, "On click");
}
private ArrayList<LinearLayout> mPanelWrappers;
}
, 나는 wrapperChild이 같이 findViewById (R.id.title1)에 결코 같다는 것을 깨달았다와 해당보기 아이를 발견. 어떤 아이디어?
왜이 작업을 수행하고 있습니까? 왜 당신은'findViewById (...)'를 사용하지 않는가? – Squonk
@MisterSquonk : 가능한 많은 뷰에 대해 findViewById를 입력하지 않고 클릭 할 핸들러를 동적으로 설정하려고합니다. 잠재적으로 많이 발생할 수 있습니다. – Phillip
답변 : http://stackoverflow.com/questions/7394560/why-dont-inflated-views-respond-to-click-listeners – Phillip