1

I have a recyclerview inside my PopupWindow, how do I dismiss the PopupWindow after clicking on an item inside my recycler?

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);
    }

And inside my ViewHolder I implemented an onClick method:

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?
        }
    }

Edit:

public interface OnRecyclerItemClick{
        void dismissDialog();
    }

SelectBucketMenu extends PopupWindow implements SelectBucketAdapter.OnRecyclerItemClick

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


}

//How do I call the interface method here?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

3 Answers3

2

There is 2 way to do this : 1) You can use the interface to dissmis your dialog 2) You can send your dialog object in your adapter constructor and after that when you set the click method in adapter then set their dialog.dismiss();

//this will be done in your adapter 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?
        }
    }

and this will be done in you activity/fragment

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

// No send listner object in you adapter constructor:

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
  • 1,972
  • 1
  • 8
  • 17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206133/discussion-between-sandeep-malik-and-david-s). – Sandeep Malik Jan 17 '20 at 09:58
2

EDIT: Sandeep Malik has two other valid options. It's up to you to choose. :)

You can user the ViewHolder as an inner class in the SelectBucketMenu, assuming SelectBucketMenu has a dismiss() function like so:

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
        }
    }
}
Khodor
  • 129
  • 1
  • 7
0

You can pass Dialog or AlertDialog reference via RecyclerView Adapter constructor. Then You can dismiss the dialog inside click event. If you use normal view for popup, you can pass the View reference via RecyclerView Adapter constructor and visibility gone inside the click event.