NotifyDataSetChanged在ExpandableListView中不起作用

时间:2013-03-06 15:42:05

标签: android listview checkbox invalidation notifydatasetchanged

这是我的可扩展列表视图:

public class SnatchView extends ExpandableListView {
public SnatchAdt m_adt;

public FileBrowser fileBrowser;

public SnatchView(Context context) {
    super(context);
    m_adt = new SnatchAdt(context, this);
}

public SnatchView(Context context, AttributeSet attrs) {
    super(context);
    m_adt = new SnatchAdt(context, this);
}

public void start() {
    setAdapter(m_adt);
    m_adt.start();
    setBackgroundColor(SnatchAdt.clr_back);
    setGroupIndicator(null);// getResources().getDrawable(R.drawable.folder_icon)
}

public void edit() {
    m_adt.notifyDataSetChanged();
    m_adt.notifyDataSetInvalidated();
}

public String getChildPath(int group, int child) {
    return m_adt.getChildPath(group, child);
}

public boolean getChildDelete(int group, int child) {
    return m_adt.getChildDelete(group, child);
}

public void close() {
    m_adt.close();
}
}

这是我的适配器中更令人讨厌的部分:

public class SnatchAdt extends BaseExpandableListAdapter implements ExpandableListAdapter {
private final String TAG = SnatchAdt.class.getSimpleName();

protected static int clr_back = 0xff356AA0;// 0xFFCCCCCC;

protected int clr_text = 0xffffffff;// 0xFF000044;

private SnatchView snatchView;

public class SnatchItem {
    public String m_path;

    public String m_name;

    public boolean toDelete = false;

    LinearLayout m_view;
}

public class SnatchGroup {
    private Vector<SnatchItem> m_items = new Vector<SnatchItem>();

    public SnatchItem get(int index) {
        return m_items.get(index);
    }

    public int get_count() {
        return m_items.size();
    }

    public void add_item(String path, String name, boolean toDelete) {

        final SnatchItem item = new SnatchItem();
        item.m_name = name;
        item.m_path = path;
        item.toDelete = toDelete;
        item.m_view = new LinearLayout(m_context);
        Bitmap myBitmap = BitmapFactory.decodeResource(m_context.getResources(), R.drawable.file_icon);

        ImageView v = new ImageView(m_context);
        v.setImageBitmap(myBitmap);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(50, 0, 0, 0);
        v.setLayoutParams(lp);
        final CheckBox cb = new CheckBox(m_context);
        cb.setPadding(10, 10, 10, 10);
        cb.setFocusable(false);
        cb.setFocusableInTouchMode(false);

        cb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (cb.isChecked()) {
                    item.toDelete = true;
                    snatchView.smoothScrollBy(1, 1);
                } else {
                    item.toDelete = false;
                    snatchView.smoothScrollBy(1, 1);
                }
                // snatchView.invalidateViews();
                // notifyDataSetChanged();
                snatchView.edit();
                // snatchView.post(new Runnable() {
                // @Override
                // public void run() {
                // notifyDataSetChanged();
                // }
                // });
            }
        });

        TextView view = new TextView(m_context);
        view.setText(name);
        view.setBackgroundColor(clr_back);
        view.setTextSize(18);
        view.setTextColor(clr_text);
        view.setPadding(20, 0, 0, 0);
        item.m_view.addView(cb);
        item.m_view.addView(v);
        item.m_view.addView(view);
        item.m_view.setPadding(12, 20, 0, 20);
        item.m_view.setBackgroundColor(clr_back);
        m_items.add(item);
        item.m_view.setId(clr_back);
    }

    String m_path;

    LinearLayout m_view;
}

private Vector<SnatchGroup> m_groups = new Vector<SnatchGroup>();

private DataSetObserver m_obs;

private Context m_context;

private boolean canceling = false;

private Handler m_hand_ui = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (m_groups != null) {
            m_groups.add((SnatchGroup) msg.obj);
        }
        if (m_obs != null) {
            m_obs.onChanged();
        }
        super.handleMessage(msg);
    }
};

private Thread m_thread = new Thread() {
    @Override
    public void run() {
        set_group_root();
    }
};

public SnatchAdt(Context ctx, SnatchView snatchView) {
    m_context = ctx;
    this.snatchView = snatchView;
}

private synchronized Object get_child(int groupPosition, int childPosition) {
    return m_groups.get(groupPosition).get(childPosition);
}

private synchronized String get_child_path(int groupPosition, int childPosition) {
    return m_groups.get(groupPosition).get(childPosition).m_path;
}
}

现在它只有在我移动listview之后才会失效。除非我移动listview,否则我调用snatchView.edit();的代码也不会被调用。有没有人有一个ideea如何使代码工作? 我也试过调用invalidate,notifyDataSetChanged,notifyDataSetInvalidated,在runOnUIThread中调用notifyDataSetChange,或者单独的线程,但仍然没有运气。

1 个答案:

答案 0 :(得分:-3)

我已将Expandable ListView更改为普通ListView,并在复选框上将焦点和setEnabled设置为false