Android Contextual Action Bar not appearing after item selection

末鹿安然 提交于 2019-12-24 07:19:41

问题


In my android application, I am trying to show contextual action bar (CAB) in listview for item delete. The problem is that whenever an item is pressed for long, the item gets selected, but CAB doesn't appear. I have verified with so many tutorials, can't figure out what am I missing. Any help would be appreciated.

Adapter Class

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;
}



}

Fragment Class

    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 file

<?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:


From looking at your code I can see the onCreateActionMode callback function returns false.

onCreateActionMode should return true if the action mode should be created, false if entering this mode should be aborted.

So when the user long clicks on the ListItem and the event is triggered it reaches onCreateActionMode and returns false. Telling it to abort. Hence no CAB menu appears. Try changing it to true and also adding in some logs to see if it ever makes it into the callback function.

This would be the change below:

  @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 Link to reference : http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode, android.view.Menu)



来源:https://stackoverflow.com/questions/24358733/android-contextual-action-bar-not-appearing-after-item-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!