如何在android中的适配器类中显示弹出菜单

时间:2017-08-11 11:52:38

标签: android popupmenu

我是Android开发的新手,我使用适配器类查看了一个填充的回收器视图。这里的问题是当用户点击textViews 时实现弹出菜单。我只能访问适配器类中的textView,因为它没有活动,我在实现弹出菜单时遇到了困难。我搜索了这个网站,我找到了类似的链接,但他们的解决方案从未对我有效。这就是为什么我注册也许你们可以我。我已经尝试了两个晚上,我似乎无法找到解决方法。在这个适配器类中实现弹出菜单时总是会出错,它似乎永远不会起作用,而且非常令人沮丧。

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> values;

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView txtHeader;
        public View layout;

        public ViewHolder(View v) {
            super(v);
            layout = v;
            txtHeader = (TextView) v.findViewById(R.id.postTitle);
        }
    }

    public void add(int position, String item) {
        values.add(position, item);
        notifyItemInserted(position);
    }

    public void remove(int position) {
        values.remove(position);
        notifyItemRemoved(position);
    }

    public MyAdapter(List<String> myDataset, Context context) {
        values = myDataset;
        this.context = context.getApplicationContext();
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.postlayout2, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final String name = values.get(position);
        try {
            final String postTitle;
            postTitle = name;
            holder.txtHeader.setText(postTitle);

            // I would like to implement a popup menu display here
        } catch (Exception e) {
            Log.d("KOPe", "3: " + e.getMessage());
        }
    }

    @Override
    public int getItemCount() {
        return values.size();
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

bind()方法的 ViewHolder 类中使用它:

textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(textView.getContext());
                alertDialog.setTitle("Message");
                alertDialog.setMessage(Html.fromHtml("<small> successfully done </small>"));
                alertDialog.setNegativeButton("no",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                               //perform action
                                Log.d(TAG,"done");
                            }
                        });
                final AlertDialog alert = alertDialog.create();
                alert.show();
            }
        });