I'm developing an App that uses a DrawerLayout to display a side menu. One of the items presented by this menu is a ListFragment. In my ListView I want to display a floating context menu .
In%% of my ListFragment I made the binding of my list and executed the method onCreateView()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
ListView listView = (ListView)rootView.findViewById(android.R.id.list);
registerForContextMenu(listView);
return rootView;
}
To display the menu I executed the registerForContextMenu()
method:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
Menu Code
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:title="Item 1" />
<item android:id="@+id/item2"
android:title="Item 2" />
</menu>
Layout used in ListFragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical|center_horizontal"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:id="@+id/progressBar"/>
</LinearLayout>
</FrameLayout>
Although everything seems to be normal, the menu does not appear when holding the ListView item. When debugging the code I noticed that in none of the other fragments the onCreateContextMenu () method is executed, only in the Activity responsible for changing the fragments. How do I solve this problem?