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

时间:2015-01-16 00:22:04

标签: android android-fragments

我正在尝试在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;
        }
    });

1 个答案:

答案 0 :(得分:0)

您可以尝试在片段的父布局上设置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;

        }

    });