自定义 ListView 中的弹出菜单 [英] Popup Menu in custom ListView

查看:31
本文介绍了自定义 ListView 中的弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要达到的目标:

我有一个自定义的 ListView 适配器.我想为每个 Listitem 添加一个弹出菜单,与当前 Google Play 应用程序中的 ListView 非常相似.

I have a custom ListView adapter. To each Listitem I want to add a popup menu, pretty similar to the ListView in the current Google Play application.

这是我试过的:我的大部分代码来自这个 Android 示例 samplesandroid-19uiActionBarCompat-ListPopupMenu

This is what I tried: Most of my code comes from this Android sample samplesandroid-19uiActionBarCompat-ListPopupMenu

CustomFragmentPageAdapter.java:

// create new fragment
mCustomFragment = CustomFragment.newInstance(position);

CustomFragment.java

public class CustomFragment extends ListFragment implements View.OnClickListener{

...

@Override
public void onClick(final View v) {
    v.post(new Runnable() {
        @Override
        public void run() {
            showPopupMenu(v);
        }
    });
}

private void showPopupMenu(View view) {

    PopupMenu popup = new PopupMenu(getActivity(), view);

    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

    popup.show();
}

CustomArrayAdapter:

public class CustomAdapter extends ArrayAdapter<WatchListPlayerItem> {
    ...    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View rowView = inflater.inflate(R.layout.watch_list_row, parent, false);

        View popupButton = rowView.findViewById(R.id.imgPopUp);

        popupButton.setTag(getItem(position));

        popupButton.setOnClickListener(mFragment);

        return rowView;
    }
}

popup_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/install"
        android:title="Install" />
    <item
        android:id="@+id/addtowishlist"
        android:title="Add to wishlist" />
</menu>

Logcat 输出:

java.lang.RuntimeException: Failed to resolve attribute at index 6
            at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
            at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423)
            at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591)
            at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
...

在我的 CustomFragment 中的 popup.show() 处抛出错误.

The error is thrown at popup.show() in my CustomFragment.

这个错误显然让我发疯了,非常感谢任何解决这个问题的帮助!

This error is clearly driving me crazy and ANY help to solve this issue is highly appreciated!

推荐答案

我终于找到了问题的解决方案,尽管我没有解释为什么这个解决方案有效.

I finally found the solution to my problem, eventhough I have no explanation why this solution works.

使用以下导入我总是有错误:

With the following import I always had the error:

import android.support.v7.widget.PopupMenu;

它适用于以下导入:

import android.widget.PopupMenu;

我测试了 Ric 提供的代码(感谢您的大力帮助!)和我自己的.两人现在都在工作.也许有人会解释为什么在这种情况下导入很重要.

I tested the code provided by Ric (Thanks for the great help!) and my own. Both are working now. Maybe someone has an explanation why the import matters in this case.

这篇关于自定义 ListView 中的弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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