此文章参考:https://www.cnblogs.com/zhujiabin/p/6236090.html 对原来的listview 替换成了 新的 recyclerview SpinerPopWindow.java public class SpinerPopWindow<T> extends PopupWindow { private LayoutInflater inflater; private RecyclerView mListView; private List<Map<String,Object>> list; private DeductionTypeListAdapter mAdapter; private Context context; private View.OnClickListener onClickListener; private TextView tvName; private SpinerPopWindow spinerPopWindow; public SpinerPopWindow(Context context, List<Map<String,Object>> list, TextView tvName) { super(context); this.context=context; inflater=LayoutInflater.from(context); this.list=list; this.tvName=tvName; init(); } public void setSpinerPopWindow(SpinerPopWindow spinerPopWindow){ this.spinerPopWindow=spinerPopWindow; } private void init(){ View view = inflater.inflate(R.layout.spiner_deductiontype, null); setContentView(view); setWidth(WindowManager.LayoutParams.WRAP_CONTENT); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); setFocusable(true); ColorDrawable dw = new ColorDrawable(0x00); setBackgroundDrawable(dw); mListView = (RecyclerView) view.findViewById(R.id.spiner_deductiontype_rc); mListView.setLayoutManager(new LinearLayoutManager(context,RecyclerView.VERTICAL,false)); mListView.setAdapter(mAdapter=new DeductionTypeListAdapter(R.layout.item_deductiontype,list)); } private class DeductionTypeListAdapter extends BaseQuickAdapter<Map<String,Object>, BaseViewHolder> { public DeductionTypeListAdapter(@LayoutRes int layoutResId, @Nullable List<Map<String,Object>> nameList) { super(layoutResId, nameList); } @Override protected void convert(@NotNull BaseViewHolder baseViewHolder, final Map<String,Object> item) { baseViewHolder.setText(R.id.item_deductiontype_tv_name, StringUtils.getNotNull(item.get("name"))); baseViewHolder.getView(R.id.item_deductiontype_ll_whole).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tvName.setText(StringUtils.getNotNull(item.get("name"))); spinerPopWindow.dismiss(); } }); } } }
下面是具体用法:
mainactivity里面
private SpinerPopWindow<String> mSpinerPopWindow;
设置下拉框点击事件
rlSelectDeductionType.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSpinerPopWindow.setWidth(rlSelectDeductionType.getWidth()); mSpinerPopWindow.showAsDropDown(rlSelectDeductionType); imDeductionSelect.setImageResource(R.drawable.icon_up);//下拉点击后的样式 } });
Map<String,Object> map=new HashMap<>(); map.put("name","12313"); deductionList.add(map); map.put("name","333"); deductionList.add(map);//随便搞一个集合加载数据用,具体需求自行添加 mSpinerPopWindow = new SpinerPopWindow<String>(this, deductionList,tvSelectDeductionType);//tvSelectDeductionType是你自己要显示选择后文字的textview mSpinerPopWindow.setSpinerPopWindow(mSpinerPopWindow); mSpinerPopWindow.setOnDismissListener(dismissListener);
/** * 监听popupwindow取消 */ private PopupWindow.OnDismissListener dismissListener=new PopupWindow.OnDismissListener() { @Override public void onDismiss() { imDeductionSelect.setImageResource(R.drawable.icon_drop);//点击选择事件结束后 弹框消失 回复下拉样式 } };