Android弹窗ListPopupWindow的简单应用

本文介绍了如何在Android中使用ListPopupWindow和PopupWindow来创建弹窗列表选择,包括自定义Adapter以实现更丰富的显示效果。通过实例代码展示了如何设置弹窗的高度、宽度、模态以及背景,并提供了单行文本和多样式列表的Adapter实现。
摘要由CSDN通过智能技术生成

概述

常用的弹窗有菜单,或者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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值