在单击一个toggleButton时,ListView内部多个toggleButton正在启用

时间:2013-08-02 09:30:07

标签: android expandablelistview

我有一个 ExpandableListView 并在每个Parent Row中。

我有一个ToggleButton,当我打开一个切换按钮时,在滚动下面我正在打开多个toggleButton。为什么会发生这种情况,我在日志中正确地获得了groupPosition一切?

这是我的代码:

public class WifiAPListAdapter extends BaseExpandableListAdapter {

    private WifiAPListActivity context;
    public ArrayList<ExpandListGroup> groups;
    private WifiManager wifiManager;
    WifiStatus checkWifiStatus;
    public ToggleButton togglebtn;
    int pos;
    Holder holder;
    int previousPosition = -1;
    private static final int CHILD_COUNT = 1;

    public WifiAPListAdapter(WifiAPListActivity context,
            ArrayList<ExpandListGroup> result) {
        this.context = context;
        this.groups = result;
        wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        checkWifiStatus = new WifiStatus(context);

    }

    public Object getChild(int groupPosition, int childPosition) {

        return groups.get(groupPosition);

    }

    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View view, ViewGroup parent) {
        previousPosition = groupPosition;
        ExpandListGroup child = (ExpandListGroup) getChild(groupPosition,
                groupPosition);
        // Log.i("", "getChildView: "+childPosition);
        if (view == null) {
            holder = new Holder();
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.list_child_item, null);
            holder.label_ssid = (TextView) view.findViewById(R.id.label_ssid);
            holder.tv_ssid = (TextView) view.findViewById(R.id.tvssid);
            holder.label_bssid = (TextView) view.findViewById(R.id.label_bssid);
            holder.tv_bssid = (TextView) view.findViewById(R.id.tvbssid);
            holder.label_capabilities = (TextView) view
                    .findViewById(R.id.label_capabilities);
            holder.tv_capabilities = (TextView) view
                    .findViewById(R.id.tvcapabilities);
            holder.label_signalStrength = (TextView) view
                    .findViewById(R.id.label_signalstrength);
            holder.tv_signalStrength = (TextView) view
                    .findViewById(R.id.tvsignalstrength);
            holder.wifi_signalimg = (ImageView) view.findViewById(R.id.signal);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();

        }

        // *********************setting data to the child list**************

        holder.tv_ssid.setText(child.getSSID());
        holder.tv_bssid.setText(child.getBSSID());
        holder.tv_capabilities.setText(child.getCapabilities());
        holder.tv_signalStrength.setText(child.getData());

        Bitmap bitmap = setSignalImage(Integer.parseInt(child.getData()));
        holder.wifi_signalimg.setImageBitmap(bitmap);

        return view;
    }

    public int getChildrenCount(int groupPosition) {
        Log.i("", "getChildrenCount: " + groupPosition);

        return CHILD_COUNT;

    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        Log.i("", "getGroupCount: " + groups.size());
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isLastChild, View view,
            ViewGroup parent) {

        Log.i("", "getGroupView: " + groupPosition);
        if (view == null) {
            holder = new Holder();
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inf.inflate(R.layout.list_parent_items, null);

            holder.tv_ssid = (TextView) view.findViewById(R.id.ssid);
            holder.tv_bssid = (TextView) view.findViewById(R.id.bssid);
            holder.btn_toggle = (ToggleButton) view.findViewById(R.id.togBtn);

            // holder.btn_toggle.setTag(groups.get(groupPosition).getBSSID());

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        // holder.btn_toggle.setTag(groups.get(groupPosition).getBSSID());
        holder.tv_ssid.setText(groups.get(groupPosition).getSSID());
        holder.tv_bssid.setText(groups.get(groupPosition).getBSSID());

        return view;
    }

    public void toggle(View v) {
        if (holder.btn_toggle.isChecked()) {
        }
        Log.i("", "toggle");
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

任何身体都可以告诉一个好的方法来避免这个问题而不去除持有者,因为如果我有n个项目,那么每次我们应该使用持有者时绘制视图并不是一个好主意。

2 个答案:

答案 0 :(得分:0)

删除持有者并关闭if循环如何关闭以及如何关闭也使用逻辑来切换getGroupView内部。因为每次你滚动listview getGroupView被调用所以每次这个方法必须检查条件。

 private WifiAPListActivity context;
    public ArrayList<ExpandListGroup> groups;
    private WifiManager wifiManager;
    WifiStatus checkWifiStatus;
    public ToggleButton togglebtn;
    int pos;
    Holder holder;
    int previousPosition = -1;
    private static final int CHILD_COUNT = 1;

    public WifiAPListAdapter(WifiAPListActivity context,
            ArrayList<ExpandListGroup> result) {
        this.context = context;
        this.groups = result;
        wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        checkWifiStatus = new WifiStatus(context);

    }

    public Object getChild(int groupPosition, int childPosition) {

        return groups.get(groupPosition);

    }

    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View view, ViewGroup parent) {
        previousPosition = groupPosition;
        ExpandListGroup child = (ExpandListGroup) getChild(groupPosition,
                groupPosition);
        // Log.i("", "getChildView: "+childPosition);
        if (view == null) {
            holder = new Holder();
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.list_child_item, null);
            }
           label_ssid = (TextView) view.findViewById(R.id.label_ssid);
           tv_ssid = (TextView) view.findViewById(R.id.tvssid);
            label_bssid = (TextView) view.findViewById(R.id.label_bssid);
            tv_bssid = (TextView) view.findViewById(R.id.tvbssid);
            label_capabilities = (TextView) view
                    .findViewById(R.id.label_capabilities);
            tv_capabilities = (TextView) view
                    .findViewById(R.id.tvcapabilities);
            label_signalStrength = (TextView) view
                    .findViewById(R.id.label_signalstrength);
            tv_signalStrength = (TextView) view
                    .findViewById(R.id.tvsignalstrength);
            wifi_signalimg = (ImageView) view.findViewById(R.id.signal);



        // *********************setting data to the child list**************

        tv_ssid.setText(child.getSSID());
        tv_bssid.setText(child.getBSSID());
        tv_capabilities.setText(child.getCapabilities());
        tv_signalStrength.setText(child.getData());

        Bitmap bitmap = setSignalImage(Integer.parseInt(child.getData()));
        wifi_signalimg.setImageBitmap(bitmap);

        return view;
    }

    public int getChildrenCount(int groupPosition) {
        Log.i("", "getChildrenCount: " + groupPosition);

        return CHILD_COUNT;

    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        Log.i("", "getGroupCount: " + groups.size());
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isLastChild, View view,
            ViewGroup parent) {

        Log.i("", "getGroupView: " + groupPosition);
        if (view == null) {
            holder = new Holder();
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inf.inflate(R.layout.list_parent_items, null);
      }

            tv_ssid = (TextView) view.findViewById(R.id.ssid);
            tv_bssid = (TextView) view.findViewById(R.id.bssid);
            btn_toggle = (ToggleButton) view.findViewById(R.id.togBtn);

            // holder.btn_toggle.setTag(groups.get(groupPosition).getBSSID());



        // holder.btn_toggle.setTag(groups.get(groupPosition).getBSSID());
        tv_ssid.setText(groups.get(groupPosition).getSSID());
        tv_bssid.setText(groups.get(groupPosition).getBSSID());

        return view;
    }

    public void toggle(View v) {
        if (btn_toggle.isChecked()) {
        }
        Log.i("", "toggle");
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

答案 1 :(得分:0)

由于回收视图的机制,这种情况正在发生。考虑到ListView,我写了blog post同样的内容。

如果您不了解视图的回收,那么您可能必须阅读this blog以了解视图回收的确切方法!