可扩展的popUpwindow
B: 实现子类: comm_bottom_popwindow_item.xml 其实就是定义一个linearlayout,根据具体的需求添加里面的item即可,同时通过一个自定义的接口监听item的点击就ok了
发布日期:2021-11-12 07:57:42
浏览次数:34
分类:技术文章
本文共 6801 字,大约阅读时间需要 22 分钟。
原文地址:http://blog.csdn.net/yaya_soft/article/details/40399727
通常我们使用popUpwindow的时候例如都是写一个布局文件然后设置到popUpwindow的自定义布局当中,但有时候我们可能有这样的需求,底部的取消按钮是一定的,但是上面的选项不是写死的两个或者三个而是更加我们的需求动态来决定的,这个时候我们不可能去写死一个布局了,那么就需要我们自定义一个pop,动态添加同时要可以监听各项的点击事件。。。
先说一下思路,我们先定义一个basepopwindow,来抽象几个公用方法,然后通过子类来实现
具体代码:
A:抽象基类
- public abstract class BasePopupWindow extends PopupWindow {
- protected View popRootView;
- public BasePopupWindow() {
- super();
- }
- public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public BasePopupWindow(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public BasePopupWindow(Context context) {
- super(context);
- }
- public BasePopupWindow(int width, int height) {
- super(width, height);
- }
- public BasePopupWindow(View contentView, int width, int height,
- boolean focusable) {
- super(contentView, width, height, focusable);
- }
- public BasePopupWindow(View contentView) {
- super(contentView);
- }
- public BasePopupWindow(View contentView, int width, int height){
- super(contentView, width, height,true);
- this.popRootView = contentView;
- setFocusable(true);
- setOutsideTouchable(true);
- setTouchable(true);
- ColorDrawable dw = new ColorDrawable(0xb0000000);
- this.setBackgroundDrawable(dw);
- setAnimationStyle(R.style.AnimBottom);
- initViews();
- initEvents();
- init();
- }
- public abstract void initViews();
- public abstract void initEvents();
- public abstract void init();
- public View findViewById(int id) {
- return popRootView.findViewById(id);
- }
里面加入我的一些说明:
- public class CommBottomPopWindow extends BasePopupWindow implements
- OnClickListener {
- private Button cancleBtn;
- private PopWindowListener listener;
- private LinearLayout mLayout;
- private Context mContext;
- private boolean isHasSubTitle = false;
- private LayoutInflater inflater;
- /**
- * 功能描述: 设置点击事件<br>
- * 〈功能详细描述〉
- * 点击的自定义回调接口
- */
- public void setPopListener(PopWindowListener listener) {
- this.listener = listener;
- }
- public CommBottomPopWindow(Context context) {
- //布局填充
- super((LayoutInflater.from(context).inflate(
- R.layout.comm_bottom_popwindow, null)),
- LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
- mContext = context;
- }
- /**
- * 功能描述:初始化小标题 <br>
- 顶部是否需要提示的小标题
- */
- public void initPopSubTitle(String notiTxt) {
- mLayout.addView(createItem(notiTxt, true));
- }
- /**
- * 功能描述: 初始化item<br>
- * 〈功能详细描述〉
- 动态添加的条目
- */
- public void initPopItem(List<String> list) {
- if (list == null || list.size() == 0) {
- return;
- }
- for (int i = 0; i < list.size(); i++) {
- String title = list.get(i);
- mLayout.addView(createItem(title, i, list.size()));
- }
- }
- private View createItem(String itemTxt, boolean isSubTitle) {
- return createItem(itemTxt, -1, -1, isSubTitle);
- }
- private View createItem(String itemTxt, final int index, int total) {
- return createItem(itemTxt, index, total, false);
- }
- /**
- * 功能描述: 创建item<br>
- * 〈功能详细描述〉
- *
- 创建具体的条目
- */
- private View createItem(String itemTxt, final int index, int total,
- boolean isSubTitle) {
- inflater = (LayoutInflater) mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.comm_bottom_popwindow_item, null);
- LinearLayout layout = (LinearLayout) view
- .findViewById(R.id.comm_popwindow_item_layout);
- TextView textView = (TextView) view
- .findViewById(R.id.comm_popwindow_item_txt);
- textView.setText(itemTxt);
- if (isSubTitle) {
- isHasSubTitle = true;
- layout.setBackgroundResource(R.drawable.selectpopwin_up);
- textView.setTextColor(ResUtil.getColor(R.color.color_999999));
- } else if (index == 0 && !isHasSubTitle) {
- layout.setBackgroundResource(R.drawable.btn_selectpopwin_up);
- } else if (index == total - 1) {
- layout.setBackgroundResource(R.drawable.btn_selectpopwin_down);
- } else {
- layout.setBackgroundResource(R.drawable.btn_camp_selpopwin_center);
- }
- view.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (index == -1) {
- return;
- }
- if (listener != null) {
- listener.onPopSelected(index);
- }
- }
- });
- return view;
- }
- @Override
- public void initViews() {
- mLayout = (LinearLayout) findViewById(R.id.comm_bottom_popwindow_layout);
- cancleBtn = (Button) findViewById(R.id.camp_pop_cancle);
- isHasSubTitle = false;
- }
- @Override
- public void initEvents() {
- cancleBtn.setOnClickListener(this);
- popRootView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dismiss();
- }
- });
- }
- @Override
- public void init() {
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.camp_pop_cancle:
- dismiss();
- break;
- default:
- break;
- }
- }
- /**
- * 功能描述: 显示pop window<br>
- * 〈功能详细描述〉
- */
- public void show(View view) {
- showAtLocation(view, Gravity.BOTTOM, 0, 0);
- }
- //回调接口定义
- public interface PopWindowListener {
- public void onPopSelected(int which);
- }
C:贴下布局
comm_bottom_popwindow.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="bottom"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- android:paddingTop="20dp" >
- <LinearLayout
- android:id="@+id/comm_bottom_popwindow_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- </LinearLayout>
- <Button
- android:id="@+id/camp_pop_cancle"
- android:layout_width="fill_parent"
- android:layout_height="@dimen/comm_padding_size_9"
- android:layout_marginBottom="15dp"
- android:layout_marginTop="15dp"
- android:background="@drawable/btn_selectpopwin_cancel"
- android:text="@string/camp_cancle"
- android:textColor="@color/text_blue"
- android:textSize="17sp" />
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/comm_popwindow_item_layout"
- android:layout_width="fill_parent"
- android:layout_height="@dimen/comm_padding_size_9"
- android:gravity="center"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/comm_popwindow_item_txt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/camp_balance_query"
- android:gravity="center"
- android:textColor="@color/text_blue"
- android:textSize="17sp" />
- </LinearLayout>
ps 用法如下:
- private void initPopWindow() {
- if (mPopWindow == null) {
- mPopWindow = new CommBottomPopWindow(getActivity());
- List<String> list = new ArrayList<>();
- list.add("test1");
- list.add("test2");
- // 带显示小标题,可加可不加
- mPopWindow.initPopSubTitle("sub title context");
- mPopWindow.initPopItem(list);
- mPopWindow.setPopListener(mPopListener);
- }
- // 显示pop window
- mPopWindow.show(view);
- }
转载地址:https://blog.csdn.net/happy_love1990/article/details/78327977 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月24日 07时57分12秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity中实现有限状态机FSM
2019-04-27
Unity中实现反弹
2019-04-27
U3D游戏开发框架(九)——事件序列
2019-04-27
Unity中解决“SetDestination“ can only be called on an active agent that has been placed on a NavMesh
2019-04-27
Unity中的刚体
2019-04-27
Unity中的坐标转换
2019-04-27
Unity中为什么不能对transform.position.x直接赋值?
2019-04-27
Unity中物体移动方法详解
2019-04-27
使用对象池优化性能
2019-04-27
Unity中的UI方案(基础版)
2019-04-27
Lua(一)——Lua介绍
2019-04-27
Lua(二)——环境安装
2019-04-27
Unity中父子物体的坑
2019-04-27
基础知识——进位制
2019-04-27
Lua(十二)——表
2019-04-27
Lua(十三)——模块与包
2019-04-27
Lua(四)——变量
2019-04-27
Lua(十四)——元表
2019-04-27
Lua(十五)——协同程序
2019-04-27
Lua(十六)——文件
2019-04-27