0

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

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

        //...   
    }

    //...   
}

我的自定义适配器

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中可行吗?

谢谢!

4

2 回答 2

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调用都需要设置标签。

As a sidenote: you should use a Holder pattern so you don't have to call findViewById (which is costly) in getGroupView.

于 2014-10-06T17:23:00.467 回答
0

First of all you should use the ViewHolder pattern. Why don't you add an OnCheckedChangeListener to your Switch if you inflate your layout? It is possible that you should set the switch unfocusable, in order to still be able to click your list items.

于 2014-10-06T17:24:43.403 回答