I'm a newbie, so please bear with me. I've implemented a SlidingPaneLayout in my app. However, in the example I followed it populated the menu fragment with an array of strings. I think it was just as an example. How do I populate the menu fragment with links to the fragments I want to give my users access to?
I have a menu list with the names of the fragments I'd like to display, but it doesn't seem to be implemented.
Here is the code from my menu fragment:
public class MainListFragment extends ListFragment {
private ArrayAdapter<String> mAdapter;
public static final String[] items = { "Item 1: xxxxxxxxxxxxxxxxx",
"Item 2: xxxxxxxxxxxxxxxxx", "Item 3: xxxxxxxxxxxxxxxxx",
"Item 4: xxxxxxxxxxxxxxxxx", "Item 5: xxxxxxxxxxxxxxxxx",
"Item 6: xxxxxxxxxxxxxxxxx", "Item 7: xxxxxxxxxxxxxxxxx" };
ListFragmentItemClickListener iItemClickListener;
/** An interface for defining the callback method */
public interface ListFragmentItemClickListener {
/**
* This method will be invoked when an item in the ListFragment is
* clicked
*/
void onListFragmentItemClick(View view, int position);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items);
setListAdapter(mAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
return super.onCreateView(inflater, container, savedInstanceState);
}
/** A callback function, executed when this fragment is attached to an activity */
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
/** This statement ensures that the hosting activity implements ListFragmentItemClickListener */
iItemClickListener = (ListFragmentItemClickListener) activity;
} catch(Exception e) {
Toast.makeText(activity.getBaseContext(),"Exception", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_list, menu);
}
@Override
public void onListItemClick(ListView list, View view, int position, long id) {
/**
* Invokes the implementation of the method onListFragmentItemClick in
* the hosting activity
*/
iItemClickListener.onListFragmentItemClick(view, position);
}
}
Here is the code from the menu list:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_ehr"
android:title="@string/ehr_short" />
<item
android:id="@+id/nav_protocols"
android:title="@string/protocols" />
</group>
</menu>
You should create your menu in an XML file and then handle events using menu event handlers. This example should help you.
The main idea is that when you use the Android Menu interface components you get access to event handlers like
onCreateOptionsMenuandonOptionsItemSelectedwhich allow you to control your UI flow.