点击控件弹出Popwindow下拉框
程序员文章站
2023-12-28 12:01:04
...
//设置科室不能编辑i
et_apply_keshi.setKeyListener(null);
//设置职称不能编辑
et_apply_zhicheng.setKeyListener(null);
if (o instanceof KeShiBean) {
Log.e("科室接口:", ((KeShiBean) o).getMessage());
for (int i=0;i<((KeShiBean) o).getResult().size();i++){
iv_apply_keshi.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
if (((KeShiBean) o).getMessage().equals("查询成功")) {
//创建窗口
final ListPopupWindow listPopupWindow=new ListPopupWindow(getBaseContext());
//创建适配器 适配器继承 BaseAdapter 参数为集合数据和上下文
PopupWindow_Adapter popupWindow_adapter = new PopupWindow_Adapter(((KeShiBean) o).getResult(), getBaseContext());
//给listPopupWindow设置适配器
listPopupWindow.setAdapter(popupWindow_adapter);
//设置listPopupWindow的宽
listPopupWindow.setWidth(300);
//设置listPopupWindow的高
listPopupWindow.setHeight(400);
//设置背景颜色
listPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.white));
//设置listPopupWindow在哪个控件下面
listPopupWindow.setAnchorView(sp_apply_keshi);
listPopupWindow.setModal(true);
//设置下拉列表的对齐方式。Gravity.START表示与参照控件左侧对齐,Gravity.END表示与参照控件右侧对齐。注意:该函数只在4.4.2及以上版本中使用。
listPopupWindow.setDropDownGravity(Gravity.START);
//设置listPopupWindow子视图点击事件 改变输入框内容
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
sp_apply_keshi.setText(((KeShiBean) o).getResult().get(i).getDepartmentName());
//进行存值 科室的id
KeShi = ((KeShiBean) o).getResult().get(i).getId();
Log.e("科室id:",KeShi+"");
//收回listPopupWindow展示
listPopupWindow.dismiss();
}
});
//展示listPopupWindow
listPopupWindow.show();
}
}
});
}
**
Adapter
**
public class PopupWindow_Adapter extends BaseAdapter {
private List<KeShiBean.ResultBean> list;
private Context context;
public PopupWindow_Adapter(List<KeShiBean.ResultBean> result, Context baseContext) {
list=result;
context=baseContext;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null){
view= LayoutInflater.from(context).inflate(R.layout.popupwindows_adappter,null);
holder=new ViewHolder();
holder.textView=view.findViewById(R.id.tv_pop_xiala);
view.setTag(holder);
}else {
holder= (ViewHolder) view.getTag();
}
holder.textView.setText(list.get(i).getDepartmentName());
return view;
}
class ViewHolder{
TextView textView;
}
}