1

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

我有一个切换按钮,当我打开一个切换按钮时,在下面滚动时,我打开了多个切换按钮。为什么会发生这种情况,我在日志中正确获取了 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 个项目,那么每次我们应该使用支架时都绘制视图不是一个好主意。

4

2 回答 2

0

这是由于视图回收机制而发生的。考虑到 ListView,我写了一篇博客文章。

如果您不了解视图回收,那么您可能必须阅读此博客以准确了解视图回收的工作原理!

于 2013-08-02T09:43:17.600 回答
0

删除持有者并关闭 if 循环进行膨胀,就像我关闭的方式一样,也使用逻辑在 getGroupView 内切换。因为每次滚动列表视图时都会调用 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;
    }
于 2013-08-02T09:42:40.583 回答