苦労

2011-09-09 17 views
0

I次の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> 

をそして私はしかしループにより、 "TITLE2"、および "TITLE3" "TITLE1" TextViewsのクリックリスナーを設定したいと思いますそして、それに対応するビューの子を見つける:

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; 
} 

のonClickが発射されていない、と私はwrapperChildがfindViewById(R.id.title1)に等しいことはありませんことに気づいたいくつかの理由。何か案は?

+0

なぜこれをやっていますか?なぜあなたは 'findViewById(...)'を使わないのですか? – Squonk

+0

@MisterSquonk:可能な限り多くのビューが存在する可能性があるため、findViewByIdを何回も入力しなくてもオンクリックハンドラを動的に設定したいと考えています。 – Phillip

+0

回答:http://stackoverflow.com/questions/7394560/why-dont-inflated-views-respond-to-click-listeners – Phillip

答えて

0

私は2例と考えることができます:

  • まず、あなたはダイナミックなレイアウトを作成します。この場合、TextViewを作成するときに、必要なOnClickListenerを設定して準備ができます。

  • 次に特定のIDを持つことができ、findViewByIdによるビューへのアクセス、または特定のリスナーをandroid:onclickを介してXMLに直接設定することができます。いずれの場合においても

は、代わりにこの wrapperChild.setOnClickListener(this);のこれを試してください: wrapperChild.setOnClickListener(new OnClickListerer(){ // Code here});。お役に立てれば!

+0

あなたにとって有益でしたか? –