popupWindow弹窗的封装(带效果图)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言(相比于前一次发布的,整体还是要熟练很多了,代码基本上都有注释,在后期使用的时候如果还是有什么不太理解的地方可以在下面评论,看到后会一一答复)

效果

**相信做Android 有很多这样的场景,就是新增或者修改,提交等一些操作的时候都需要一个弹框,弹框可以在手机有限的页面上显得更加优雅,今天主要说的一个弹窗就是,比如新增一条数据的时候,关于这个数据的类型,我们点击它,里面会有多种类型提供给我们选择,今天就封装好这样一个弹窗**

一、popupWindow

**相比于Dialog,PopupWindow的位置比较随意,可以在任意位置显示,而Dialog相对固定,其次就是背景变暗的效果,PopupWindow可以轻松的定制背景,无需复杂的黑科技,而且可以在任意地方调用popupWindow进行取消操作.**

二、使用步骤

直接上代码

1. 导入recyclerview依赖

  implementation 'com.android.support:recyclerview-v7:28.0.0'

2.这个是弹框里面的一个集合的适配器

package com.dingler.pipeline.adapter;
	
	import android.content.Context;
	import android.view.LayoutInflater;
	import android.view.View;
	import android.view.ViewGroup;
	import android.widget.TextView;
	
	import androidx.annotation.NonNull;
	import androidx.recyclerview.widget.RecyclerView;
	
	import com.dingler.pipeline.R;
	
	import java.util.List;

	public class StrAdapter extends RecyclerView.Adapter<StrAdapter.Holder> {
	    List<String> list;
	    Context context;
	
	    public StrAdapter(List<String> list, Context context) {
	        this.list = list;
	        this.context = context;
	    }
	
	    @NonNull
	    @Override
	    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
	        View view = LayoutInflater.from(context).inflate(R.layout.itemstr, parent, false);
	        return new Holder(view);
	    }
	
	    @Override
	    public void onBindViewHolder(@NonNull Holder holder, int position) {
	        holder.textView.setText(list.get(position));
	
	        holder.itemView.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                strCallBack.onClick(position);
	            }
	        });
	
	
	    }
	
	    @Override
	    public int getItemCount() {
	        return list.size();
	    }
	
	    class Holder extends RecyclerView.ViewHolder{
	        TextView textView;
	        public Holder(@NonNull View itemView) {
	            super(itemView);
	            textView=itemView.findViewById(R.id.textstr);
	        }
	    }
	
	    public interface StrCallBack{
	        void onClick(int position);
	    }
	
	    public StrCallBack strCallBack;
	
	    public void setStrCallBack(StrCallBack strCallBack){
	        this.strCallBack=strCallBack;
	    }
	}

3.popupWindow的封装类

package com.dingler.pipeline.util;
	
	import android.content.Context;
	import android.view.Gravity;
	import android.view.LayoutInflater;
	import android.view.View;
	import android.view.WindowManager;
	import android.widget.Button;
	import android.widget.PopupWindow;
	import android.widget.TextView;
	
	import androidx.appcompat.app.ActionBar;
	import androidx.recyclerview.widget.LinearLayoutManager;
	import androidx.recyclerview.widget.RecyclerView;
	
	import com.dingler.pipeline.R;
	import com.dingler.pipeline.adapter.StrAdapter;
	
	import java.util.List;
	
	public class PopupWindowUtils {
	    private int number=0;
	    public void   popuwindow(Context context, List<String> list, String textView_title, TextView textView_name){
	        PopupWindow popupWindow;
	        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        View vPopupWindow = inflater.inflate(R.layout.popuwindow_utils, null, false);//引入弹窗布局
	        popupWindow = new PopupWindow(vPopupWindow, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
	        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
	        popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
	
	        //获取弹框id
	        Button button = vPopupWindow.findViewById(R.id.popuwindow_btn_false);
	        RecyclerView recyclerView = vPopupWindow.findViewById(R.id.popuwindow_recyc);
	        TextView textView = vPopupWindow.findViewById(R.id.popuwindow_text);
	
	        //获取传递过来的数据添加到弹窗的标题展示
	        textView.setText(textView_title);
	
	        //recyclerview列表舒适化(这里用的是线性列表,还有表格和瀑布)
	        LinearLayoutManager manager = new LinearLayoutManager(context);
	        manager.setOrientation(RecyclerView.VERTICAL);
	        recyclerView.setLayoutManager(manager);
	
	        //new集合适配器,将集合数据闯过去,后面是上下文
	        StrAdapter strAdapter = new StrAdapter(list, context);
	        //展示列表
	        recyclerView.setAdapter(strAdapter);
	        //recyclerview的接口回调方法,提供点击事件
	        strAdapter.setStrCallBack(new StrAdapter.StrCallBack() {
	            @Override
	            public void onClick(int position) {
	                textView_name.setText(list.get(position));
	                number = position+1;
	                popupWindow.dismiss();
	
	                popCallBack.onClick(number);
	            }
	        });
	
	        //点击取消按钮取消弹框
	        button.setOnClickListener(v -> popupWindow.dismiss());
	        //点击弹框外部取消弹窗
	        setPopupDis(popupWindow,vPopupWindow);
	
	        //引入依附的布局
	        View parentView = LayoutInflater.from(context).inflate(R.layout.popuwindow_utils, null);
	        popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
	        popupWindow.setTouchable(true);
	    }
	
	
	
	    //取消弹框和背景
	    private static void setPopupDis(PopupWindow popupWindow, View view) {
	        view.setOnTouchListener((view1, motionEvent) -> {
	            if (popupWindow.isShowing()) {
	                popupWindow.dismiss();
	            }
	            return false;
	        });
	    }
	
	
	
	    public interface PopCallBack{
	        void onClick(int position);
	    }
	
	    public PopCallBack popCallBack;
	
	    public void setPopCallBack(PopCallBack popCallBack){
	        this.popCallBack=popCallBack;
	    }
	
	}

4.popupWindow的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#4D676767"
    android:gravity="bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:background="#fff"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_marginBottom="@dimen/dp_10"
                android:layout_marginTop="@dimen/dp_10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:id="@+id/popuwindow_text"/>

        </LinearLayout>

        <LinearLayout
            android:background="#EAEAEA"
            android:layout_width="match_parent"
            android:layout_height="1dp">
        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_marginBottom="@dimen/dp_10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/popuwindow_recyc"/>

        <Button
            android:id="@+id/popuwindow_btn_false"
            android:background="@drawable/shape_login2"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dp_10"
            android:text="取消"/>

    </LinearLayout>


</LinearLayout> 

5、代码调用,在点击事件里面写这个就好

List<String> list = new ArrayList<>();
	                list.add("首页");
	                list.add("管网");
	                list.add("我的");
	                PopupWindowUtils popupWindowUtils = new PopupWindowUtils();
	                //参数传递的是,上下文,提供选择的list集合,弹框的标题,需要在那个TextView控件展示
	                popupWindowUtils.popuwindow(this, list, "测试选框", testText);
	                popupWindowUtils.setPopCallBack(new PopupWindowUtils.PopCallBack() {
	                    @Override
	                    public void onClick(int position) {
	                        //应为考虑到根据类型选择需要传一个类型的int数据给后端,所以这里有一个接口回调
	                        Log.e("aaa",""+position);
	                    }
	                });

总结,这个就是今天说的popupWindow弹框的封装,应为这个是之前用的相对比较多的地方,所以做了一个整理.(如有想要看的博客可以在下面提供,后续慢慢发布)

请添加图片描述
请添加图片描述
请添加图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现该功能需要先创建一个按钮,然后在按钮的点击事件中创建一个 PopupWindow 对象。在 PopupWindow 中,可以设置一个布局文件,该文件会展示在中。接下来,可以为中的按钮设置一个点击事件,通过点击事件再创建一个 PopupWindow 对象,展示新的层。 具体实现步骤如下: 1. 创建一个按钮,设置按钮的点击事件 2. 在点击事件中使用 PopupWindow 类创建一个对象,并且设置它的布局文件 3. 获取布局文件中的子视,并在子视中为按钮设置点击事件 4. 在按钮的点击事件中创建一个新的 PopupWindow 对象,然后为它设置另一个布局文件,展示新的层 关于具体的代码实现,你可以参考下面的示例代码: ``` kotlin // 创建一个按钮 val button = Button(this) button.text = "点击" button.setOnClickListener(View.OnClickListener { // 创建一个 PopupWindow 对象 val popupWindow = PopupWindow(this) // 设置的布局文件 val view = layoutInflater.inflate(R.layout.popup_window, null) popupWindow.contentView = view // 获取布局文件中的子视 val popupButton = view.findViewById<Button>(R.id.popup_button) popupButton.setOnClickListener(View.OnClickListener { // 创建一个新的 PopupWindow 对象 val popupWindow2 = PopupWindow(this) // 设置的布局文件 val view2 = layoutInflater.inflate(R.layout.popup_window_2, null) popupWindow2.contentView = view2 // 显示 popupWindow2.showAsDropDown(popupButton) }) // 显示 popupWindow.showAsDropDown(button) }) ``` 在代码中,首先创建一个按钮,并设置它的点击事件。在点击事件中,创建了一个 PopupWindow 对象,然后为它设置了一个布局文件。接着,使用 findViewById() 方法获取布局文件中的子视,并为它设置了一个点击事件。在点击事件中,又创建了一个新的 PopupWindow 对象,并设置了一个新的布局文件。最后,分别通过 showAsDropDown() 方法显示了两个层。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值