【问题标题】:Why don't active contextual action bar when check the checkbox on the listview?选中列表视图上的复选框时,为什么不激活上下文操作栏?
【发布时间】:2015-12-27 17:53:00
【问题描述】:

我想在列表视图中选中复选框时显示自定义操作栏。我这样写了一种 xml 文件类型的菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/delete"
        android:title="actionbar"
        android:icon="@drawable/recycle"/>

</menu>

我用于自定义列表视图此代码:

private class Adapter_collection extends ArrayAdapter<String> {

public Adapter_collection(Context context, int resource, int textViewResourceId,
        String[] name_collection_tbl_collection) {
    super(context, resource, textViewResourceId, name_collection_tbl_collection);
}

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

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.listview_items, parent, false);
    TextView txt_item_list_collection = (TextView)row.findViewById(R.id.textView_Item_Listview);
    CheckBox checkBox=(CheckBox)row.findViewById(R.id.checkBox_Item_Listview);

    checkBox.setTag(position);
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {         
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked ) {   
            if (mActionmode!=null) {
                MyActionModeCallBack callBack = new MyActionModeCallBack();
                mActionmode = startActionMode(callBack);
            }

                //Toast.makeText(getApplicationContext(), buttonView.getTag().toString() , Toast.LENGTH_SHORT).show();      
        }
    });
    txt_item_list_collection.setText(name_collection_tbl_collection[position]);
    return row;
}

}

我添加这个类以使用上下文操作栏:

    private class MyActionModeCallBack implements ActionMode.Callback {

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.actionbar_menu, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionmode = null;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        return false;
    }
}

现在,当我选中 listview 中的复选框时,操作栏不会改变。请指教。

【问题讨论】:

  • 你调试过代码有没有进入这个块 checkBox.setOnCheckedChangeListener(){} ?
  • 我现在调试:mActionmode 不为空,因此不要进入 if 语句。我删除了if语句并且工作了。但是xml文件中存在的回收图标不显示。
  • mActionmode 不是 null 还是 null do clear ?
  • 我使用 mActionmode = null;仅在 onDestroyActionMode 方法中。
  • 我想在这里问 mActionmode 是 null 还是 not null if (mActionmode!=null) {...}

标签: android checkbox android-listview


【解决方案1】:

这个问题通过这个改变解决了:

 checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {         
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked ) {   

            MyActionModeCallBack callBack = new MyActionModeCallBack();
            mActionmode = startActionMode(callBack);


            //Toast.makeText(getApplicationContext(), buttonView.getTag().toString() , Toast.LENGTH_SHORT).show();      
    }
});

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多