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