3.自定义控件:PopupWindow小案例
2019-05-28 本文已影响0人
BusyBunny
案例展示:
PopupWindow.gif1. 思路很简单:
在PopupWindow中添加ListView即可;
popupWindow=new PopupWindow(listView,et_print.getWidth(),400);
popupWindow.showAsDropDown(et_print,0,-5);
2. 最主要有1个关键点:
PopupWindow界面中如果存在Button或者ImageButton等按钮控件会抢夺所在Layout的焦点,导致其他区域点击不生效,解决办法是==在其所在的布局界面==添加以下例句:
android:descendantFocusability="blocksDescendants"
并设置PopupWindow的焦点为true
//获取焦点,点击事件生效
popupWindow.setFocusable(true);
3.PopupWindow点击空白处取消:
//点击空白处取消PopupWindow
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
popupWindow.dismiss();
}
});
3.完整代码:
public class ListActivity extends AppCompatActivity {
private EditText et_print;
private ListView listView;
private List<String> dataList;
private PopupWindow popupWindow;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
initViews();
initData();
}
private void initData() {
dataList=new ArrayList<>();
for (int i=0;i<5;i++){
dataList.add((1000+i)+"");
}
}
private void showPopupWindow() {
initListViews();
popupWindow=new PopupWindow(listView,et_print.getWidth(),400);
//获取焦点,点击事件生效
popupWindow.setFocusable(true);
//显示在指定位置下:
popupWindow.showAsDropDown(et_print,0,-5);
//点击空白处取消PopupWindow
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
popupWindow.dismiss();
}
});
}
private void initListViews() {
listView=new ListView(this);
listView.setAdapter(new ListAdapter(dataList));
//去除分割线:
listView.setDividerHeight(0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
et_print.setText(dataList.get(position));
popupWindow.dismiss();
}
});
}
private void initViews() {
et_print=findViewById(R.id.et_print);
ImageButton ib_select=findViewById(R.id.ib_select);
ib_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow();
}
});
}
class ListAdapter extends BaseAdapter{
private List<String> dataList;
public ListAdapter(List<String> dataList){
this.dataList=dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder=null;
if (convertView==null){
view= LayoutInflater.from(ListActivity.this).inflate(R.layout.item_list,null,false);
viewHolder=new ViewHolder();
viewHolder.tv_text=view.findViewById(R.id.tv_text);
viewHolder.ib_delete=view.findViewById(R.id.ib_delete);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder= (ViewHolder) convertView.getTag();
}
viewHolder.tv_text.setText(dataList.get(position));
viewHolder.ib_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataList.remove(position);
notifyDataSetChanged();
if (dataList.size()==0){
popupWindow.dismiss();
}
}
});
return view;
}
class ViewHolder{
TextView tv_text;
ImageButton ib_delete;
}
}
}