提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言(相比于前一次发布的,整体还是要熟练很多了,代码基本上都有注释,在后期使用的时候如果还是有什么不太理解的地方可以在下面评论,看到后会一一答复)
效果
**相信做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弹框的封装,应为这个是之前用的相对比较多的地方,所以做了一个整理.(如有想要看的博客可以在下面提供,后续慢慢发布)