今天遇到一个需求,就是EdittText + PopupWindow 用户在EdittTex输入后,PopupWindow筛选展示出来,先看效果图:
如上,我贴重要的代码,然后Demo在末尾
/**
* 初始化PopupWindow 关键代码
*/
private void initPopuWindow(){
if (!init){
View view1 = mActivity.getLayoutInflater().inflate(R.layout.popup_options, null);
listView = (ListView) view1.findViewById(R.id.list);
carAdapter = new ConfigurationFilterPopWindAdapter(mActivity, carStringList);
listView.setAdapter(carAdapter);
carShipNoPopWindow = new PopupWindow(view1, width, ViewGroup.LayoutParams.WRAP_CONTENT, true);
carShipNoPopWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));//背景色,半透明
carShipNoPopWindow.setFocusable(false);
carShipNoPopWindow.setOutsideTouchable(false);
carShipNoPopWindow.setAnimationStyle(R.style.animation_push_from_bottom);
carShipNoPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
carShipNoPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
init = true;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mActivity, carStringList.get(position), Toast.LENGTH_SHORT).show();
carShipNoPopWindow.dismiss();
}
});
}
}
异步筛选信息
/**
*作者:GaoXiaoXiong
*创建时间:2017/7/10
*注释描述:车牌号Runnable,异步刷新界面
*
* 关键代码
*/
Runnable carChangeRunnable = new Runnable() {
@Override
public void run() {
String data = etCarShipNo.getText().toString();
if (etCarShipNo.getText().length()==0)
return;
if (carShipNoPopWindow!=null&&!carShipNoPopWindow.isShowing()){
carShipNoPopWindow.showAsDropDown(etCarShipNo,0,-3);
}
if (carShipNoPopWindow==null){
width = etCarShipNo.getWidth();
initPopuWindow();
carShipNoPopWindow.showAsDropDown(etCarShipNo,0,-3);
}
carStringList.clear();//先要清空,不然会叠加
getNewData("10", data,newCarString);//获取更新数据
carAdapter.notifyDataSetChanged();//更新
}
};