2014-10-06 60 views
0

我有一个可扩展列表视图。我使用自定义适配器来加载我自己的.xml布局。每组有一个开关。我不知道会显示多少组。现在我想知道如何动态地向每个交换机添加一个监听器,以便对变化做出反应。如何将监听器添加到ExpandableListView中的动态视图中?

ExpandableListView活动:

public class ExpandableListView extends Activity { 

    private MyCustomAdapter adapter; 
    private ExpandableListView listView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.detail_layout); 

     initViews();   
    } 

    private void initViews() { 

     listView = (ExpandableListView) findViewById(R.id.foo); 
     adapter = new MyCustomAdapter(this, data); // my custom adapter 
     listView.setAdapter(adapter); 

     //... 
    } 

    //... 
} 

MyCustomAdapter

public class MyCustomAdapter extends BaseExpandableListAdapter { 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 

     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); // load layout with switch 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.myTextView; 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

    // ... 
} 

list_group.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="8dp" 
    android:background="#000000"> 


    <TextView 
     android:id="@+id/myTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:paddingLeft="20dp" 
     android:paddingRight="0dp" 
     android:paddingEnd="0dp" 
     android:paddingStart="20dp" 
     android:textSize="17sp" /> 


    <Switch 
     android:id="@+id/mySwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:focusable="false" 
     android:checked="true" 
    /> 


</RelativeLayout> 

如何将侦听器添加到每个“mySwitch”?这可能在ExpandableListView

谢谢!

回答

0

在充气组布局时添加监听器。您可以通过设置与该组位置对应的标签来区分哪个组的开关被点击。

下面是一个例子:

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     ... 

     // Add a switch listener 
     Switch mySwitch = (Switch) convertView.findViewById(R.id.mySwitch); 
     mySwitch.setTag(groupPosition); 
     mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       Log.e("TAG", "Clicked position: " + buttonView.getTag()); 
      } 
     }); 
    } else { 
     convertView.findViewById(R.id.mySwitch).setTag(groupPosition); 
    } 

    ... 

    return convertView; 
} 

请注意,您需要设置标签每次getGroupView被调用。

作为旁注:您应该使用Holder模式,因此您不必在getGroupView中拨打findViewById(这很贵)。

+0

感谢这个非常简单的解决方案。我认为在'getGroupView'中设置监听器可能是一个问题,因为这个方法一遍又一遍地被调用。 – null 2014-10-07 06:49:27

+1

@null感谢Android开发团队的convertViews :) – Simas 2014-10-07 07:49:00

0

首先你应该使用ViewHolder模式。 如果膨胀布局,为什么不向On Switch添加OnCheckedChangeListener?您可能应该将开关设置为不可对焦,以便仍然可以点击您的列表项目。

+0

是的,我会改变我的代码。你是对的,因为这个开关确实不能确定。 – null 2014-10-07 06:50:48

相关问题