Floating context menu does not work in Listfragment

Asked

Viewed 509 times

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?

  • 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].

  • I’ve seen it and I see it’s returning false, if he returned true could be the problem. At this point I don’t know what else could be.

  • 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() in false onCreateContextMenu() from Mainactivity is called.

1 answer

0

Well, messes happen all the time out there and I have to be part of that mess list. Joking aside, I’m going to report here the root of the problem so that it serves as a warning like "Don’t do this".

The code used in the question works perfectly. And you don’t need none change to accomplish what it should, which is to display the context menu.

The only observation I leave is about the return of the event setOnItemLongClickListener which must be associated with Listview if you want to capture the object from the list. As explained by @ramaral, for the context menu to be displayed it is necessary that the return of OnItemLongClickListener be it false.

Let’s go to the mess

In my project I work with objects that contain the 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 specific case a reference to a Listfragment, called listFragment. The problem that the context menu is not being presented is that instead of me calling listFragment.registerForContextMenu() was calling out for context.registerForContextMenu(). Which in itself was a big mistake since I make that bond on ListFragment.onCreateView.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.