Reputation: 397
does anyone have a tutorial to implement a CAB in fragment that uses support library v7 (appcomat_v7)?
I cant find any plus the developer.android tutorial is ambiguous.
Here is my code so far. I am doing a simple test program that as you select items in the list, the context action bar will appear that says stuff like you selected x items and bring up a delete button.
I am trying to do this in a fragment is well so all code posted here is inside a fragment.
the context bar XML file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:idk="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_delete"
android:icon="@android:drawable/ic_menu_delete"
android:orderInCategory="100"
idk:showAsAction="ifRoom|withText"
android:title="Delete"/>
</menu>
This is the implementation of the ActionMode.Callback interface
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.menu_delete:
return true;
}
return false;
}
//// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.delete_only, menu);
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode)
{
// TODO Auto-generated method stub
mActionMode = null;
}
//// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
// TODO Auto-generated method stub
return false;
}
};
This is the on Item Click Listener for the list view
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
// TODO Auto-generated method stub
if(listItemSelections.contains(1) && mActionMode == null)
{
mActionMode = getActivity().startActionMode(mActionModeCallback);
}
}
This is the line that gives me the error
mActionMode = getActivity().startActionMode(mActionModeCallback);
It says
The method startActionMode(ActionMode.Callback) in the type Activity is not applicable for the arguments (ActionMode.Callback)
Im not sure what to do, I cant find a tutorial that explains the implementation within a fragment and using the support library.
Thank you for reading.
Upvotes: 1
Views: 2165
Reputation: 18923
If you are using Action bar library then use this.
ActionBarActivity activity=(ActionBarActivity)getActiivty();
activity.startSupportActionMode(mActionModeCallback);
and if it is simple activity then prefer this one
getActivity().startActionMode(mActionModeCallback);
Upvotes: 1
Reputation: 3344
If you're using support library, then you should use
mActionMode = getActivity().startSupportActionMode(mActionModeCallback);
Instead of
mActionMode = getActivity().startActionMode(mActionModeCallback);
Check for more details Support v7 ActionMode CallBack
Upvotes: 1