如何在 setOnLongClickListener 上设置和整个片段视图?

How to set and entire Fragment view on setOnLongClickListener?

我正在尝试在 setOnLongClickListener 上设置整个片段。当我为其他视图对象(如 TextView)设置 setOnLongCLickListener 时,我可以让上下文菜单出现,但当用户长按屏幕上的任何地方(片段)时则不能。请问有人可以帮忙吗?这是我的代码:

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.the_article_fragment, parent, false);

          v.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View view) {
            if (mActionMode != null) {
                return false;
            }

            mActionMode = getActivity().startActionMode(mActionModeCallback);
            view.setSelected(true);
            return true;
        }
    });

您可以尝试在片段的父布局上设置 OnLongClickListener。如果是线性布局:

  LinearLayout fragment_linearlayout= (LinearLayout)v.findViewById(R.id.fragment_linear_layout);
    fragment_linearlayout.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
              mActionMode = getActivity().startActionMode(mActionModeCallback);
              return true;

        }

    });