我有一个列表视图,当我长按列表时,我想显示一个菜单(上下文操作模式)。当我第一次点击时它不起作用,但其他时候它起作用。我注意到onItemLongClick总是被调用,但是Contextual Action Mode只是第二次启动。 这是我的代码:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
stringList = new LinkedList<>();
stringList.add("A");
stringList.add("B");
stringList.add("C");
stringList.add("D");
stringList.add("E");
listView = getListView();
arrayAdapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stringList);
listView.setAdapter(arrayAdapter);
final AbsListView.MultiChoiceModeListener choiceModeListener = new AbsListView.MultiChoiceModeListener()
{
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
Toast.makeText(getActivity(),position+"",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
MenuInflater menuInflater = mode.getMenuInflater();
menuInflater.inflate(R.menu.contentmenu2,menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
};
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(choiceModeListener);
Toast.makeText(getActivity(),"LONG CLICK",Toast.LENGTH_SHORT).show();
return true;
}
});
}
答案 0 :(得分:0)
我绕过了这部分代码的问题:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(choiceModeListener);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
listView.setMultiChoiceModeListener(choiceModeListener);
return true;
}
});
通过这种方式启动上下文菜单,因为我调用它两次,但我不明白为什么我要调用它两次......