概述
常用的弹窗有菜单,或者Dialog,但更加人性化和可自定义的还是PopupWindow
如果只是展示列表数据或者弹窗列表选择,直接使用ListPopupWindow即可,不用再单独去设置布局。
如果想要更加多样化的那就自定义一个布局,使用PopupWindow即可,也不复杂。
用法
自定义ListPopupWindow类
public class ChargeItemSumPop extends ListPopupWindow {
public ChargeItemSumPop(Context context) {
super(context);
}
}
属性设置
因为里面已经有一个列表控件了,所以,不用再绑定布局
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(600);
setModal(true);
setBackgroundDrawable(new ColorDrawable(0xCC000000));
绑定Adapter
//添加想要展示的数据
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
List<Integer> lstYear = new ArrayList<>();
for(int i = 2015; i <= year; i++){
lstYear.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, lstYear);
setAdapter(adapter);
Activity监听
ChargeDateYearPop pop = new ChargeDateYearPop(this);
pop.setOnItemClickListener((adapterView, view, i, l) -> {
bindingView.chargeYear.setText(String.valueOf(adapterView.getAdapter().getItem(i)));
pop.dismiss();
});
pop.setAnchorView(bindingView.chargeYear);
pop.show();
完整弹窗类
与普通的弹窗不一样的地方在于这里面是一个列表,所以要绑定Adapter进行展示
public class ChargeDateYearPop extends ListPopupWindow {
public ChargeDateYearPop(Context context) {
super(context);
setHeight(800);
setWidth(200);
setModal(true);
setBackgroundDrawable(new ColorDrawable(0xCC000000));
initView(context);
}
private void initView(Context context) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
List<Integer> lstYear = new ArrayList<>();
for(int i = 2015; i <= year; i++){
lstYear.add(i);
}
Collections.sort(lstYear);
Collections.reverse(lstYear);
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, lstYear);
setAdapter(adapter);
}
}
Activity
private void showChargeDateYear(){
ChargeDateYearPop pop = new ChargeDateYearPop(this);
pop.setOnItemClickListener((adapterView, view, i, l) -> {
bindingView.chargeYear.setText(String.valueOf(adapterView.getAdapter().getItem(i)));
pop.dismiss();
//重载数据等的操作
//mPresenter.getCharges(getChargeDate());
});
pop.setAnchorView(bindingView.chargeYear);
pop.show();
}
自定义Adapter
有时候发现自带的列表显示上不是自己想要的,就需要自定义Adapter来丰富画面显示
单行文本Adapter
一般使用ListPopupWindow只是用来显示单行文本列表,但Android自带的几个布局不可控,此时就需要自定义一个Adapter来绑定文本列表
使用范型 T 来控制传入的是 Integer 还是 String
public class ListAdapter<T extends Object> extends BaseAdapter {
List<T> mList;
Context mContext;
public ListAdapter(Context context, List<T> lst) {
mContext = context;
mList = lst;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ConstraintLayout layout = new ConstraintLayout(mContext);
TextView textView = new TextView(mContext);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setText(String.valueOf(mList.get(i)));
textView.setTextSize(16);
textView.setTypeface(Typeface.DEFAULT_BOLD); //加粗文字
textView.setPadding(5, 6, 5, 9);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
layout.addView(textView, layoutParams);
return layout.getRootView();
}
}
多样式Adapter
那就要创建一个item 的布局文件了,然后在 getView里返回绑定好的布局即可
以下代码仅作参考,因为我这里使用ViewBinding视图绑定功能,同时对ListView作了一些优化,写出来是想告诉朋友,只要你想,复杂的布局样式也是可以实现在 ListPopupWindow 中的。
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ItemChargeDataBinding mView;
if (view == null) {
mView = ItemChargeDataBinding.inflate(LayoutInflater.from(mContext));
view = mView.getRoot();
view.setTag(mView);
} else mView = (ItemChargeDataBinding) view.getTag();
ChargeDataBean chargeDataBean = lstCharges.get(i);
if (chargeDataBean.getItem_desc().length() > 1)
mView.itemChargeHead.setText(chargeDataBean.getItem_desc().substring(0, 1));
mView.itemChargeAccount.setText(chargeDataBean.getAccount_desc());
mView.itemChargeItem.setText(chargeDataBean.getItem_desc());
mView.itemChargeTag.setText(chargeDataBean.getCharge_tag());
mView.itemChargeRem.setText(chargeDataBean.getCharge_rem());
mView.itemChargeDate.setText(chargeDataBean.getCharge_date());
if (chargeDataBean.getCharge_type() == 1) {
mView.itemChargeDataLabel.setText("+");
mView.itemChargeDataLabel.setTextColor(ContextCompat.getColor(mView.getRoot().getContext(), R.color.Crimson));
mView.itemChargeAmount.setTextColor(ContextCompat.getColor(mView.getRoot().getContext(), R.color.Crimson));
} else {
mView.itemChargeDataLabel.setText("-");
mView.itemChargeDataLabel.setTextColor(ContextCompat.getColor(mView.getRoot().getContext(), R.color.SpringGreen));
mView.itemChargeAmount.setTextColor(ContextCompat.getColor(mView.getRoot().getContext(), R.color.SpringGreen));
}
DecimalFormat df = new DecimalFormat("0.00");
mView.itemChargeAmount.setText(df.format(chargeDataBean.getCharge_amount()));
return view;
}