长按ListFragment

发布于 2024-11-24 22:40:46 字数 166 浏览 5 评论 0原文

我正在使用 ListFragment 并执行 onListItemClick。一切正常,但现在我想使用长项目单击(例如 setOnItemLongClickListener(new OnItemLongClickListener() for an Activity)。我如何在我的片段中使用它?

谢谢!

I'm working with a ListFragment and doing a onListItemClick. Everything works fine, but now I want to use a long Item Click (e.g setOnItemLongClickListener(new OnItemLongClickListener() for an Activity). How can I use this in my fragment?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明明#如月 2024-12-01 22:40:46

是的,tsync 发布的解决方案对我有用。我也遇到了同样的问题并认为这是不可能的。我尝试了上述建议,如下:

public  class ProjectsFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();
            return true;
        }
    });

并且成功了!

Yes, the solution posted by tsync works for me. I too had ran into same problem and considered that this is not possible. I tried the above suggestion as follows:

public  class ProjectsFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();
            return true;
        }
    });

and it worked!

猫卆 2024-12-01 22:40:46

根据您想要实现的内容,您可以对上下文菜单使用给定的方法:

首先注册长按的 View 类(在 Fragment 类中):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    registerForContextMenu(this.getListView());
}

然后实现这两个方法,创建上下文菜单并执行您想要的操作单击菜单项时:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = this.getActivity().getMenuInflater();
    inflater.inflate(R.menu.my_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {

        case R.id.add: // <-- your custom menu item id here
            // do something here
            return true;

        default:
            return super.onContextItemSelected(item);
    }
}

Depending on what you want to realize you can use the given methods for context menues:

First register the View class which gets long pressed (inside your Fragment class):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    registerForContextMenu(this.getListView());
}

Than implement these two methods, to create a context menu and do what ever you want when a menu item is clicked:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = this.getActivity().getMenuInflater();
    inflater.inflate(R.menu.my_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {

        case R.id.add: // <-- your custom menu item id here
            // do something here
            return true;

        default:
            return super.onContextItemSelected(item);
    }
}
小鸟爱天空丶 2024-12-01 22:40:46

这对我有用

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
        //Get your item here with the position                   
        return true;
    }
});

This works for me

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
        //Get your item here with the position                   
        return true;
    }
});
北座城市 2024-12-01 22:40:46

有点晚了,但我最近遇到了片段中上下文菜单的问题。菜单会加载,但只会加载一个弹出浮动菜单,没有图标等。上面的答案帮助我指明了正确的方向。

因此,经过一些尝试和错误,我用以下方法解决了这个问题:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    registerForContextMenu(view)
}

在 onItemSelected:

  override fun onItemSelected(itemViewHolder: Presenter.ViewHolder?, item: Any?, rowViewHolder: RowPresenter.ViewHolder?, row: Row?) {
    itemViewHolder?.view?.setOnLongClickListener { v: View ->
        Boolean
        requireActivity().openContextMenu(itemViewHolder.view)
        true
    }
}

这解决了问题,现在当我长按一个项目时,会显示带有图标的完整上下文菜单,并且浮动弹出菜单消失了。

Bit late on this but i recently had an issue with the context menu in a fragment. The menu would load but only a popup floating menu with no icons etc.. The answer above helped point me in the right direction.

So with a bit of trial and error i resolved it with this:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    registerForContextMenu(view)
}

And in onItemSelected:

  override fun onItemSelected(itemViewHolder: Presenter.ViewHolder?, item: Any?, rowViewHolder: RowPresenter.ViewHolder?, row: Row?) {
    itemViewHolder?.view?.setOnLongClickListener { v: View ->
        Boolean
        requireActivity().openContextMenu(itemViewHolder.view)
        true
    }
}

This solved the issue, now when i long click an item the full context menu with icons is displayed and the floating popup menu has gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文