显示cab的自定义适配器中的android复选框

时间:2014-04-29 10:30:34

标签: android checkbox android-adapter

我尝试使用自定义适配器实现上下文操作栏(cab)。 首先,我用cab制作了简单的ListView,它起作用了。 其次,我尝试使自定义适配器包含CheckBoxTextViewCheckBox用于驾驶室,但它不起作用。

public class MainActivity extends Activity {

ListView lv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<String> arr = new ArrayList<String>();
    arr.add("1");
    arr.add("2");
    arr.add("3");
    arr.add("4");
    arr.add("5");
    lv1 = (ListView)findViewById(R.id.listView1);
    lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv1.setAdapter(new Adapters(this, R.layout.item, arr));
    lv1.setMultiChoiceModeListener(new ModeCallback());
}

private class Adapters extends ArrayAdapter<String> implements OnCheckedChangeListener{

    Context context;
    ArrayList<String> arr;
    CheckBox cb;
    int position;
    ViewGroup parent;

    public Adapters(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
        this.context = context;
        this.arr = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = null;
        if(v == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item, null);
        }
        TextView tv = (TextView)v.findViewById(R.id.tv1);
        tv.setText(arr.get(position));
        cb = (CheckBox)v.findViewById(R.id.cb1);
        cb.setOnCheckedChangeListener(this);
        this.parent = parent;
        this.position = position;
        return v;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        ((ListView)parent).setItemChecked(position, true);
    }

}

private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        mode.setTitle("Select Items");
        setSubtitle(mode);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        setSubtitle(mode);
    }

    private void setSubtitle(ActionMode mode) {
        final int checkedCount = lv1.getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setSubtitle(null);
                break;
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + checkedCount + " items selected");
                break;
        }
    }
}

问题未被更改CheckBox。 我真的想知道这个, 找不到任何类似的问题。

1 个答案:

答案 0 :(得分:1)

问题是,实际上,添加可聚焦元素(例如 CheckBox 按钮)会禁用点击整体列表项的功能他们的专注力必须被禁用。您不应该向主布局添加任何可点击标记。

将x 中的复选框设置为可聚焦为false

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:focusable="false"/>