对话框打开再生器视图行按钮单击

时间:2016-07-11 10:24:55

标签: android

在Recycler View中我点击一个按钮添加到购物车,打开一个对话框。 在Recycler视图适配器

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.addtocart.setOnClickListener((Mp3HindiLandingActivity) context);
}

在我的活动中(Mp3HindiLandingActivity)

          @Override
         public void onClick(final View v) {
    switch (v.getId()) { case R.id.addtocart:
            PopupMenu popup = new PopupMenu(ctx, v);
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menu1:

                            Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
                            return true;
                        case R.id.menu2:
                            Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
                            return true;
                        default:
                            return false;
                    }
                }
            });
            popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
            popup.show();

            break;`
           }}

它显示addtocart图标下方的菜单,但我需要根据附加的图像具有不同背景的菜单项?

请告诉我这是如何实施的 Please Refer Image

2 个答案:

答案 0 :(得分:7)

你可以试试这个。

paletteViewHolder.btncart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    PopupMenu popup = new PopupMenu(paletteViewHolder.imgpopup.getContext(), v);
                    // This activity implements OnMenuItemClickListener .
                    //popup.setOnMenuItemClickListener ((OnMenuItemClickListener) this);
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.friendrequest:
                                    Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
                                    return true;
                                case R.id.blockhide:
                                    Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
                                    return true;
                                case R.id.followunfollow:
                                    Toast.makeText(v.getContext(), "Follow/UnFollow", Toast.LENGTH_LONG).show();
                                    return true;
                                default:
                                    return false;
                            }
                            //return false;
                        }
                    });
                    popup.inflate(R.menu.menu);
                    popup.show();
                }
            });

//在菜单

上创建menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu1"
        android:title="menu1"/>
    <item
        android:id="@+id/menu2"
        android:title="menu2"/>
    <item
        android:id="@+id/menu3"
        android:title="menu3"/>
</menu>

对你有帮助。

答案 1 :(得分:2)

首先创建一个内部类ViewHolder,如下所示:

private class MyViewHolder extends RecyclerView.ViewHolder {
    public Button btnCart;

    public MyViewHolder(View view) {
        super(view);
        btnCart = (Button) view.findViewById(R.id.btnCart);
    }
}

然后覆盖适配器中的onBindViewHolder(您的适配器扩展了RecyclerView.Adapter)。

@Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.btnCart.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               // call the opening of your dialog here
           }
        });

    }