0
I am developing an App that uses a Drawerlayout to present a side menu. One of the items presented by this menu is a Listfragment. In my Listview I want to exhibit a floating context menu.
In the onCreateView()
of my Listfragment I made the Binding of my list and ran the method registerForContextMenu()
:
@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 method onCreateContextMenu()
:
@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 apparently everything is normal the menu does not appear when holding the Listview item. When debugging the code I realized that in none of the other Ragments the onCreateContextMenu() method is executed, only in the Activity responsible for switching the Fragments. How to solve this problem?
Do you have any
OnItemLongClickListener()
attributed to that Listview?– ramaral
Yes, I do, @ramaral. It is detailed in the question on Viewholder [http://answall.com/questions/87609/vincular-informa%C3%A7%C3%B5es-do-modelo-em-uma-listview-de-forma-correta].
– Geison Santos
I’ve seen it and I see it’s returning
false
, if he returnedtrue
could be the problem. At this point I don’t know what else could be.– ramaral
Changing the return value of Onitemlongclicklistener() to
true
onCreateContextMenu() of my Mainactivity is no longer called, however neither is the Fragment-specific method. Keeping the return of Onitemlongclicklistener() infalse
onCreateContextMenu() from Mainactivity is called.– Geison Santos