如何在android中的工具栏上设置动作模式

时间:2016-04-18 04:15:01

标签: android

我在片段中只创建了工具栏。我已经实现了多个删除列表视图的操作模式。但是动作模式将在工具栏(即)操作栏上方打开。如何在工具栏上显示该操作模式我在我的主题中尝试了<item name="windowActionModeOverlay">true</item>,但它不起作用。

public class Event_notification extends Fragment implements SwipeRefreshLayout.OnRefreshListener  {

    List<String> myList;
    private List<FeedItem> feedsList= new ArrayList<FeedItem>();
    ListView listView;
    TextView emptyview;
    String message;
    EventNotification_adapter adapter;
    String userID,userid_share;
    CheckBox check;
    ArrayList<String> list = new ArrayList<String>();
    String EventStatus,date;
    Dialog dialog;
    public static ActionMode mActionMode;
    ActionBar action;
    private JSONObject EventJsonObject;
    protected static final String TAG = "NOTIFICATION";
    ProgressWheel pw;
    ResetPasswordActivity obj = new ResetPasswordActivity();
    Constants constants=new Constants();

    private SwipeRefreshLayout swipeRefreshLayout;

    public static final String MY_PREFS_NAME = "MyPrefsFile";
    public Event_notification() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *

     * @return A new instance of fragment Event_notification.
     */
    // TODO: Rename and change types and number of parameters
    @Override
    public void onCreate(Bundle savedInstanceState) {
//        getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
        super.onCreate(savedInstanceState);
//        getActivity().getActionBar().hide();


        SharedPreferences prefs1 = getContext().getSharedPreferences(MY_PREFS_NAME, getContext().MODE_PRIVATE);
        userID= prefs1.getString("userid", null);
        System.out.println("userid" + userID);

//        actionBar.hide();
        // setHasOptionsMenu(false);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        System.out.println("Notification Fragment");
        View v=inflater.inflate(R.layout.fragment_event_notification, container, false);
        //return inflater.inflate(R.layout.row_eventnotification, container, false);
        FontChangeCrawler fontChanger = new FontChangeCrawler(getContext().getAssets(), "arial.ttf");
        fontChanger.replaceFonts((ViewGroup) v);
        emptyview=(TextView)v.findViewById(R.id.empty_view);
        listView=(ListView)v.findViewById(R.id.recycler_view);
        swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);
        check=(CheckBox)v.findViewById(R.id.check1);
        pw = (ProgressWheel) v.findViewById(R.id.pw_spinner);
        obj.styleRandom(pw, getContext());
        getnotification();

        adapter=new EventNotification_adapter(getContext(),feedsList,Event_notification.this);

        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

        listView.setMultiChoiceModeListener(new  MultiChoiceModeListener() {

            @Override

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

            }
                @Override

            public void  onDestroyActionMode(ActionMode mode) {

                adapter.removeSelection();
                               }
                      @Override

            public boolean  onCreateActionMode(ActionMode mode, Menu menu) {
                    // TODO  Auto-generated method stub
                mActionMode = mode;
            mActionMode.getMenuInflater().inflate(R.menu.multiple_delete, menu);
                    return true;
                }

                @Override
                public boolean  onActionItemClicked(final ActionMode mode,

                                                final MenuItem item) {
                    // TODO  Auto-generated method stub
                    switch  (item.getItemId()) {
                        case R.id.selectAll:
                            final int checkedCount  = feedsList.size();

                        // If item  is already selected or checked then remove or
                        // unchecked  and again select all
                            for (int i = 0; i < checkedCount; i++) {
                                listView.setItemChecked(i, true);
                            //  listviewadapter.toggleSelection(i);
                        }
                            mode.setTitle(checkedCount  + "  Selected");
                             return true;

                    case R.id.unselect:
                        mode.finish();
                        return false;

                    case R.id.delete:

                        // Add  dialog for confirmation to delete selected item
                        // record.
                        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getContext(), R.style.AppCompatAlertDialogStyle);
                        builder.setMessage("Do you  want to delete selected record(s)?");
                        builder.setNegativeButton("No", new  DialogInterface.OnClickListener() {

                                @Override

                            public void  onClick(DialogInterface dialog, int which) {
                                    // TODO  Auto-generated method stub
                            }

                        });

                        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                            @Override

                            public void onClick(DialogInterface dialog, int which) {

                                // TODO  Auto-generated method stub
                                   SparseBooleanArray selected = adapter
                                            .getSelectedIds();

                                    for (int i = (selected.size()); i >= 0; i--) {
                                        if (selected.valueAt(i)) {
//                                        String selecteditem=Integer.parseInt(selected.keyAt(i));
//
                                        FeedItem movie = feedsList.get(selected.keyAt(i));

                                        list.add(movie.getMessageid());

                                        System.out.println("EVENT ID____"+list);

                                        int  selecteditem = selected.keyAt(i);
                                        // String selecteditem1=Integer.toString(item.getItemId());
                                        int selecteditem1=item.getItemId();

                                        EventDelete();
                                        dialog.dismiss();
                                        // Remove  selected items following the ids
                                            adapter.remove(selecteditem);
                                        System.out.println("FEEDLIST SIZE+++"+feedsList.size());
                                        if(feedsList.size()==0){
                                            emptyview.setVisibility(View.VISIBLE);
                                        }
                                        else{
                                            emptyview.setVisibility(View.GONE);
                                        }
                                    }
                                }
                                // Close CAB
                                mode.finish();
                                selected.clear();
                            }

                        });

                        AlertDialog alert =  builder.create();
                        //alert.setIcon(R.drawable.questionicon);// dialog  Icon
                        alert.setTitle("Confirmation"); // dialog  Title
                        alert.show();
                        return true;
                    default:
                        mode.finish();
                        return false;

                }


            }

`

1 个答案:

答案 0 :(得分:0)

试试这个。

android.view.ActionMode mMode = toolbar.startActionMode(new Callback() {

            @Override
            public boolean onPrepareActionMode(
                    ActionMode mode, Menu menu) {
                    return false;
            }

            @Override
            public void onDestroyActionMode(
                    ActionMode mode) {

            }

            @Override
            public boolean onCreateActionMode(
                    ActionMode mode, Menu menu) {
                    return false;
            }

            @Override
            public boolean onActionItemClicked(
                    ActionMode mode, MenuItem item) {
                    return false;
            }
});

如果你想 Fininsh 动作模式

if (mMode != null) {
        mMode.finish();
}

不要忘记在styles.xml

下的主要主题中添加以下内容
<item name="android:windowActionModeOverlay">true</item>

你很高兴。