Floating context menu does not work in ListFragment

0

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?

    
asked by anonymous 18.09.2015 / 06:16

1 answer

0

Well, messes happen to so many around and I have to be part of that bumbling roll. Joking aside, I'm going to report the root of the problem here to serve as a "Do not do it" warning.

The code used in the question works perfectly . And it does not need no change to accomplish what it should, which is to display the context menu.

The only comment I leave is about the return of the setOnItemLongClickListener event that should be associated with the ListView if you want to catch the list object. As explained by @ramaral , in order for the context menu to be displayed, the return of OnItemLongClickListener must be false .

Let's get in the way

In my project I work with objects that contain rules regarding the manipulation of screen events and requests made by AsyncTask. This business object maintains an attribute that contains a reference to Activity, called context , and in this case a reference to a ListFragment, called listFragment . The problem that the context menu is not being displayed is that instead of calling listFragment.registerForContextMenu() was calling context.registerForContextMenu() . Which by itself was already a big mistake since I make this link in ListFragment.onCreateView .

    
18.09.2015 / 17:56