实现效果:
单独封装PopWindow类,点击类目内容回填,弹窗渐变,弹窗后主界面失去焦点并变暗
PopWindow类抽取
新建一个ShowPopWindow.java popwindow样式
<?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="wrap_content"
android:background="@drawable/shape_listview"
android:orientation="vertical">
<ListView
android:id="@+id/lv_ck"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:cacheColorHint="#00000000"
android:overScrollMode="never" />
<TextView
android:id="@+id/cancel_pop"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shape_corner"--这是圆角矩形样式
android:gravity="center"
android:text="取消"
android:textColor="@color/black" />
</LinearLayout>
shape_listview 渐变样式
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@color/white"
android:endColor="@color/slateGray"
android:angle="270"
/>
<!-- <solid android:color="@color/white" />-->
<corners
android:bottomRightRadius="4dip"
android:bottomLeftRadius="4dip"
android:topLeftRadius="4dip"
android:topRightRadius="4dip"
/>
<corners android:radius="20dp"/>
<stroke android:width="1dp" android:color="@color/black" />
</shape>
public class ShowPopWindow {
private final Context mContext;
private final Window mWindow;
private final ArrayAdapter mAdapter;
private final int res;
private PopupWindow popupWindow;
private View popView;
private int clickPos = -1;
public ShowPopWindow(Context context, int convertViewResource, Window window, ArrayAdapter adapter) {
this.mContext = context;
this.mWindow = window;
this.res = convertViewResource;
this.mAdapter = adapter;
}
public void initPopWindow() {
popView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow, null);
View rootView = LayoutInflater.from(mContext).inflate(res, null);
TextView cancle = popView.findViewById(R.id.cancel_pop);
ListView lv_ck = popView.findViewById(R.id.lv_ck);
lv_ck.setAdapter(mAdapter);
popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(null);
popupWindow.setAnimationStyle(com.sab.libui.baseui.R.style.PopAnimation);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
WindowManager.LayoutParams lp = mWindow.getAttributes();
lp.alpha = 0.7f;
mWindow.setAttributes(lp);
cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickPos = -1;
lp.alpha = 1f;
popupWindow.dismiss();
}
});
lv_ck.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
clickPos = position;
lp.alpha = 1f;
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, -50);
}
public PopupWindow getPopWin() {
return popupWindow;
}
public View getPopView() {
return popView;
}
public int getClickPos() {
return clickPos;
}
}
调用方法及点击回填事件
Window mWindow=getWindow();
showPopWindow = new ShowPopWindow(GoodsMoveActivity.this, 使用该组件的界面, mWindow, adapter);
showPopWindow.initPopWindow();
PopupWindow popWin = showPopWindow.getPopWin();
View popView = showPopWindow.getPopView();
popWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = mWindow.getAttributes();
lp.alpha = 1f;
mWindow.setAttributes(lp);
int clickPos = showPopWindow.getClickPos();
if (clickPos != -1) {
String item = adapter.getItem(clickPos);
vb.etCk.setText(item);
}
}
});
|