San Jaisy
San Jaisy

Reputation: 17128

How to override onActionItemClicked of ActionMode in android

I have a fragment with context action mode. The context action mode is working great. The ActionMode.Callback has a method onActionItemClicked. I want to override this method in my class. below is the code I tried.The override method in my class is not called.

public class HouseHoldMembersFragment extends ActionBarCallBack {
        ActionMode mMode;
        public HouseHoldMembersFragment() {
        }
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            return false;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_house_hold_members, container, false);
            ExpandList = (ExpandableListView) view.findViewById(R.id.expandable_list_view);


            ExpandList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                        mMode = view.startActionMode(new ActionBarCallBack());


                        return true;
                    }
                    return false;
                }
            });
            return view;
        }

Here is the implementation class

public class ActionBarCallBack extends Fragment implements ActionMode.Callback {
    ActionMode mMode;

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

        return false;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        mode.getMenuInflater().inflate(com.deerwalk.androidcommon.R.menu.context_action_menu, menu);
        return true;
    }

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

    }

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

        mode.setTitle("Action");
        return false;
    }


}

I want to make the ActionMode a common and overide the onActionItemClicked method. Till now i have tried but my class onActionItemClicked method is not being called. How to solve this issue.

Upvotes: 1

Views: 2182

Answers (1)

ozo
ozo

Reputation: 761

Try the following:

ExpandList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
    @Override
    public boolean onItemLongClick (AdapterView < ? > parent, View view,int position, long id){
    if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        startSupportActionMode(new ActionMode.Callback() {
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

                return false;
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                mode.getMenuInflater().inflate(com.deerwalk.androidcommon.R.menu.context_action_menu, menu);
                return true;
            }

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

            }

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

                mode.setTitle("Action");
                return false;
            }

        });
    }
}

Upvotes: 1

Related Questions