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