1.介绍:
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的,而与对话框有点不同,对话框的位置是相对比较固定,而PopupWindow得位置比较随意,对话框是非线程阻塞的,而PopupWindod是线程阻塞的。
2.使用:
定义一个PopupWindow的布局:popup_window_layout.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
card_view:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/pw_play_model"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88FFFFFF"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前播放"
android:textColor="@color/text_artist_color"
android:textSize="16sp"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/pd_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="(1)"
android:textColor="@color/text_size_color"
android:textSize="14sp"
card_view:layout_constraintStart_toEndOf="@+id/textView2"
card_view:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:src="@drawable/single_play_text"
android:id="@+id/pw_model_play_ib"
android:layout_width="70dp"
android:layout_marginLeft="5dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:background="@drawable/button_selector"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
card_view:layout_constraintBottom_toTopOf="@id/list_view_pw"
android:background="#22000000"
android:layout_width="match_parent"
android:layout_height="1dp"
tools:ignore="MissingConstraints" />
<ListView
android:id="@+id/list_view_pw"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toBottomOf="@+id/pw_model_play_ib" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx
2.在代码中加载弹窗:
public void showPopupWindow(View view){
if (songList == null) {
return;
}
//获取屏幕的宽和高
DisplayMetrics dm =context.getResources().getDisplayMetrics();
int width=dm.widthPixels;
int height=dm.widthPixels;
View contentView= LayoutInflater.from(context).inflate(R.layout.popup_window_layout,null);
listView = contentView.findViewById(R.id.list_view_pw);
playModelImageButton = contentView.findViewById(R.id.pw_model_play_ib);
final TextView countText = contentView.findViewById(R.id.pd_count);
countText.setText("("+songList.size()+")");
PopupWindowListViewAdapter adapter=new PopupWindowListViewAdapter(context,R.layout.popup_window_listview_item,songList);
adapter.setMusicBinder(musicBinder);
listView.setAdapter(adapter);
initPlayModelButton();
playModelImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置点击事件
modelTag++;
if (modelTag > 3) {
modelTag = 1;
}
musicBinder.setModelTag(modelTag);
initPlayModelButton();
}
});
adapter.setUpdateSizeTextListener(new PopupWindowListViewAdapter.UpdateSizeTextListener() {
@Override
public void updateSizeText() {
countText.setText("(" + songList.size() + ")");
}
});
PopupWindow popupWindow=new PopupWindow(contentView,width-60,height,true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
return false;
}
});
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
// 我觉得这里是API的一个bug
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
//设置动画
popupWindow.setAnimationStyle(R.style.MyPopupWIndow_Anim);
//设置在底部弹出
popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
//设置弹窗消失时点的事件,用于更新详情页的播放模式
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//其他页面不更新
if (isDetailMusicActivity) {
playModelUpdateListener.update();
}
}
});
Log.e(TAG, " 弹出窗口显示" );
}
3.效果图片:
image.png注意:
如果想点击弹窗以外的地方要弹窗消失,或者按Back键使弹窗消失,需要设置弹窗的背景。具体的原因看下面的参考链接。
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
如果弹窗中存在ListView,如果在一般情况下ListView的Item的点击事件是失效的,可以通过以下的两种方法设置。
1.设置popupwindow为聚焦:setFocusable(true)
2.在ListView的Adapter的getView方法中进行item点击事件处理
//因为API<19时会导致LitView的Item点击无法回调的问题因此在这里设置监听器
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateDataInfo.getINSTANCE().updateDataInfoListener.startMusicWhenClick(position);
//设置背景
notifyDataSetChanged();
Log.e(TAG, "onClick: 播放歌曲" + songList.get(position).getTitle());
}
});
弹窗显示的位置可以参考https://www.jianshu.com/p/6c32889e6377
参考链接:
https://www.cnblogs.com/mengdd/p/3569127.html
https://www.jianshu.com/p/c19b01b9e390
https://www.jianshu.com/p/1a009b964d00
网友评论