Related choices in context menu in fragments



  • I have fragments, and they have two methods to create a contextual menu:

    public class PagePhrases extends Fragment {
        private String TAG = "myApplication";
        private String MSG = "MSG: ";
    
    private View viewForOpenContextMenu = null;
    private int itemPosition = 0;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.table, null);
        viewForOpenContextMenu = view.findViewById(R.id.viewForOpenContextMenu);
    
        ...
        ...
        ...
    
        return view;
    }
    
    ...
    ...
    ...
    
    public void onLongItemClick(int position) {
        Log.i(TAG, MSG + "PAGE PHRASES 1");
        itemPosition = position;
    
        registerForContextMenu(viewForOpenContextMenu);
        getActivity().openContextMenu(viewForOpenContextMenu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Log.i(TAG, MSG + "PAGE PHRASES");
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(getActivity(), "Удалить = " + itemPosition, Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getActivity(), "Изменить = " + itemPosition, Toast.LENGTH_SHORT).show();
                break;
        }
    
        return super.onContextItemSelected(item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        Log.i(TAG, MSG + "PAGE PHRASES 2");
        menu.add(Menu.NONE, 1, Menu.NONE, R.string.context_menu_item_1);
        menu.add(Menu.NONE, 2, Menu.NONE, R.string.context_menu_item_2);
    }
    

    }

    and

    public class PageWords extends Fragment {
    private String TAG = "myApplication";
    private String MSG = "MSG: ";

    private View viewForOpenContextMenu = null;
    private int itemPosition = 0;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.table, null);
        viewForOpenContextMenu = view.findViewById(R.id.viewForOpenContextMenu);
    
        ...
        ...
        ...
    
        return view;
    }
    
    ...
    ...
    ...
    
    public void onLongItemClick(int position) {
        Log.i(TAG, MSG + "PAGE WORDS 1");
        itemPosition = position;
    
        registerForContextMenu(viewForOpenContextMenu);
        getActivity().openContextMenu(viewForOpenContextMenu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Log.i(TAG, MSG + "PAGE WORDS");
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(getActivity(), "Удалить = " + itemPosition, Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getActivity(), "Изменить = " + itemPosition, Toast.LENGTH_SHORT).show();
                break;
        }
    
        return super.onContextItemSelected(item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        Log.i(TAG, MSG + "PAGE WORDS 2");
        menu.add(Menu.NONE, 1, Menu.NONE, R.string.context_menu_item_1);
        menu.add(Menu.NONE, 2, Menu.NONE, R.string.context_menu_item_2);
    }
    

    }

    Here's the mark:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white" >

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/table_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /&gt;
    
    &lt;View
        android:id="@+id/viewForOpenContextMenu"
        android:layout_width="0dp"
        android:layout_height="0dp" /&gt;
    

    </LinearLayout>

    The problem is, onLongItemClick and onCreateContextMenu is for one fragment, and, in the choice of the paragraph, onContextItemSelected It's called first in one and then in another piece! But why and how will it get rid of it?



  • It's probably the same thing. ID I don't know. Do them differently (i.e. 1 and 2 in the first fragment and 3, 4 in the second) and should earn it properly.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2