zoukankan      html  css  js  c++  java
  • Android开发 PopupWindow开发的一些例子

    前言

      这里有一些我实际使用PopupWindow开发的一些例子,分享大家开拓思路使用。此篇博客只有代码例子,无其他入门讲解,如果你需要学习,请参考我的另一篇博客:https://www.cnblogs.com/guanxinjing/p/10156153.html

    例子1

    类似长按文本弹出的处理文本的复制,黏贴,剪切,这种迷你对话框。代码较多,我放到github上了

    github地址:https://github.com/z3880341/buttonDialog

    例子2

    效果图

    没有PopupWindow对话框时:

    PopupWindow对话框出现时:

    dialog_family_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">
    
        <ImageView
            android:id="@+id/line1"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:background="@color/white"
            android:paddingLeft="20dp"
            android:src="@drawable/line_black"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <TextView
            android:id="@+id/not_family"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_family_1"
            android:drawablePadding="13dp"
            android:gravity="center_vertical|left"
            android:paddingLeft="14dp"
            android:paddingTop="14dp"
            android:paddingBottom="14dp"
            android:text="@string/no_family_created_yet"
            android:textColor="@color/color_2C"
            android:textSize="16sp"
            android:visibility="gone"
            android:background="@color/white"
            app:layout_constraintTop_toBottomOf="@id/line1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/family_list_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/ColorWhite"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/not_family" />
    
        <ImageView
            android:id="@+id/line2"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:background="@color/white"
            android:paddingLeft="20dp"
            android:src="@drawable/line_black"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/family_list_recyclerview" />
    
        <View
            android:id="@+id/translucent_bg"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/color_80000000"
            app:layout_constraintTop_toBottomOf="@id/line2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    
        <TextView
            android:id="@+id/family_list"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_family_3"
            android:drawablePadding="13dp"
            android:gravity="center_vertical|left"
            android:paddingLeft="14dp"
            android:paddingTop="14dp"
            android:paddingBottom="14dp"
            android:text="@string/family_list"
            android:textColor="@color/color_2C"
            android:textSize="16sp"
            android:background="@drawable/bg_family_list"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/line2" />
        
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    java

    public class FamilyListDialog extends PopupWindow {
        private View mView;
        private TextView mNotFamily;
        private RecyclerView mFamilyListRecyclerview;
        private TextView mFamilyList;
        private View mTranslucentBg;
        private FamilyListDialogAdapter mFamilyListDialogAdapter;
        private FamilyListDialogListener mListener;
        private int mHeightPixels;
        private List<FamilyBean> mFamilyBeanList;
    
        public FamilyListDialog(@NotNull Context context) {
            super();
            DisplayMetrics outMetrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
            setWidth(outMetrics.widthPixels);
            mHeightPixels = outMetrics.heightPixels;
            setHeight(mHeightPixels);
            mView = LayoutInflater.from(context).inflate(R.layout.dialog_family_list, null, false);
            mNotFamily = (TextView) mView.findViewById(R.id.not_family);
            mFamilyListRecyclerview = (RecyclerView) mView.findViewById(R.id.family_list_recyclerview);
            mFamilyList = (TextView) mView.findViewById(R.id.family_list);
            mTranslucentBg = (View) mView.findViewById(R.id.translucent_bg);
            adaptiveRecyclerViewHeight(5, context);
            mFamilyListDialogAdapter = new FamilyListDialogAdapter();
            mFamilyListRecyclerview.setAdapter(mFamilyListDialogAdapter);
            setContentView(mView);
            initListener();
        }
    
        @Override
        public void showAsDropDown(View anchor) {
            if(Build.VERSION.SDK_INT >= 24) {
                Rect rect = new Rect();
                anchor.getGlobalVisibleRect(rect);
                DisplayMetrics outMetrics = new DisplayMetrics();
                Context context = anchor.getContext();
                ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
                int h = outMetrics.heightPixels - rect.bottom;
                setHeight(h);
            }
            super.showAsDropDown(anchor);
        }
    
        private void initListener() {
            mTranslucentBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            mNotFamily.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onClickNotFamily();
                    }
    
                }
            });
            mFamilyList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onClickFamilyList(mFamilyBeanList);
                    }
    
                }
            });
            mFamilyListDialogAdapter.setOnItemClickListener(new FamilyListDialogAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(FamilyBean clickFamilyBean, boolean isConfirm) {
                    if (mListener != null) {
                        mListener.onClickFamilyListItem(clickFamilyBean, isConfirm);
                    }
                }
            });
        }
    
        public void setData(List<FamilyBean> list) {
            mFamilyBeanList = list;
            if (list == null || list.isEmpty()) {
                mFamilyListDialogAdapter.clearData();
                mFamilyListRecyclerview.setVisibility(View.GONE);
                mNotFamily.setVisibility(View.VISIBLE);
                return;
            }
            mNotFamily.setVisibility(View.GONE);
            mFamilyListRecyclerview.setVisibility(View.VISIBLE);
            mFamilyListDialogAdapter.refreshData(list);
        }
    
        /**
         * 自适应列表View在到指定数量item后固定高度,
         *
         * @param targetNum
         */
        private void adaptiveRecyclerViewHeight(int targetNum, Context context) {
            mFamilyListRecyclerview.setLayoutManager(new LinearLayoutManager(context) {
                @Override
                public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
                    int count = state.getItemCount();
                    if (count > 0) {
                        if (count > targetNum) {
                            count = targetNum;
                        }
                        int realHeight = 0;
                        int realWidth = 0;
                        for (int i = 0; i < count; i++) {
                            View view = recycler.getViewForPosition(0);
                            if (view != null) {
                                measureChild(view, widthSpec, heightSpec);
                                int measuredWidth = View.MeasureSpec.getSize(widthSpec);
                                int measuredHeight = view.getMeasuredHeight();
                                realWidth = realWidth > measuredWidth ? realWidth : measuredWidth;
                                realHeight = realHeight + measuredHeight;
                            }
                        }
                        setMeasuredDimension(realWidth, realHeight);
                    } else {
                        super.onMeasure(recycler, state, widthSpec, heightSpec);
                    }
                }
            });
        }
    
        /**
         * 销毁,主要释放接口
         */
        public void destroy() {
            mListener = null;
        }
    
        /**
         * 设置监听器
         *
         * @param mListener
         */
        public void setListener(FamilyListDialogListener mListener) {
            this.mListener = mListener;
        }
    
        public interface FamilyListDialogListener {
            /**
             * 点击没有家庭
             */
            void onClickNotFamily();
    
            /**
             * 点击家庭列表
             */
            void onClickFamilyList(List<FamilyBean> familyBeanList);
    
            /**
             * 点击列表item
             *
             * @param clickFamilyBean 被点击选择的家庭
             * @param isConfirm       是否已经加入此家庭
             */
            void onClickFamilyListItem(FamilyBean clickFamilyBean, boolean isConfirm);
    
        }
    
        /**
         * 对话框列表内部适配器类
         */
        private static class FamilyListDialogAdapter extends RecyclerView.Adapter<FamilyListDialogAdapter.ViewHolder> {
            private List<FamilyBean> mList = new ArrayList<>();
            private OnItemClickListener mOnItemClickListener;
    
            private void refreshData(List<FamilyBean> list) {
                mList.clear();
                mList.addAll(list);
                notifyDataSetChanged();
    
            }
    
            private void clearData() {
                mList.clear();
                notifyDataSetChanged();
            }
    
            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_family_list_dialog, parent, false);
                ViewHolder viewHolder = new ViewHolder(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            int position = viewHolder.getAdapterPosition();
                            if (position == RecyclerView.NO_POSITION) {
                                return;
                            }
                            boolean isConfirm = true;
                            for (FamilyBean.MembersBean membersBean : mList.get(position).getMembers()) {
                                if (membersBean.getUserId() == UserDataSP.getInstance().getUserId()) {//在成员列表里查找到当前用户id
                                    isConfirm = membersBean.isIsConfirm();
                                    break;
                                }
                            }
                            mOnItemClickListener.OnItemClick(mList.get(position), isConfirm);
                        }
                    }
                });
                return viewHolder;
            }
    
            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                FamilyBean familyItem = mList.get(position);
                holder.familyName.setText(familyItem.getName());
                for (FamilyBean.MembersBean membersBean : familyItem.getMembers()) {
                    if (membersBean.getUserId() == UserDataSP.getInstance().getUserId()) {//在成员列表里查找到当前用户id
                        if (!membersBean.isIsConfirm()) {
                            holder.status.setText(R.string.waiting_to_join);
                            return;
                        }
                    }
                }
                holder.status.setText("");
            }
    
            @Override
            public int getItemCount() {
                return mList.size();
            }
    
            public void setOnItemClickListener(OnItemClickListener listener) {
                mOnItemClickListener = listener;
    
            }
    
            private class ViewHolder extends RecyclerView.ViewHolder {
                View rootView;
                TextView familyName;
                TextView status;
    
                private ViewHolder(@NonNull View itemView) {
                    super(itemView);
                    rootView = itemView;
                    familyName = (TextView) rootView.findViewById(R.id.family_name);
                    status = (TextView) rootView.findViewById(R.id.status);
                }
            }
    
            public interface OnItemClickListener {
                /**
                 * @param clickFamilyBean 被点击选择的家庭
                 * @param isConfirm       是否已经加入此家庭
                 */
                void OnItemClick(FamilyBean clickFamilyBean, boolean isConfirm);
            }
    
        }
    }

    End

  • 相关阅读:
    Golang调用windows下的dll动态库中的函数
    Golang 编译成 DLL 文件
    go 通过nginx代理后获取用户ip
    idea教程--Maven 骨架介绍
    idea教程--快速插入依赖
    Java开发名词解释
    Android Studio安装
    Java基础--Eclipse关联Java源码
    【排序基础】3、随机生成排序算法 测试用例
    【排序基础】2、使用模板(泛型)编写选择排序算法
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/12746009.html
Copyright © 2011-2022 走看看