选择项目后,Android上下文操作栏不会出现

时间:2014-06-23 05:11:41

标签: android-layout android-fragments action android-context

在我的Android应用程序中,我试图在列表视图中显示上下文操作栏(CAB)以删除项目。问题是每当按下一个项目很长时,该项目就会被选中,但CAB不会出现。我已经验证了这么多教程,无法弄清楚我错过了什么。任何帮助,将不胜感激。

适配器类

public class DuasListAdapter extends ArrayAdapter<String>{

    // class variables
    private final Context context;// to save context
    private final List<String> duas;// to save list of stores
    LayoutInflater inflater;// 
    private SparseBooleanArray selectedItemsIds;

    public DuasListAdapter(Context ctx,int resourceId, List<String> duasList) {

        super(ctx, resourceId, duasList);
        selectedItemsIds = new SparseBooleanArray();
        context = ctx;// save context
        duas = duasList;// save list of stores
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// save inflater layout

    }

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

    final   ViewHolder holder;

    if(convertView==null){


        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.adapter_list_duas, null);
        convertView.setTag(holder);
        holder.duaIndex = position;
        holder.background = (LinearLayout) convertView.findViewById(R.id.ll_duas_list);
        holder.iv_duatype = (ImageView) convertView.findViewById(R.id.iv_duatype);
        holder.tv_dua_arabic = (TextView) convertView.findViewById(R.id.tv_dua_arabic);
        holder.tv_dua_no = (TextView) convertView.findViewById(R.id.dua_no);


    }

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

    return convertView;




    }


static class ViewHolder {

         int duaIndex;
        LinearLayout background;// background to display color for read and unread messages
        ImageView iv_duatype;
        TextView tv_dua_arabic;// title of message
        TextView tv_dua_ref;// message by and message created on
        TextView tv_dua_no;

        /**
         * @description constructor to save context and list of stores views
         * @param v to refer the view
         * @return
         */

    }



    public void remove(List<String> object){

        duas.remove(object);
        notifyDataSetChanged();
    }

public List<String> getDuas(){

    return duas;
    }

public void toggleSelection(int position) {
    selectView(position, !selectedItemsIds.get(position));
}

public void removeSelection() {
    selectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public void selectView(int position, boolean value) {
    if (value)
        selectedItemsIds.put(position, value);
    else
        selectedItemsIds.delete(position);
    notifyDataSetChanged();
}

public int getSelectedCount() {
    return selectedItemsIds.size();
}

public SparseBooleanArray getSelectedIds() {
    return selectedItemsIds;
}



}

片段类

    Context context;
    LinearLayout ll_back_duas_list_header;
    TextView ll_back_duas_list_header_title;
    ListView lvDuaas;
public static   DuasListAdapter duasAdapter;
ActionMode mAction;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = inflater.getContext();
        View view = inflater.inflate(R.layout.fragment_list_duas, container, false);
        ll_back_duas_list_header = (LinearLayout) view.findViewById(R.id.ll_back_duas_list_header);
        ll_back_duas_list_header_title = (TextView) view.findViewById(R.id.ll_back_duas_list_header_title);
        lvDuaas = (ListView) view.findViewById(R.id.lv_duas);
        setHasOptionsMenu(true);
        return view;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {


        String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
        String[] versesList = verses.split(",");
        final ArrayList<String> duas = new ArrayList<String>();
        for (int i = 0; i < versesList.length; i++) {
            if (versesList[i].length() > 0)
                duas.add(versesList[i]);
        }

        duasAdapter = new DuasListAdapter(context,R.layout.adapter_list_duas,duas);
        lvDuaas.setAdapter(duasAdapter);


        lvDuaas.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub

                    lvDuaas.setChoiceMode(lvDuaas.CHOICE_MODE_MULTIPLE);
                    lvDuaas.setItemChecked(position, true);

                mAction = getActivity().startActionMode(modeCallBack);
                view.setSelected(true);
                    return true;
                }
            });
super.onActivityCreated(savedInstanceState);
    }


ActionMode.Callback modeCallBack = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
             mode.getMenuInflater().inflate(R.menu.activity_main, menu);
            return false;
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode mode,
                MenuItem item) {
            // TODO Auto-generated method stub
             switch (item.getItemId()) {
                case R.id.delete:
                    // Calls getSelectedIds method from ListViewAdapter Class
                    SparseBooleanArray selected = duasAdapter
                            .getSelectedIds();
                    // Captures all selected ids with a loop
                    for (int i = (selected.size() - 1); i >= 0; i--) {
                        if (selected.valueAt(i)) {
                            String selecteditem = duasAdapter.getItem(selected.keyAt(i));
                            // Remove selected items following the ids
                            duasAdapter.remove(selecteditem);
                        }
                    }
                    // Close CAB
                    mode.finish();
                    return true;
                default:
                    return false;
                }

        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            duasAdapter.removeSelection();
        }




    };


}

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="@string/delete"
       />

</menu>

1 个答案:

答案 0 :(得分:1)

从查看代码我可以看到 onCreateActionMode 回调函数返回false。

  如果应该创建动作模式,

onCreateActionMode应该返回true,如果应该中止进入这种模式,则返回false。

因此,当用户长按ListItem并触发事件时,它会到达 onCreateActionMode 并返回false。告诉它中止。因此,不会出现CAB菜单。尝试将其更改为true,并添加一些日志以查看它是否进入回调函数。

这将是以下变化:

  @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
         Log.d("Test","In onCreateActionMode");
         mode.getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;//code updated here
    }

Android链接参考:http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode,android.view.Menu)