我是靠谱客的博主 安详面包,最近开发中收集的这篇文章主要介绍Android弹窗ListPopupWindow的简单应用详解,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

概述

常用的弹窗有菜单,或者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();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是安详面包为你收集整理的Android弹窗ListPopupWindow的简单应用详解的全部内容,希望文章能够帮你解决Android弹窗ListPopupWindow的简单应用详解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(153)

评论列表共有 0 条评论

立即
投稿
返回
顶部