如何选择后长按一个ListView项? [英] How to select an ListView item after long click?

查看:165
本文介绍了如何选择后长按一个ListView项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愚蠢的小问题。我已经注册了一个 ListFragment 既是 OnItemClickListener OnItemLongClickListener 自己的的ListView

I've got a silly little problem. I've registered a ListFragment both as OnItemClickListener and OnItemLongClickListener of its own ListView.

onItemClick 事件被称为,意图用于该项目的详细视图活动开始后,有没有问题。

When the onItemClick event is called, an intent for the detail view activity of that item is started, no problems there.

onItemLongClick 事件发生,我要完成以下事情:

When the onItemLongClickevent happens, I want to accomplish the following things:

  • 创建一个CAB
  • 保持长期pressed项目中选择

code:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    if(this.cabMode != null)
        return false;
    this.cabMode = getActivity().startActionMode(editModeCallback);
    view.setSelected(true);
    return true;
}

在CAB将显示,但是,选择不会留在该项目。

The CAB will show, however, the selection won't stay with the item.

一些零零碎碎的,万一他们是相关的:我读过有关解决此问题,调用 view.requestFocusFromTouch()或使用 listView.setItemChecked(),但是这并没有为我工作。此外,对于列表项的意见是从自定义布局实例化,但不具有任何定制的事件侦听器附

Some bits and pieces, in case they are relevant: I've read about fixing this issue with calls to view.requestFocusFromTouch() or using listView.setItemChecked(), but that didn't work for me. Also, the views for the list items are instanced from a custom layout, but don't have any custom event listeners attached.

任何帮助是AP preciated。 THX!

Any help is appreciated. Thx!

推荐答案

这是可能的,但只是勉强......其实我不知道这样一个简单的事情如何收场如此可笑的复杂。

It's possible, but just barely... I actually don't know how such a simple thing can wind up so ridiculously complicated.

的关键,答案可以在这里找到:<一href="http://stackoverflow.com/questions/12386333/android-keep-blue-background-after-listview-selection">Android:保持蓝色背景后的ListView选择

The key to the answer can be found here: Android: keep blue background after ListView selection

这是什么归结为是定义所使用的 ListView的一个附加风格和设置选择模式为 AbsListView.CHOICE_MODE_SINGLE (如链接答案解释)。

What this boils down to is to define an additional style that is used by the ListView and setting the choice mode to AbsListView.CHOICE_MODE_SINGLE (as explained in the linked answer).

这允许您以编程方式切换使用选择 Listview.setItemChecked()。但是,你需要保持在 onItemLongClick 回调自己被触按项目的索引轨道,因为 ListView.setSelection()不会那么做(至少 ListView.getSelectedItem()总是返回-1,据我可以看到)。

This allows you programmatically toggle the selection using Listview.setItemChecked(). However, you need to keep track of the index of the touched item in the onItemLongClick callback yourself, because ListView.setSelection() won't do that (at least ListView.getSelectedItem() will always return -1 as far as I can see).

code(为简单起见,我的片段实现了所有三个 OnItemClickListener OnItemLongClickListener ,和          ActionMode.Callback ):

Code (for simplicity, my fragment implements all three OnItemClickListener, OnItemLongClickListener, and ActionMode.Callback):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    this.listViewAdapter = new ListViewAdapter();
    this.root = (ListView)inflater.inflate(R.layout.fragment_bookmarks, container, false);
    this.root.setAdapter(this.listViewAdapter);
    this.root.setOnItemClickListener(this);
    this.root.setOnItemLongClickListener(this);
    this.root.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
    return this.root;
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    if(this.cabMode != null)
        return false;
    this.selectedPosition = position;
    this.root.setItemChecked(position, true);

    this.root.setOnItemClickListener(null);
    this.cabMode = getActivity().startActionMode(this);
    return true;
}

最后,如果你想摆脱的选择时,CAB关闭:

And finally, if you want to get rid of the selection when the CAB is closed:

@Override
public void onDestroyActionMode(ActionMode mode) {
    cabMode = null;
    this.root.setItemChecked(this.selectedPosition, false);
    this.selectedPosition = -1;
    this.root.setOnItemClickListener(this);
}

注册和注销的 OnItemClickListener 可确保同时CAB活跃,你不会不小心触发通常与项目相关的操作(​​如打开详细视图)。

Registering and unregistering the OnItemClickListener makes sure that while the CAB is active you won't accidentally trigger the action usually associated with the item (like opening a detail view).

这篇关于如何选择后长按一个ListView项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆