CommonPopup,自己写的通用的点击或者输入监听,在所在的控件位置下方弹出类似于搜索提示的界面,也可以传入接下来要执行的界面的绝对路径,做成点击跳转其他页面。
使用方法:直接new该类,然后填入参数(有几项必填否则报错,详情看程序源码)
然后触发事件中调用该类引用.show()方法显示
selectList是个String List,根据传入的下标拆分显示,如果需要添加跳转页面再set contextList也就是绝对路径
\
/**
* Created by hanzh on 2018/3/6.
* 当前类只能用于界面只有一个下拉选择框
* 使用时需要判断选择框是否是闭合状态
* 需要传入下拉列表数据,主线程,显示控件
popup窗口
private PopupWindow typeSelectPopup;
使用isShowing()检查popup窗口是否在显示状态
if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
typeSelectPopup.showAsDropDown(mSelectTv, 0, 10);
}
*/
public class CommonPopup {
// popup窗口
private PopupWindow typeSelectPopup;
// popup窗口里的ListView
private ListView mTypeLv;
// 下拉列表数据
private List<String> selectList;
//下拉列表适配器
private ArrayAdapter selecterAdapter;
private Boolean showChoose = false;
private Activity mContext;
//TextView选择框
private View mSelectTv;
//下拉列表的背景
private int itemBackground;
//设置点击事件跳转的页面
private List<String> contextList;
//子列表宽度
private int width;
//子列表高度
private int height;
//跳转页面是否被销毁
private Boolean finish = true;
/**
* 数据类型下拉选择框
*/
public void initSelectPopup() {
mTypeLv = new ListView(mContext);
// 设置适配器
selecterAdapter = new ArrayAdapter<>(mContext, R.layout.popup_text_item, selectList);
mTypeLv.setAdapter(selecterAdapter);
// 设置ListView点击事件监听
mTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 在这里获取item数据
String value = selectList.get(position);
if(showChoose){
// 把选择的数据展示对应的TextView上
TextView select =(TextView) mSelectTv;
select.setText(value);
}
if(null != contextList){
try {
Intent Intent = new Intent(mContext,Class.forName("文档路径"+contextList.get(position)));
Bundle Bundle = new Bundle();
Bundle.putString("oldText","");
Intent.putExtras(Bundle);
mContext.startActivity(Intent);
} catch (ClassNotFoundException e) {
Log.w("CommonPopup Exception","跳转页面未找到");
}
}
// 选择完后关闭popup窗口
typeSelectPopup.dismiss();
if(finish) {
mContext.finish();
}
}
});
typeSelectPopup = new PopupWindow(mTypeLv, 270, ActionBar.LayoutParams.WRAP_CONTENT, true);//在这里可以设置弹出来的界面宽和高
// 取得popup窗口的背景图片
Drawable drawable = ContextCompat.getDrawable(mContext, itemBackground);
typeSelectPopup.setBackgroundDrawable(drawable);
typeSelectPopup.setFocusable(true);
typeSelectPopup.setOutsideTouchable(true);
typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// 关闭popup窗口
typeSelectPopup.dismiss();
}
});
}
/**
*放入执行的主线程
* @param mContext
*/
public void setmContext(Activity mContext) { this.mContext = mContext;}
/**
* 放入点击事件的控件,用于定位
* @param mSelectTv
*/
public void setmSelectTv(View mSelectTv) { this.mSelectTv = mSelectTv; }
/**
* 是否将选中的子列表显示内容放入点击控件中
* @param showChoose
*/
public void showChoose(Boolean showChoose) { this.showChoose = showChoose; }
/**
* 填充下拉列表的显示项,一个arraylist数组,数组有多少个String就显示多少个下拉
* @param selectList
*/
public void setSelectList(List<String> selectList) {
this.selectList = selectList;
}
/**
* 设置下拉列表的背景图片(只能存放图片)
* @param itemBackground
*/
public void setItemBackground(int itemBackground) { this.itemBackground = itemBackground; }
/**
* 显示下拉,点击子列表之后收缩
*/
public void show() {
initSelectPopup();
if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
typeSelectPopup.showAsDropDown(mSelectTv, 0, 0);
}
}
/**
* 设置下拉列表跳转页面,和下拉列表显示list下标相对应
* @param contextList
*/
public void setContextList(List<String> contextList) { this.contextList = contextList; }
/**
* 设置子列表宽度
* @param width
*/
public void setWidth(int width) {
this.width = width;
}
/**
* 设置子列表高度
* @param height
*/
public void setHeight(int height) {
this.height = height;
}
/**
* 设置当前页面跳转之后是否被销毁,默认销毁
* @param finish
*/
public void setFinish(Boolean finish) { this.finish = finish; }
}
\