我在PopupWindow内有一个recyclerview,单击回收台内的某个项目后如何关闭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?
}
}
有两种方法可以做到:1)您可以使用interface
散布对话框2)您可以在适配器构造函数中发送对话框对象,然后在适配器中设置click方法后再设置其dialog.dismiss();
您可以将ViewHolder用作SelectBucketMenu中的内部类,假设SelectBucketMenu具有如下的dismiss()函数: