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