观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

版权声明

本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/12746009.html

本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。

前言

  这里有一些我实际使用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

posted on 2020-04-21 17:18  观心静  阅读(617)  评论(0编辑  收藏  举报