PopupWindow 中的 RecyclerView,单击项目后如何关闭?

RecyclerView inside PopupWindow, how to dismiss after clicking item?

我的 PopupWindow 里面有一个 recyclerview,我如何在我的回收站中的一个项目上关闭 clicking 之后的 PopupWindow

public SelectBucketMenu(Context context, ArrayList<String> mBucketNames, ArrayList<String> mImagesBucket) {
        super(context);
        this.mContext = context;
        this.mBucketNames = mBucketNames;
        this.mImagesBucket = mImagesBucket;

        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        //Need set windowlayout for API 19 otherwise window won't appear
        setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        setupView();
    }

    private void setupView(){
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_selectbucket, null);
        ButterKnife.bind(this, view);
        setContentView(view);

        mAdapter = new SelectBucketAdapter(mContext, mBucketNames, mImagesBucket);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycler.setAdapter(mAdapter);
    }

在我的 ViewHolder 中,我实现了一个 onClick 方法:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        @BindView(R.id.vh_selectbucketmenu_rellayout) RelativeLayout vhLayout;

        int mPosition;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            vhLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mPosition = getAdapterPosition();

            //How to dismiss the menu?
        }
    }

编辑:

public interface OnRecyclerItemClick{
        void dismissDialog();
    }

SelectBucketMenu extends PopupWindow implements SelectBucketAdapter.OnRecyclerItemClick

@Override
public void onClick(View view) {
   mPosition = getAdapterPosition();


}

//这里如何调用接口方法?

有 2 种方法可以做到这一点:1) 您可以使用 interface 关闭您的对话 2)你可以在你的适配器构造函数中发送你的对话框对象,然后当你在适配器中设置点击方法然后设置它们的 dialog.dismiss();

//这将在您的适配器中完成class

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private OnRecyclerItemClick clickListner;

        @BindView(R.id.vh_selectbucketmenu_rellayout) RelativeLayout vhLayout;

        int mPosition;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            vhLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mPosition = getAdapterPosition();
            clickListner.dismissDialog();
            //How to dismiss the menu?
        }
    }

这将在你身上完成 activity/fragment

private OnRecyclerItemClicklistner listner = new ViewPlanSelectedListner() {
        @Override
        public void OnRecyclerItemClick() {
            dialog.dissmis();
        }
    };

// 适配器构造函数中没有发送监听器对象:

private void setupView(){
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_selectbucket, null);
        ButterKnife.bind(this, view);
        setContentView(view);

        mAdapter = new SelectBucketAdapter(mContext, mBucketNames, mImagesBucket, listner);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycler.setAdapter(mAdapter);
    }

编辑:Sandeep Malik 还有两个其他有效选项。由你来选择。 :)

您可以在 SelectBucketMenu 中将 ViewHolder 用作内部 class,假设 SelectBucketMenu 具有 dismiss() 函数,如下所示:

public SelectBucketMenu(Context context, ArrayList<String> mBucketNames, ArrayList<String> mImagesBucket) {
        super(context);
        this.mContext = context;
        this.mBucketNames = mBucketNames;
        this.mImagesBucket = mImagesBucket;

        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        //Need set windowlayout for API 19 otherwise window won't appear
        setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        setupView();
    }

    private void setupView(){
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_selectbucket, null);
        ButterKnife.bind(this, view);
        setContentView(view);

        mAdapter = new SelectBucketAdapter(mContext, mBucketNames, mImagesBucket);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycler.setAdapter(mAdapter);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        @BindView(R.id.vh_selectbucketmenu_rellayout) RelativeLayout vhLayout;

        int mPosition;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            vhLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mPosition = getAdapterPosition();

            dismiss(); // Here's what you need
        }
    }
}

您可以通过 RecyclerView 适配器构造函数传递 Dialog 或 AlertDialog 引用。然后您可以关闭单击事件中的对话框。如果您将普通视图用于弹出窗口,则可以通过 RecyclerView Adapter 构造函数传递 View 引用,并在点击事件中传递可见性。