您现在的位置是:群英 > 开发技术 > 移动开发
Android的ListPopupWindow基本用法是什么
Admin发表于 2022-04-28 17:17:301198 次浏览
在实际案例的操作过程中,我们可能会遇到“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基本用法是什么已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: ListPopupWindow
相关信息推荐
2022-06-24 17:51:27 
摘要:这篇文章主要介绍了HTML5中使用json对象的实例代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
2022-04-27 16:29:35 
摘要:获取文件类型的方法:1、使用“$_FILES['uploadfile']['type']”语句;2、使用“finfo_file(finfo_open(FILEINFO_MIME),文件路径)”语句;3、通过读取文件头六个字节来进行判断。
2022-05-19 17:39:52 
摘要:在go语言中,可以利用“Slice(切片)”追加元素的特性完成元素的删除操作,通过内建函数append()实现对单个元素以及元素片段的删除;语法“append(list[:删除索引], list[(删除索引+1):]...)”。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部