用来记录,便于查找
先上图
第一种情况:
显示在某个view下面,并占满下面的全部布局
private void statusPopupWindow ( final TextView mTextView, final List< SpeedOption > mList, final int index ) {
int[] ps = new int[2];
//得到view到屏幕顶部的宽度和高度
popup_line.getLocationInWindow( ps );
LayoutInflater relativeLayout = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
View view = relativeLayout.inflate( R.layout.popup_speed_to_choice, null );
mPopupWindow = new PopupWindow( view,
RelativeLayout.LayoutParams.MATCH_PARENT, SystemTools.getScreenHeigh( this ) - ps[1] - popup_line.getHeight() );
RecyclerView titleRecyclerView = view.findViewById( R.id.title );
titleRecyclerView.setLayoutManager( new LinearLayoutManager( this ) );
CommonAdapter< SpeedOption > titleAdapter;
titleRecyclerView.setAdapter( titleAdapter = new CommonAdapter< SpeedOption >( this, mList, R.layout.item_speed_title ) {
@Override
public void convert ( ViewHolder helper, final SpeedOption item ) {
helper.setText( R.id.title, item.getName() );
}
} );
RecyclerView typeRecyclerView = view.findViewById( R.id.type );
typeRecyclerView.setLayoutManager( new LinearLayoutManager( this ) );
// 使其聚集
mPopupWindow.setFocusable( true );
// 设置允许在外点击消失
mPopupWindow.setOutsideTouchable( true );
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
mPopupWindow.setBackgroundDrawable( new BitmapDrawable() );
mPopupWindow.setOnDismissListener( () -> {
} );
titleAdapter.setOnItemClickLitener( ( view1, position ) -> {
CommonAdapter< SpeedOption > typeAdapter;
typeRecyclerView.setAdapter( typeAdapter = new CommonAdapter< SpeedOption >( SpeedToSearchActivity.this, mList.get( position ).getList(), R.layout.item_speed_content ) {
@Override
public void convert ( ViewHolder helper, SpeedOption item ) {
helper.setText( R.id.content, item.getName() );
helper.getView( R.id.content ).setOnClickListener( v1 -> {
ToastUtil.toast( mList.get( position ).getName() + item.getName() );
mPopupWindow.dismiss();
} );
}
} );
} );
if ( Build.VERSION.SDK_INT != Build.VERSION_CODES.N ) {
//只有24这个版本有问题,源码的问题
mPopupWindow.showAsDropDown( popup_line );
} else {
int[] location = new int[2];
popup_line.getLocationOnScreen( location );
int y = location[1];
mPopupWindow.showAtLocation( popup_line, Gravity.NO_GRAVITY, 0, y + mTextView.getHeight() );
}
}
布局文件:
popup_speed_to_choice.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/white"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
/>
</LinearLayout>
其中一个子布局:
item_speed_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/size_dp_40"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="第一个"
android:textColor="@color/font_color"
android:textSize="@dimen/text_size_14"
/>
<ImageView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/size_dp_15"
android:layout_marginStart="@dimen/size_dp_15"
android:layout_toEndOf="@+id/title"
android:layout_toRightOf="@+id/title"
android:contentDescription="@null"
android:src="@mipmap/circle_question"
/>
</RelativeLayout>
调用:
statusPopupWindow( industry, mSpeedOption, 2 );
第二种情况:
显示在某个view下面,并根据实际情况占下面布局空间
代码跟上面一样,但是计算高度的时候设置成
mPopupWindow = new PopupWindow( view,
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
还有个背景阴影,可以用
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
dismiss()恢复就行
不过上面哪种背景阴影是除popupWindow外,整个布局背景都变化。
另一种情况是用一个控件,通过View的gone和visible来控制,比较灵活
<FrameLayout
android:id="@+id/mask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mask"
android:visibility="visible"
/>
<color name="mask">#60000000</color>