一般现在很多地址选择器都是仿苹果,三级联动的地址选择器,但给人的感觉缺少点Android精神。为什么不用Popupwindow来展示呢!以前写pop要写很多东西在Activity中,而其他地方要用,又得写重复的,就算复制过来,也会加深当前页面的复杂度!所以这里打算把其提取出来!
首先我们要写个工具类:(里面包含了popwindow布局初始化,数据填充,打开和关闭,背景透明度变化等!)
public class PopBankUtils { /** * 初始化银行卡选择器 */ PopupWindow popBank; int screenHeight; int popHeight; PopBankListAdapter bankListAdapter; private Activity mContext; String bank_id="0"; onBankSelectListener listener; public interface onBankSelectListener{ void bankSelect(String bank_id); } public void setListener(onBankSelectListener listener) { this.listener = listener; } public PopBankUtils(Activity context, List<Bank> bankPopList, final TextView tvBankName) { if (bankPopList == null||bankPopList.size()==0) return; this.mContext = context; int screenWidth = ScreenUtils.getScreenWidth(mContext); screenHeight = ScreenUtils.getScreenHeight(mContext); View view = LayoutInflater.from(mContext).inflate(R.layout.pop_bank_select_list_layout, null); popBank = new PopupWindow(view, screenWidth, screenHeight, true); popHeight = popBank.getContentView().getMeasuredHeight(); popBank.setAnimationStyle(R.style.AnimRightInLeftOut); popBank.setBackgroundDrawable(new ColorDrawable(0)); popBank.setOutsideTouchable(true); popBank.setOnDismissListener(new poponDismissListener()); //设置退款原因数据 RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); bankListAdapter = new PopBankListAdapter(); recyclerView.setLayoutManager(new LinearLayoutManager(mContext)); recyclerView.setAdapter(bankListAdapter); bankListAdapter.setNewData(bankPopList); bankListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { bankListAdapter.setSelectItem(position); Bank item = (Bank) adapter.getItem(position); bank_id = item.getId(); tvBankName.setText(item.getName()); closepopBank(); } }); TextView tvTitle=(TextView) view.findViewById(R.id.tv_title); tvTitle.setText("选择银行卡"); //关闭pop ImageView btnClose = (ImageView) view.findViewById(R.id.iv_back); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { closepopBank(); } }); //设置popwindow的 for (int i = 0; i < bankPopList.size(); i++) { Bank bank = bankPopList.get(i); if (bank.getId().equals(bank_id))bankListAdapter.setSelectItem(i); } } class poponDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { ScreenUtils.setWindowBackgroundAlpha(mContext, 1f); } } public void openPopBank(){ if (popBank==null){ ToastUtil.showLong("没有银行可供选择!"); return; } popBank.showAtLocation(mContext.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0); if (popBank.isShowing()) { ScreenUtils.setWindowBackgroundAlpha(mContext,0.5f); } else { ScreenUtils.setWindowBackgroundAlpha(mContext, 1f); } } private void closepopBank() { if (null != popBank && popBank.isShowing()){ popBank.dismiss(); listener.bankSelect(bank_id); } } }
每个人的pop布局都不一样,这里我的是这样的:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/common_top_bar"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"/> <Button android:id="@+id/btn_close" android:background="@color/textYellow" android:textSize="18sp" android:textColor="@color/white" android:text="" android:visibility="invisible" android:layout_width="match_parent" android:layout_height="48dp"/></LinearLayout>
上面还用到了动画:
popBank.setAnimationStyle(R.style.AnimRightInLeftOut);
<style name="AnimRightInLeftOut"> <item name="android:windowEnterAnimation">@anim/right_in</item> <item name="android:windowExitAnimation">@anim/left_out</item> </style>
其中right_in.xml为:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="100%" android:toYDelta="0%" /></set>
其中left_out.xml为:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><translate android:duration="300" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="100%" android:toYDelta="0%" /></set>
在Activity中使用的时候首先通过接口拿到银行卡列表数据,包含了id和name。填充到PopBankUtils中
PopBankUtils popBankUtils = new PopBankUtils(MerchatRegistrationActivity.this, banks, tvBankName); popBankUtils.setListener(new PopBankUtils.onBankSelectListener() { @Override public void bankSelect(String bank_id) { bankId= bank_id; } });
然后通过点击控件触发:
popBankUtils.openPopBank();
这样就拿到了银行卡id,并把银行名称填充到了控件中,不仅大大减少了Activity复杂度,而且其他页面要用可以复用!最后效果图:
银行名称选择.gif
作者:Small_Cake
链接:https://www.jianshu.com/p/17ca61449329