FilterPopupWindow.java
public class FilterPopupWindow extends PopupWindow {
private Context context;
private RecyclerView recyclerView;
List<String> mList;
BaseQuickAdapter mAdapter;
;
public FilterPopupWindow(Context context, @NotNull List<String> list,RelativeLayout view,int spanCount,String textStr) {
super(context);
this.context = context;
this.mList = list;
initPPP(view,spanCount,textStr);
}
private OnFilterPopupWindowListener onFilterPopupWindowListener;
public void setOnFilterPopupWindowListener(OnFilterPopupWindowListener onFilterPopupWindowListener) {
this.onFilterPopupWindowListener = onFilterPopupWindowListener;
}
public interface OnFilterPopupWindowListener {
void getText(String string,int i);
}
private void initPPP(RelativeLayout view,int spanCount,String textStr) {
View inflate = View.inflate(context, R.layout.ppp_order_filter, null);
mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.ppp_order_filter_child_item,mList){
@Override
protected void convert(BaseViewHolder helper, String item) {
TextView titleTv = helper.getView(R.id.ppp_order_filter_child_item_title);
titleTv.setGravity(Gravity.CENTER);
if(textStr.equals(item)){
titleTv.setActivated(true);
}
titleTv.setText(item);
titleTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = titleTv.getText().toString();
if (onFilterPopupWindowListener != null){
onFilterPopupWindowListener.getText(text,helper.getAdapterPosition());
}
dismiss();
}
});
}
};
recyclerView = inflate.findViewById(R.id.ppp_order_filter_recycler);
recyclerView.setLayoutManager(new GridLayoutManager(context, spanCount));
recyclerView.setAdapter(mAdapter);
setWidth(view.getWidth()-10);
setContentView(inflate);
setFocusable(true);
setTouchable(true);
setOutsideTouchable(true);
setBackgroundDrawable(new ColorDrawable());
}
}
ppp_order_filter.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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:padding="0dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ppp_order_filter_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:paddingRight="@dimen/px5dp"
android:paddingLeft="@dimen/px5dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ppp_order_filter_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:alpha="0.3"
android:background="#000000"
android:orientation="horizontal" />
</LinearLayout>
ppp_order_filter_child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ppp_order_filter_child_item_title"
android:layout_width="match_parent"
android:layout_height="@dimen/px35dp"
android:gravity="center"
android:background="@drawable/bg_order_filter_css"
android:text="普通"
android:textSize="@dimen/px12sp"
android:textColor="@drawable/bg_order_filter_text_css">
</TextView>
bg_order_filter_css.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<shape>
<corners android:radius="2dp" />
<solid android:color="@color/centiment_date_selected_color" />
<stroke android:width="1dp" android:color="@color/colorAccent" />
</shape>
</item>
<item>
<shape android:state_activated="false">
<corners android:radius="2dp" />
<solid android:color="@color/centiment_date_selected_color" />
<stroke android:width="1dp" android:color="@color/bg_loading" />
</shape>
</item>
</selector>
bg_order_filter_text_css.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_activated="true" />
<item android:color="#333333" />
</selector>
在activity里使用,radio是点击出现弹窗的控件,也可以是TextView或者其他,传入这个参数是为了做选中的效果。
FilterPopupWindow orderFilterPopupWindow ;
private void setPopupWindow( List<String> list1, RadioButton radio, int spanCount) {
orderFilterPopupWindow = new FilterPopupWindow(this, list1, realFilter,spanCount,radio.getText().toString());
orderFilterPopupWindow.showAsDropDown(realFilter);
orderFilterPopupWindow.setOnFilterPopupWindowListener(new FilterPopupWindow.OnFilterPopupWindowListener() {
@Override
public void getText(String string, int position) {
orderFilterPopupWindow.dismiss();
if (string == null){
return;
}
}
orderFilterPopupWindow.dismiss();
}
});
}
效果图
选择前:
选择后