0

我一直在玩ExpandableListView很多,我無法弄清楚爲按鈕添加按鈕偵聽器,這將是視圖中的孩子。我設法得到了一個按鈕偵聽器,它使用下面的getChildView(),但它似乎是所有按鈕的同一個偵聽器。Androids ExpandableListView - 在哪裏把按鈕監聽器是兒童的按鈕

最好的情況是,我可以在實例化ExpandableListAdapter類的類中實現按鈕偵聽器,而不必將實際的ExpandableListAdapter類中的偵聽器。在這一點上,我甚至不知道這是可能的

我一直在嘗試本教程/代碼:HERE

getChildView()

@Override 
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1) 
{ 
    ChildHolder childHolder; 
    if (view1 == null) 
    { 

     view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null); 

     childHolder = new ChildHolder(); 

     childHolder.section_btn = (Button)view1.findViewById(R.id.item_title); 
     view1.setTag(childHolder); 
     childHolder.section_btn.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show(); 

      } 
     }); 



    }else { 
     childHolder = (ChildHolder) view1.getTag(); 
    } 
     childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section); 
     Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF"); 

     childHolder.section_btn.setTypeface(tf); 
     return view1; 
} 

任何幫助將不勝感激。謝謝,我會站在旁邊。

回答

2

如果按鈕位於ExpandableListView中,它們的偵聽器需要位於適配器中。

我不確定你的問題的主旨,但如果你問你如何將按鈕與子行的內容聯繫起來,我可以回答。 :p

爲了演示目的,我假設一個簡單的子行佈局。

child_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:orientation="horizontal" > 
<TextView 
    android:id="@+id/ListItem1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="left|center_vertical" 
    android:paddingLeft="7dip" 
    android:paddingRight="7dip" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
<TextView 
    android:id="@+id/ListItem2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="right|center_vertical" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
<Button 
    android:id="@+id/button" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:layout_width="50dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

然後,爲了獲得當按下按鈕所處的行的內容,您可以使用該按鈕回溯到父vieew,然後獲取必要的孩子的看法和他們的內容:

childHolder.section_btn.setOnClickListener(new OnClickListener()  
    {  
     @Override  
     public void onClick(View v)  
     {  
      LinearLayout ll = (LinearLayout) v.getParent(); // get the view containing the button 
      TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1); // get the reference to the first widget 
      TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2); // get the reference to the second widget 
      String text1 = tv1.getText.toString(); // Get the contents of the first widget to a string 
      String text2 = tv2.getText.toString(); // Get the contents of the second widget to a string 
     }  
    }); 

如果這不是你想要澄清你的問題,我會再試一次。

+0

非常感謝你和偉大的想法從實際的部件中獲取字符串,不知道爲什麼我沒有想到這一點。 – CommonKnowledge 2012-07-06 20:35:02