CheckableRelativeLayout上的默认选中复选框

时间:2016-08-09 19:19:43

标签: android

我需要一些帮助,我不知道如何解决这个问题。

当我设置适配器时,我需要在默认情况下选择几个复选框,但我不能这样做。为什么?我认为问题是CheckableRelativeLayout。我使用RelativeLayout的这个变种,因为我需要点击行并选择复选框。

当我使用RelativeLayout的这个变体时,列表中的所有复选框都是未选中的。如果你看到我的日志到底,当选中复选框并且我不知道原因时会触发事件“onCheckedChanged”。

请帮助我一点,谢谢。

这是我的活动代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        items = new ArrayList<Item>();

        items.add(new Item("A1", false));
        items.add(new Item("A2", true));

        listView = (ListView) findViewById(R.id.listView);

        adapter = new Adapter(this, items);
        listView.setAdapter(adapter);


}

这是我的物品代码:

public class Item {

    private String title;
    private Boolean checked;

    public Item(String title, Boolean checked) {
        this.title = title;
        this.checked = checked;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }
}

My CheckableRelativeLayout

    public class CheckableRelativeLayout extends RelativeLayout implements Checkable {


private boolean isChecked;
private List<Checkable> checkableViews;

public CheckableRelativeLayout(Context context, AttributeSet attrs,
                               int defStyle) {
    super(context, attrs, defStyle);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, int checkableId) {
    super(context);
    initialise(null);
}

/*
  * @see android.widget.Checkable#isChecked()
  */
public boolean isChecked() {
    return isChecked;
}

/*
  * @see android.widget.Checkable#setChecked(boolean)
  */
public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
    for (Checkable c : checkableViews) {
        c.setChecked(isChecked);
    }
}

/*
  * @see android.widget.Checkable#toggle()
  */
public void toggle() {
    this.isChecked = !this.isChecked;
    for (Checkable c : checkableViews) {
        c.toggle();
    }
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    final int childCount = this.getChildCount();
    for (int i = 0; i < childCount; ++i) {
        findCheckableChildren(this.getChildAt(i));
    }
}

/**
 * Read the custom XML attributes
 */
private void initialise(AttributeSet attrs) {
    this.isChecked = false;
    this.checkableViews = new ArrayList<Checkable>(5);
}

/**
 * Add to our checkable list all the children of the view that implement the
 * interface Checkable
 */
private void findCheckableChildren(View v) {
    if (v instanceof Checkable) {
        this.checkableViews.add((Checkable) v);
    }

    if (v instanceof ViewGroup) {
        final ViewGroup vg = (ViewGroup) v;
        final int childCount = vg.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(vg.getChildAt(i));
        }
    }
}

}

最后我的适配器代码:

    public class Adapter extends BaseAdapter {

public static String TAG = "Adapter";

private Context context;
private List<Item> items;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

public Adapter(Context context, List<Item> items) {

    this.context = context;
    this.items = items;

    Log.d(TAG, "Constructor");

    // Initizalice the states.
    for (int i = 0; i < this.getCount(); i++) {
        Log.d(TAG, "ItemChecked ==> " + items.get(i).getChecked());
        itemChecked.add(i, items.get(i).getChecked());
    }

}

@Override
public int getCount() {
    return this.items.size();
}

@Override
public Object getItem(int position) {
    return this.items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

// View Holder
static class ViewHolder {
    TextView title;
    CheckBox checkbox;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Log.d(TAG, "getView -------------------- position ==> " + position);

    ViewHolder viewHolder;

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.title = (TextView) convertView.findViewById(R.id.listView_text);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.myCheckBox);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Item item = items.get(position);


    Log.d(TAG, "isChecked ------------------- ==> " + itemChecked.get(position));
    viewHolder.title.setText(item.getTitle());
    viewHolder.checkbox.setChecked(itemChecked.get(position));

    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            itemChecked.set(position, isChecked);
            Log.d(TAG, "onCheckedChanged: isChecked ------------------- ==> " + isChecked);
            Log.d(TAG, "onCheckedChanged: Position ------------------- ==> " + position);
        }
    });

    return convertView;

}

}

输出日志为:

Constructor

ItemChecked ==> false

ItemChecked ==> true

getView -------------------- position ==> 0

isChecked ------------------- ==> false

getView -------------------- position ==> 1

isChecked ------------------- ==> true

onCheckedChanged: isChecked ------------------- ==> false (Why ??)

onCheckedChanged: Position ------------------- ==> 1

1 个答案:

答案 0 :(得分:0)

最后我找到答案......从这里复制:checked-multi-choice-listviews

有效。我有一个列表视图,默认情况下激活了几个复选框,当我向下/向上滚动状态时,状态不会丢失。

我的活动代码:

    adapter = new Adapter(this, items);
    listView.setAdapter(adapter);

    // !! After set the adapter i check the default checkbox.
    for (int i = 0; i < items.size(); i++) {
        listView.setItemChecked(i, items.get(i).getChecked());
    }

我的适配器代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.activity_async_select_item, parent, false);
    }

    CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.checkedTextView1);
    final Item item = items.get(position);
    checkedTextView.setText(item.getTitle());

    return convertView;

}