Android彈窗ListPopupWindow的簡(jiǎn)單應(yīng)用詳解
概述
常用的彈窗有菜單,或者Dialog,但更加人性化和可自定義的還是PopupWindow
如果只是展示列表數(shù)據(jù)或者彈窗列表選擇,直接使用ListPopupWindow即可,不用再單獨(dú)去設(shè)置布局。
如果想要更加多樣化的那就自定義一個(gè)布局,使用PopupWindow即可,也不復(fù)雜。
用法
自定義ListPopupWindow類(lèi)
public class ChargeItemSumPop extends ListPopupWindow {
public ChargeItemSumPop(Context context) {
super(context);
}
}
屬性設(shè)置
因?yàn)槔锩嬉呀?jīng)有一個(gè)列表控件了,所以,不用再綁定布局
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); setWidth(600); setModal(true); setBackgroundDrawable(new ColorDrawable(0xCC000000));
綁定Adapter
//添加想要展示的數(shù)據(jù)
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監(jiān)聽(tīng)
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();
完整彈窗類(lèi)
與普通的彈窗不一樣的地方在于這里面是一個(gè)列表,所以要綁定Adapter進(jìn)行展示
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();
//重載數(shù)據(jù)等的操作
//mPresenter.getCharges(getChargeDate());
});
pop.setAnchorView(bindingView.chargeYear);
pop.show();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android中Activity跳轉(zhuǎn)的創(chuàng)建步驟總結(jié)
這篇文章主要介紹了Android中Activity跳轉(zhuǎn)的創(chuàng)建步驟總結(jié),本文詳細(xì)的講解了從工程創(chuàng)建到跳轉(zhuǎn)Activity的實(shí)現(xiàn)完整過(guò)程,需要的朋友可以參考下2014-10-10
Android仿餓了么加入購(gòu)物車(chē)旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫(huà)的按鈕效果(實(shí)例詳解)
這篇文章主要介紹了Android仿餓了么加入購(gòu)物車(chē)旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫(huà)的按鈕效果(實(shí)例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01
從零開(kāi)始使用gradle配置即可執(zhí)行的Hook庫(kù)詳解
這篇文章主要為大家介紹了從零開(kāi)始使用gradle配置即可執(zhí)行的Hook庫(kù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Compose自定義View實(shí)現(xiàn)宇智波斑寫(xiě)輪眼
這篇文章主要為大家介紹了Compose自定義View實(shí)現(xiàn)宇智波斑寫(xiě)輪眼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android Framework Application Framework層簡(jiǎn)單介紹
這篇文章主要介紹了 Android Framework Application Framework層簡(jiǎn)單介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11

