Android-基类PopupWindow创建及使用

BasePopupWindow

主要功能

  • 实现了弹出时背景半透明效果

    实现

BasePopupWindow.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class BasePopupWindow extends PopupWindow {
private Activity activity;

public BasePopupWindow(Activity activity) {
this.activity = activity;
}

/**
* 设置页面的透明度
* @param bgAlpha 1表示不透明
*/
public void setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgAlpha;
if (bgAlpha == 1) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
}
activity.getWindow().setAttributes(lp);
}

public void onDismiss() {
if (activity != null) {
setBackgroundAlpha(1f);
}
}
}

例子

实现选择车辆的弹出选择框

TruckPop.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hhkj.mcb.R;
import com.hhkj.mcb.common.ui.bean.CarBean;
import com.hhkj.mcb.seller.callbuy.adapter.TruckAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* @ClassName: TruckPop
* @Description:
* @Author: pinguoooo
* @Date: 2020/12/11 13:57
*/
public class TruckPop extends BasePopupWindow {
@BindView(R.id.rv_truck)
RecyclerView rvTruck;

private final View view;
private Activity activity;
private List<CarBean> carBeanList;
private TruckAdapter truckAdapter;
private OnSelectedListener onSelectedListener;
private int selectIndex = 0;

public TruckPop(Activity activity, int height, List<CarBean> carBeanList) {
this(activity,height,carBeanList,0);
}

public TruckPop(Activity activity, int height, List<CarBean> carBeanList, int selectIndex) {
super(activity);
this.activity = activity;
this.carBeanList = carBeanList;
this.selectIndex = selectIndex;

setOutsideTouchable(true);
setFocusable(true);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(height);
setBackgroundDrawable(new ColorDrawable(0x30000000));
view = LayoutInflater.from(activity).inflate(R.layout.pop_truck, null);
setContentView(view);
ButterKnife.bind(this, view);
initView();
}



private void initView() {
view.setOnClickListener(view -> {
dismiss();
});

rvTruck.setLayoutManager(new LinearLayoutManager(activity));
rvTruck.setAdapter(truckAdapter = new TruckAdapter(R.layout.truck_item,selectIndex));
View headerView = LayoutInflater.from(activity).inflate(R.layout.truck_item, null);
TextView header = headerView.findViewById(R.id.tv_carNumber);
headerView.setBackgroundColor(Color.WHITE);
header.setText("全部");
header.setTextColor(Color.parseColor("#31C863"));
truckAdapter.addHeaderView(headerView);
truckAdapter.setList(carBeanList);
truckAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
CarBean carBean = (CarBean) adapter.getData().get(position);
truckAdapter.setSelectIndex(position);
if (onSelectedListener != null) {
onSelectedListener.onSelect(carBean, position);
}
}
});

}

public interface OnSelectedListener {
void onSelect(CarBean carBean, int position);
}

public OnSelectedListener getOnSelectedListener() {
return onSelectedListener;
}

public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
this.onSelectedListener = onSelectedListener;
}
}

pop_truck.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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="wrap_content">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_truck"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

TruckAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import android.graphics.Color;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hhkj.mcb.R;
import com.hhkj.mcb.common.ui.bean.CarBean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;

/**
* @ClassName: PickUpAdapter
* @Description:
* @Author: pinguoooo
* @Date: 2020/11/9 11:29
*/
public class TruckAdapter extends BaseQuickAdapter<CarBean, BaseViewHolder> {
private int selectIndex;


public TruckAdapter(int layoutResId, @Nullable List<CarBean> data, int selectIndex) {
super(layoutResId, data);
this.selectIndex = selectIndex;
}

public TruckAdapter(int layoutResId, int selectIndex) {
super(layoutResId);
this.selectIndex = selectIndex;
ToastUtils.showShort(selectIndex);
}

@Override
protected void convert(@NotNull BaseViewHolder baseViewHolder, CarBean carBean) {
//因为添加了列表头部,在获取位置索引时需要 -1
if (selectIndex == baseViewHolder.getLayoutPosition() - 1) {
//LogUtils.d("layoutPosition:" + baseViewHolder.getLayoutPosition());
baseViewHolder.itemView.setBackgroundColor(Color.WHITE);
baseViewHolder.setTextColor(R.id.tv_carNumber, Color.parseColor("#31C863"));
} else {
baseViewHolder.itemView.setBackgroundColor(Color.parseColor("#F7F7F7"));
baseViewHolder.setTextColor(R.id.tv_carNumber, Color.parseColor("#666666"));
}
}


public void setSelectIndex(int selectIndex) {
this.selectIndex = selectIndex;
//LogUtils.d("selectIndex:" + selectIndex);
notifyDataSetChanged();
}

public int getSelectIndex() {
return selectIndex;
}
}

效果如下

img

Author

Pinguoooo

Posted on

2021-06-25

Updated on

2022-04-08

Licensed under

Comentarios

:D 一言句子获取中...

Loading...Wait a Minute!