ListPopupWindow.java revision b1818e83f4a81bc4e4e30b99bb48830415be731b
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import android.content.Context; 20import android.database.DataSetObserver; 21import android.graphics.Rect; 22import android.graphics.drawable.Drawable; 23import android.os.Handler; 24import android.util.AttributeSet; 25import android.util.Log; 26import android.view.KeyEvent; 27import android.view.MotionEvent; 28import android.view.View; 29import android.view.ViewGroup; 30import android.view.ViewParent; 31import android.view.View.MeasureSpec; 32import android.view.View.OnTouchListener; 33 34/** 35 * A ListPopupWindow anchors itself to a host view and displays a 36 * list of choices. 37 * 38 * <p>ListPopupWindow contains a number of tricky behaviors surrounding 39 * positioning, scrolling parents to fit the dropdown, interacting 40 * sanely with the IME if present, and others. 41 * 42 * @see android.widget.AutoCompleteTextView 43 * @see android.widget.Spinner 44 */ 45public class ListPopupWindow { 46 private static final String TAG = "ListPopupWindow"; 47 private static final boolean DEBUG = false; 48 49 /** 50 * This value controls the length of time that the user 51 * must leave a pointer down without scrolling to expand 52 * the autocomplete dropdown list to cover the IME. 53 */ 54 private static final int EXPAND_LIST_TIMEOUT = 250; 55 56 private Context mContext; 57 private PopupWindow mPopup; 58 private ListAdapter mAdapter; 59 private DropDownListView mDropDownList; 60 61 private int mDropDownHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 62 private int mDropDownWidth = ViewGroup.LayoutParams.WRAP_CONTENT; 63 private int mDropDownHorizontalOffset; 64 private int mDropDownVerticalOffset; 65 66 private boolean mDropDownAlwaysVisible = false; 67 private boolean mForceIgnoreOutsideTouch = false; 68 69 private View mPromptView; 70 private int mPromptPosition = POSITION_PROMPT_ABOVE; 71 72 private DataSetObserver mObserver; 73 74 private View mDropDownAnchorView; 75 76 private Drawable mDropDownListHighlight; 77 78 private AdapterView.OnItemClickListener mItemClickListener; 79 private AdapterView.OnItemSelectedListener mItemSelectedListener; 80 81 private final ResizePopupRunnable mResizePopupRunnable = new ResizePopupRunnable(); 82 private final PopupTouchInterceptor mTouchInterceptor = new PopupTouchInterceptor(); 83 private final PopupScrollListener mScrollListener = new PopupScrollListener(); 84 private final ListSelectorHider mHideSelector = new ListSelectorHider(); 85 private Runnable mShowDropDownRunnable; 86 87 private Handler mHandler = new Handler(); 88 89 private Rect mTempRect = new Rect(); 90 91 private boolean mModal; 92 93 /** 94 * The provided prompt view should appear above list content. 95 * 96 * @see #setPromptPosition(int) 97 * @see #getPromptPosition() 98 * @see #setPromptView(View) 99 */ 100 public static final int POSITION_PROMPT_ABOVE = 0; 101 102 /** 103 * The provided prompt view should appear below list content. 104 * 105 * @see #setPromptPosition(int) 106 * @see #getPromptPosition() 107 * @see #setPromptView(View) 108 */ 109 public static final int POSITION_PROMPT_BELOW = 1; 110 111 /** 112 * Alias for {@link ViewGroup.LayoutParams#MATCH_PARENT}. 113 * If used to specify a popup width, the popup will match the width of the anchor view. 114 * If used to specify a popup height, the popup will fill available space. 115 */ 116 public static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT; 117 118 /** 119 * Alias for {@link ViewGroup.LayoutParams#WRAP_CONTENT}. 120 * If used to specify a popup width, the popup will use the width of its content. 121 */ 122 public static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; 123 124 /** 125 * Mode for {@link #setInputMethodMode(int)}: the requirements for the 126 * input method should be based on the focusability of the popup. That is 127 * if it is focusable than it needs to work with the input method, else 128 * it doesn't. 129 */ 130 public static final int INPUT_METHOD_FROM_FOCUSABLE = PopupWindow.INPUT_METHOD_FROM_FOCUSABLE; 131 132 /** 133 * Mode for {@link #setInputMethodMode(int)}: this popup always needs to 134 * work with an input method, regardless of whether it is focusable. This 135 * means that it will always be displayed so that the user can also operate 136 * the input method while it is shown. 137 */ 138 public static final int INPUT_METHOD_NEEDED = PopupWindow.INPUT_METHOD_NEEDED; 139 140 /** 141 * Mode for {@link #setInputMethodMode(int)}: this popup never needs to 142 * work with an input method, regardless of whether it is focusable. This 143 * means that it will always be displayed to use as much space on the 144 * screen as needed, regardless of whether this covers the input method. 145 */ 146 public static final int INPUT_METHOD_NOT_NEEDED = PopupWindow.INPUT_METHOD_NOT_NEEDED; 147 148 /** 149 * Create a new, empty popup window capable of displaying items from a ListAdapter. 150 * Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}. 151 * 152 * @param context Context used for contained views. 153 */ 154 public ListPopupWindow(Context context) { 155 this(context, null, 0, 0); 156 } 157 158 /** 159 * Create a new, empty popup window capable of displaying items from a ListAdapter. 160 * Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}. 161 * 162 * @param context Context used for contained views. 163 * @param attrs Attributes from inflating parent views used to style the popup. 164 */ 165 public ListPopupWindow(Context context, AttributeSet attrs) { 166 this(context, attrs, com.android.internal.R.attr.listPopupWindowStyle, 0); 167 } 168 169 /** 170 * Create a new, empty popup window capable of displaying items from a ListAdapter. 171 * Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}. 172 * 173 * @param context Context used for contained views. 174 * @param attrs Attributes from inflating parent views used to style the popup. 175 * @param defStyleAttr Default style attribute to use for popup content. 176 */ 177 public ListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) { 178 this(context, attrs, defStyleAttr, 0); 179 } 180 181 /** 182 * Create a new, empty popup window capable of displaying items from a ListAdapter. 183 * Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}. 184 * 185 * @param context Context used for contained views. 186 * @param attrs Attributes from inflating parent views used to style the popup. 187 * @param defStyleAttr Style attribute to read for default styling of popup content. 188 * @param defStyleRes Style resource ID to use for default styling of popup content. 189 */ 190 public ListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 191 mContext = context; 192 mPopup = new PopupWindow(context, attrs, defStyleAttr, defStyleRes); 193 } 194 195 /** 196 * Sets the adapter that provides the data and the views to represent the data 197 * in this popup window. 198 * 199 * @param adapter The adapter to use to create this window's content. 200 */ 201 public void setAdapter(ListAdapter adapter) { 202 if (mObserver == null) { 203 mObserver = new PopupDataSetObserver(); 204 } else if (mAdapter != null) { 205 mAdapter.unregisterDataSetObserver(mObserver); 206 } 207 mAdapter = adapter; 208 if (mAdapter != null) { 209 adapter.registerDataSetObserver(mObserver); 210 } 211 212 if (mDropDownList != null) { 213 mDropDownList.setAdapter(mAdapter); 214 } 215 } 216 217 /** 218 * Set where the optional prompt view should appear. The default is 219 * {@link #POSITION_PROMPT_ABOVE}. 220 * 221 * @param position A position constant declaring where the prompt should be displayed. 222 * 223 * @see #POSITION_PROMPT_ABOVE 224 * @see #POSITION_PROMPT_BELOW 225 */ 226 public void setPromptPosition(int position) { 227 mPromptPosition = position; 228 } 229 230 /** 231 * @return Where the optional prompt view should appear. 232 * 233 * @see #POSITION_PROMPT_ABOVE 234 * @see #POSITION_PROMPT_BELOW 235 */ 236 public int getPromptPosition() { 237 return mPromptPosition; 238 } 239 240 /** 241 * Set whether this window should be modal when shown. 242 * 243 * <p>If a popup window is modal, it will receive all touch and key input. 244 * If the user touches outside the popup window's content area the popup window 245 * will be dismissed. 246 * 247 * @param modal {@code true} if the popup window should be modal, {@code false} otherwise. 248 */ 249 public void setModal(boolean modal) { 250 mModal = true; 251 mPopup.setFocusable(modal); 252 } 253 254 /** 255 * Returns whether the popup window will be modal when shown. 256 * 257 * @return {@code true} if the popup window will be modal, {@code false} otherwise. 258 */ 259 public boolean isModal() { 260 return mModal; 261 } 262 263 /** 264 * Forces outside touches to be ignored. Normally if {@link #isDropDownAlwaysVisible()} is 265 * false, we allow outside touch to dismiss the dropdown. If this is set to true, then we 266 * ignore outside touch even when the drop down is not set to always visible. 267 * 268 * @hide Used only by AutoCompleteTextView to handle some internal special cases. 269 */ 270 public void setForceIgnoreOutsideTouch(boolean forceIgnoreOutsideTouch) { 271 mForceIgnoreOutsideTouch = forceIgnoreOutsideTouch; 272 } 273 274 /** 275 * Sets whether the drop-down should remain visible under certain conditions. 276 * 277 * The drop-down will occupy the entire screen below {@link #getAnchorView} regardless 278 * of the size or content of the list. {@link #getBackground()} will fill any space 279 * that is not used by the list. 280 * 281 * @param dropDownAlwaysVisible Whether to keep the drop-down visible. 282 * 283 * @hide Only used by AutoCompleteTextView under special conditions. 284 */ 285 public void setDropDownAlwaysVisible(boolean dropDownAlwaysVisible) { 286 mDropDownAlwaysVisible = dropDownAlwaysVisible; 287 } 288 289 /** 290 * @return Whether the drop-down is visible under special conditions. 291 * 292 * @hide Only used by AutoCompleteTextView under special conditions. 293 */ 294 public boolean isDropDownAlwaysVisible() { 295 return mDropDownAlwaysVisible; 296 } 297 298 /** 299 * Sets the operating mode for the soft input area. 300 * 301 * @param mode The desired mode, see 302 * {@link android.view.WindowManager.LayoutParams#softInputMode} 303 * for the full list 304 * 305 * @see android.view.WindowManager.LayoutParams#softInputMode 306 * @see #getSoftInputMode() 307 */ 308 public void setSoftInputMode(int mode) { 309 mPopup.setSoftInputMode(mode); 310 } 311 312 /** 313 * Returns the current value in {@link #setSoftInputMode(int)}. 314 * 315 * @see #setSoftInputMode(int) 316 * @see android.view.WindowManager.LayoutParams#softInputMode 317 */ 318 public int getSoftInputMode() { 319 return mPopup.getSoftInputMode(); 320 } 321 322 /** 323 * Sets a drawable to use as the list item selector. 324 * 325 * @param selector List selector drawable to use in the popup. 326 */ 327 public void setListSelector(Drawable selector) { 328 mDropDownListHighlight = selector; 329 } 330 331 /** 332 * @return The background drawable for the popup window. 333 */ 334 public Drawable getBackground() { 335 return mPopup.getBackground(); 336 } 337 338 /** 339 * Sets a drawable to be the background for the popup window. 340 * 341 * @param d A drawable to set as the background. 342 */ 343 public void setBackgroundDrawable(Drawable d) { 344 mPopup.setBackgroundDrawable(d); 345 } 346 347 /** 348 * Set an animation style to use when the popup window is shown or dismissed. 349 * 350 * @param animationStyle Animation style to use. 351 */ 352 public void setAnimationStyle(int animationStyle) { 353 mPopup.setAnimationStyle(animationStyle); 354 } 355 356 /** 357 * Returns the animation style that will be used when the popup window is 358 * shown or dismissed. 359 * 360 * @return Animation style that will be used. 361 */ 362 public int getAnimationStyle() { 363 return mPopup.getAnimationStyle(); 364 } 365 366 /** 367 * Returns the view that will be used to anchor this popup. 368 * 369 * @return The popup's anchor view 370 */ 371 public View getAnchorView() { 372 return mDropDownAnchorView; 373 } 374 375 /** 376 * Sets the popup's anchor view. This popup will always be positioned relative to 377 * the anchor view when shown. 378 * 379 * @param anchor The view to use as an anchor. 380 */ 381 public void setAnchorView(View anchor) { 382 mDropDownAnchorView = anchor; 383 } 384 385 /** 386 * @return The horizontal offset of the popup from its anchor in pixels. 387 */ 388 public int getHorizontalOffset() { 389 return mDropDownHorizontalOffset; 390 } 391 392 /** 393 * Set the horizontal offset of this popup from its anchor view in pixels. 394 * 395 * @param offset The horizontal offset of the popup from its anchor. 396 */ 397 public void setHorizontalOffset(int offset) { 398 mDropDownHorizontalOffset = offset; 399 } 400 401 /** 402 * @return The vertical offset of the popup from its anchor in pixels. 403 */ 404 public int getVerticalOffset() { 405 return mDropDownVerticalOffset; 406 } 407 408 /** 409 * Set the vertical offset of this popup from its anchor view in pixels. 410 * 411 * @param offset The vertical offset of the popup from its anchor. 412 */ 413 public void setVerticalOffset(int offset) { 414 mDropDownVerticalOffset = offset; 415 } 416 417 /** 418 * @return The width of the popup window in pixels. 419 */ 420 public int getWidth() { 421 return mDropDownWidth; 422 } 423 424 /** 425 * Sets the width of the popup window in pixels. Can also be {@link #MATCH_PARENT} 426 * or {@link #WRAP_CONTENT}. 427 * 428 * @param width Width of the popup window. 429 */ 430 public void setWidth(int width) { 431 mDropDownWidth = width; 432 } 433 434 /** 435 * Sets the width of the popup window by the size of its content. The final width may be 436 * larger to accommodate styled window dressing. 437 * 438 * @param width Desired width of content in pixels. 439 */ 440 public void setContentWidth(int width) { 441 Drawable popupBackground = mPopup.getBackground(); 442 if (popupBackground != null) { 443 mDropDownWidth = popupBackground.getIntrinsicWidth() + width; 444 } 445 } 446 447 /** 448 * @return The height of the popup window in pixels. 449 */ 450 public int getHeight() { 451 return mDropDownHeight; 452 } 453 454 /** 455 * Sets the height of the popup window in pixels. Can also be {@link #MATCH_PARENT}. 456 * 457 * @param height Height of the popup window. 458 */ 459 public void setHeight(int height) { 460 mDropDownHeight = height; 461 } 462 463 /** 464 * Sets a listener to receive events when a list item is clicked. 465 * 466 * @param clickListener Listener to register 467 * 468 * @see ListView#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener) 469 */ 470 public void setOnItemClickListener(AdapterView.OnItemClickListener clickListener) { 471 mItemClickListener = clickListener; 472 } 473 474 /** 475 * Sets a listener to receive events when a list item is selected. 476 * 477 * @param selectedListener Listener to register. 478 * 479 * @see ListView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) 480 */ 481 public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener selectedListener) { 482 mItemSelectedListener = selectedListener; 483 } 484 485 /** 486 * Set a view to act as a user prompt for this popup window. Where the prompt view will appear 487 * is controlled by {@link #setPromptPosition(int)}. 488 * 489 * @param prompt View to use as an informational prompt. 490 */ 491 public void setPromptView(View prompt) { 492 boolean showing = isShowing(); 493 if (showing) { 494 removePromptView(); 495 } 496 mPromptView = prompt; 497 if (showing) { 498 show(); 499 } 500 } 501 502 /** 503 * Post a {@link #show()} call to the UI thread. 504 */ 505 public void postShow() { 506 mHandler.post(mShowDropDownRunnable); 507 } 508 509 /** 510 * Show the popup list. If the list is already showing, this method 511 * will recalculate the popup's size and position. 512 */ 513 public void show() { 514 int height = buildDropDown(); 515 516 int widthSpec = 0; 517 int heightSpec = 0; 518 519 boolean noInputMethod = isInputMethodNotNeeded(); 520 521 if (mPopup.isShowing()) { 522 if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) { 523 // The call to PopupWindow's update method below can accept -1 for any 524 // value you do not want to update. 525 widthSpec = -1; 526 } else if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) { 527 widthSpec = getAnchorView().getWidth(); 528 } else { 529 widthSpec = mDropDownWidth; 530 } 531 532 if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) { 533 // The call to PopupWindow's update method below can accept -1 for any 534 // value you do not want to update. 535 heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT; 536 if (noInputMethod) { 537 mPopup.setWindowLayoutMode( 538 mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ? 539 ViewGroup.LayoutParams.MATCH_PARENT : 0, 0); 540 } else { 541 mPopup.setWindowLayoutMode( 542 mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ? 543 ViewGroup.LayoutParams.MATCH_PARENT : 0, 544 ViewGroup.LayoutParams.MATCH_PARENT); 545 } 546 } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) { 547 heightSpec = height; 548 } else { 549 heightSpec = mDropDownHeight; 550 } 551 552 mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible); 553 554 mPopup.update(getAnchorView(), mDropDownHorizontalOffset, 555 mDropDownVerticalOffset, widthSpec, heightSpec); 556 } else { 557 if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) { 558 widthSpec = ViewGroup.LayoutParams.MATCH_PARENT; 559 } else { 560 if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) { 561 mPopup.setWidth(getAnchorView().getWidth()); 562 } else { 563 mPopup.setWidth(mDropDownWidth); 564 } 565 } 566 567 if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) { 568 heightSpec = ViewGroup.LayoutParams.MATCH_PARENT; 569 } else { 570 if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) { 571 mPopup.setHeight(height); 572 } else { 573 mPopup.setHeight(mDropDownHeight); 574 } 575 } 576 577 mPopup.setWindowLayoutMode(widthSpec, heightSpec); 578 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); 579 580 // use outside touchable to dismiss drop down when touching outside of it, so 581 // only set this if the dropdown is not always visible 582 mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible); 583 mPopup.setTouchInterceptor(mTouchInterceptor); 584 mPopup.showAsDropDown(getAnchorView(), 585 mDropDownHorizontalOffset, mDropDownVerticalOffset); 586 mDropDownList.setSelection(ListView.INVALID_POSITION); 587 588 if (!mModal || mDropDownList.isInTouchMode()) { 589 clearListSelection(); 590 } 591 if (!mModal) { 592 mHandler.post(mHideSelector); 593 } 594 } 595 } 596 597 /** 598 * Dismiss the popup window. 599 */ 600 public void dismiss() { 601 mPopup.dismiss(); 602 removePromptView(); 603 mPopup.setContentView(null); 604 mDropDownList = null; 605 } 606 607 /** 608 * Set a listener to receive a callback when the popup is dismissed. 609 * 610 * @param listener Listener that will be notified when the popup is dismissed. 611 */ 612 public void setOnDismissListener(PopupWindow.OnDismissListener listener) { 613 mPopup.setOnDismissListener(listener); 614 } 615 616 private void removePromptView() { 617 if (mPromptView != null) { 618 final ViewParent parent = mPromptView.getParent(); 619 if (parent instanceof ViewGroup) { 620 final ViewGroup group = (ViewGroup) parent; 621 group.removeView(mPromptView); 622 } 623 } 624 } 625 626 /** 627 * Control how the popup operates with an input method: one of 628 * {@link #INPUT_METHOD_FROM_FOCUSABLE}, {@link #INPUT_METHOD_NEEDED}, 629 * or {@link #INPUT_METHOD_NOT_NEEDED}. 630 * 631 * <p>If the popup is showing, calling this method will take effect only 632 * the next time the popup is shown or through a manual call to the {@link #show()} 633 * method.</p> 634 * 635 * @see #getInputMethodMode() 636 * @see #show() 637 */ 638 public void setInputMethodMode(int mode) { 639 mPopup.setInputMethodMode(mode); 640 } 641 642 /** 643 * Return the current value in {@link #setInputMethodMode(int)}. 644 * 645 * @see #setInputMethodMode(int) 646 */ 647 public int getInputMethodMode() { 648 return mPopup.getInputMethodMode(); 649 } 650 651 /** 652 * Set the selected position of the list. 653 * Only valid when {@link #isShowing()} == {@code true}. 654 * 655 * @param position List position to set as selected. 656 */ 657 public void setSelection(int position) { 658 DropDownListView list = mDropDownList; 659 if (isShowing() && list != null) { 660 list.mListSelectionHidden = false; 661 list.setSelection(position); 662 if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) { 663 list.setItemChecked(position, true); 664 } 665 } 666 } 667 668 /** 669 * Clear any current list selection. 670 * Only valid when {@link #isShowing()} == {@code true}. 671 */ 672 public void clearListSelection() { 673 final DropDownListView list = mDropDownList; 674 if (list != null) { 675 // WARNING: Please read the comment where mListSelectionHidden is declared 676 list.mListSelectionHidden = true; 677 list.hideSelector(); 678 list.requestLayout(); 679 } 680 } 681 682 /** 683 * @return {@code true} if the popup is currently showing, {@code false} otherwise. 684 */ 685 public boolean isShowing() { 686 return mPopup.isShowing(); 687 } 688 689 /** 690 * @return {@code true} if this popup is configured to assume the user does not need 691 * to interact with the IME while it is showing, {@code false} otherwise. 692 */ 693 public boolean isInputMethodNotNeeded() { 694 return mPopup.getInputMethodMode() == INPUT_METHOD_NOT_NEEDED; 695 } 696 697 /** 698 * Perform an item click operation on the specified list adapter position. 699 * 700 * @param position Adapter position for performing the click 701 * @return true if the click action could be performed, false if not. 702 * (e.g. if the popup was not showing, this method would return false.) 703 */ 704 public boolean performItemClick(int position) { 705 if (isShowing()) { 706 if (mItemClickListener != null) { 707 final DropDownListView list = mDropDownList; 708 final View child = list.getChildAt(position - list.getFirstVisiblePosition()); 709 final ListAdapter adapter = list.getAdapter(); 710 mItemClickListener.onItemClick(list, child, position, adapter.getItemId(position)); 711 } 712 return true; 713 } 714 return false; 715 } 716 717 /** 718 * @return The currently selected item or null if the popup is not showing. 719 */ 720 public Object getSelectedItem() { 721 if (!isShowing()) { 722 return null; 723 } 724 return mDropDownList.getSelectedItem(); 725 } 726 727 /** 728 * @return The position of the currently selected item or {@link ListView#INVALID_POSITION} 729 * if {@link #isShowing()} == {@code false}. 730 * 731 * @see ListView#getSelectedItemPosition() 732 */ 733 public int getSelectedItemPosition() { 734 if (!isShowing()) { 735 return ListView.INVALID_POSITION; 736 } 737 return mDropDownList.getSelectedItemPosition(); 738 } 739 740 /** 741 * @return The ID of the currently selected item or {@link ListView#INVALID_ROW_ID} 742 * if {@link #isShowing()} == {@code false}. 743 * 744 * @see ListView#getSelectedItemId() 745 */ 746 public long getSelectedItemId() { 747 if (!isShowing()) { 748 return ListView.INVALID_ROW_ID; 749 } 750 return mDropDownList.getSelectedItemId(); 751 } 752 753 /** 754 * @return The View for the currently selected item or null if 755 * {@link #isShowing()} == {@code false}. 756 * 757 * @see ListView#getSelectedView() 758 */ 759 public View getSelectedView() { 760 if (!isShowing()) { 761 return null; 762 } 763 return mDropDownList.getSelectedView(); 764 } 765 766 /** 767 * @return The {@link ListView} displayed within the popup window. 768 * Only valid when {@link #isShowing()} == {@code true}. 769 */ 770 public ListView getListView() { 771 return mDropDownList; 772 } 773 774 /** 775 * Filter key down events. By forwarding key up events to this function, 776 * views using non-modal ListPopupWindow can have it handle key selection of items. 777 * 778 * @param keyCode keyCode param passed to the host view's onKeyDown 779 * @param event event param passed to the host view's onKeyDown 780 * @return true if the event was handled, false if it was ignored. 781 * 782 * @see #setModal(boolean) 783 */ 784 public boolean onKeyDown(int keyCode, KeyEvent event) { 785 // when the drop down is shown, we drive it directly 786 if (isShowing()) { 787 // the key events are forwarded to the list in the drop down view 788 // note that ListView handles space but we don't want that to happen 789 // also if selection is not currently in the drop down, then don't 790 // let center or enter presses go there since that would cause it 791 // to select one of its items 792 if (keyCode != KeyEvent.KEYCODE_SPACE 793 && (mDropDownList.getSelectedItemPosition() >= 0 794 || (keyCode != KeyEvent.KEYCODE_ENTER 795 && keyCode != KeyEvent.KEYCODE_DPAD_CENTER))) { 796 int curIndex = mDropDownList.getSelectedItemPosition(); 797 boolean consumed; 798 799 final boolean below = !mPopup.isAboveAnchor(); 800 801 final ListAdapter adapter = mAdapter; 802 803 boolean allEnabled; 804 int firstItem = Integer.MAX_VALUE; 805 int lastItem = Integer.MIN_VALUE; 806 807 if (adapter != null) { 808 allEnabled = adapter.areAllItemsEnabled(); 809 firstItem = allEnabled ? 0 : 810 mDropDownList.lookForSelectablePosition(0, true); 811 lastItem = allEnabled ? adapter.getCount() - 1 : 812 mDropDownList.lookForSelectablePosition(adapter.getCount() - 1, false); 813 } 814 815 if ((below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex <= firstItem) || 816 (!below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && curIndex >= lastItem)) { 817 // When the selection is at the top, we block the key 818 // event to prevent focus from moving. 819 clearListSelection(); 820 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); 821 show(); 822 return true; 823 } else { 824 // WARNING: Please read the comment where mListSelectionHidden 825 // is declared 826 mDropDownList.mListSelectionHidden = false; 827 } 828 829 consumed = mDropDownList.onKeyDown(keyCode, event); 830 if (DEBUG) Log.v(TAG, "Key down: code=" + keyCode + " list consumed=" + consumed); 831 832 if (consumed) { 833 // If it handled the key event, then the user is 834 // navigating in the list, so we should put it in front. 835 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 836 // Here's a little trick we need to do to make sure that 837 // the list view is actually showing its focus indicator, 838 // by ensuring it has focus and getting its window out 839 // of touch mode. 840 mDropDownList.requestFocusFromTouch(); 841 show(); 842 843 switch (keyCode) { 844 // avoid passing the focus from the text view to the 845 // next component 846 case KeyEvent.KEYCODE_ENTER: 847 case KeyEvent.KEYCODE_DPAD_CENTER: 848 case KeyEvent.KEYCODE_DPAD_DOWN: 849 case KeyEvent.KEYCODE_DPAD_UP: 850 return true; 851 } 852 } else { 853 if (below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { 854 // when the selection is at the bottom, we block the 855 // event to avoid going to the next focusable widget 856 if (curIndex == lastItem) { 857 return true; 858 } 859 } else if (!below && keyCode == KeyEvent.KEYCODE_DPAD_UP && 860 curIndex == firstItem) { 861 return true; 862 } 863 } 864 } 865 } 866 867 return false; 868 } 869 870 /** 871 * Filter key down events. By forwarding key up events to this function, 872 * views using non-modal ListPopupWindow can have it handle key selection of items. 873 * 874 * @param keyCode keyCode param passed to the host view's onKeyUp 875 * @param event event param passed to the host view's onKeyUp 876 * @return true if the event was handled, false if it was ignored. 877 * 878 * @see #setModal(boolean) 879 */ 880 public boolean onKeyUp(int keyCode, KeyEvent event) { 881 if (isShowing() && mDropDownList.getSelectedItemPosition() >= 0) { 882 boolean consumed = mDropDownList.onKeyUp(keyCode, event); 883 if (consumed) { 884 switch (keyCode) { 885 // if the list accepts the key events and the key event 886 // was a click, the text view gets the selected item 887 // from the drop down as its content 888 case KeyEvent.KEYCODE_ENTER: 889 case KeyEvent.KEYCODE_DPAD_CENTER: 890 dismiss(); 891 break; 892 } 893 } 894 return consumed; 895 } 896 return false; 897 } 898 899 /** 900 * Filter pre-IME key events. By forwarding {@link View#onKeyPreIme(int, KeyEvent)} 901 * events to this function, views using ListPopupWindow can have it dismiss the popup 902 * when the back key is pressed. 903 * 904 * @param keyCode keyCode param passed to the host view's onKeyPreIme 905 * @param event event param passed to the host view's onKeyPreIme 906 * @return true if the event was handled, false if it was ignored. 907 * 908 * @see #setModal(boolean) 909 */ 910 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 911 if (keyCode == KeyEvent.KEYCODE_BACK && isShowing()) { 912 // special case for the back key, we do not even try to send it 913 // to the drop down list but instead, consume it immediately 914 final View anchorView = mDropDownAnchorView; 915 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { 916 anchorView.getKeyDispatcherState().startTracking(event, this); 917 return true; 918 } else if (event.getAction() == KeyEvent.ACTION_UP) { 919 anchorView.getKeyDispatcherState().handleUpEvent(event); 920 if (event.isTracking() && !event.isCanceled()) { 921 dismiss(); 922 return true; 923 } 924 } 925 } 926 return false; 927 } 928 929 /** 930 * <p>Builds the popup window's content and returns the height the popup 931 * should have. Returns -1 when the content already exists.</p> 932 * 933 * @return the content's height or -1 if content already exists 934 */ 935 private int buildDropDown() { 936 ViewGroup dropDownView; 937 int otherHeights = 0; 938 939 if (mDropDownList == null) { 940 Context context = mContext; 941 942 /** 943 * This Runnable exists for the sole purpose of checking if the view layout has got 944 * completed and if so call showDropDown to display the drop down. This is used to show 945 * the drop down as soon as possible after user opens up the search dialog, without 946 * waiting for the normal UI pipeline to do it's job which is slower than this method. 947 */ 948 mShowDropDownRunnable = new Runnable() { 949 public void run() { 950 // View layout should be all done before displaying the drop down. 951 View view = getAnchorView(); 952 if (view != null && view.getWindowToken() != null) { 953 show(); 954 } 955 } 956 }; 957 958 mDropDownList = new DropDownListView(context, !mModal); 959 if (mDropDownListHighlight != null) { 960 mDropDownList.setSelector(mDropDownListHighlight); 961 } 962 mDropDownList.setAdapter(mAdapter); 963 mDropDownList.setVerticalFadingEdgeEnabled(true); 964 mDropDownList.setOnItemClickListener(mItemClickListener); 965 mDropDownList.setFocusable(true); 966 mDropDownList.setFocusableInTouchMode(true); 967 mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 968 public void onItemSelected(AdapterView<?> parent, View view, 969 int position, long id) { 970 971 if (position != -1) { 972 DropDownListView dropDownList = mDropDownList; 973 974 if (dropDownList != null) { 975 dropDownList.mListSelectionHidden = false; 976 } 977 } 978 } 979 980 public void onNothingSelected(AdapterView<?> parent) { 981 } 982 }); 983 mDropDownList.setOnScrollListener(mScrollListener); 984 985 if (mItemSelectedListener != null) { 986 mDropDownList.setOnItemSelectedListener(mItemSelectedListener); 987 } 988 989 dropDownView = mDropDownList; 990 991 View hintView = mPromptView; 992 if (hintView != null) { 993 // if an hint has been specified, we accomodate more space for it and 994 // add a text view in the drop down menu, at the bottom of the list 995 LinearLayout hintContainer = new LinearLayout(context); 996 hintContainer.setOrientation(LinearLayout.VERTICAL); 997 998 LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams( 999 ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f 1000 ); 1001 1002 switch (mPromptPosition) { 1003 case POSITION_PROMPT_BELOW: 1004 hintContainer.addView(dropDownView, hintParams); 1005 hintContainer.addView(hintView); 1006 break; 1007 1008 case POSITION_PROMPT_ABOVE: 1009 hintContainer.addView(hintView); 1010 hintContainer.addView(dropDownView, hintParams); 1011 break; 1012 1013 default: 1014 Log.e(TAG, "Invalid hint position " + mPromptPosition); 1015 break; 1016 } 1017 1018 // measure the hint's height to find how much more vertical space 1019 // we need to add to the drop down's height 1020 int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST); 1021 int heightSpec = MeasureSpec.UNSPECIFIED; 1022 hintView.measure(widthSpec, heightSpec); 1023 1024 hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams(); 1025 otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin 1026 + hintParams.bottomMargin; 1027 1028 dropDownView = hintContainer; 1029 } 1030 1031 mPopup.setContentView(dropDownView); 1032 } else { 1033 dropDownView = (ViewGroup) mPopup.getContentView(); 1034 final View view = mPromptView; 1035 if (view != null) { 1036 LinearLayout.LayoutParams hintParams = 1037 (LinearLayout.LayoutParams) view.getLayoutParams(); 1038 otherHeights = view.getMeasuredHeight() + hintParams.topMargin 1039 + hintParams.bottomMargin; 1040 } 1041 } 1042 1043 // Max height available on the screen for a popup. 1044 boolean ignoreBottomDecorations = 1045 mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED; 1046 final int maxHeight = mPopup.getMaxAvailableHeight( 1047 getAnchorView(), mDropDownVerticalOffset, ignoreBottomDecorations); 1048 1049 // getMaxAvailableHeight() subtracts the padding, so we put it back, 1050 // to get the available height for the whole window 1051 int padding = 0; 1052 Drawable background = mPopup.getBackground(); 1053 if (background != null) { 1054 background.getPadding(mTempRect); 1055 padding = mTempRect.top + mTempRect.bottom; 1056 } 1057 1058 if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) { 1059 return maxHeight + padding; 1060 } 1061 1062 final int listContent = mDropDownList.measureHeightOfChildren(MeasureSpec.UNSPECIFIED, 1063 0, ListView.NO_POSITION, maxHeight - otherHeights, 2); 1064 // add padding only if the list has items in it, that way we don't show 1065 // the popup if it is not needed 1066 if (listContent > 0) otherHeights += padding; 1067 1068 return listContent + otherHeights; 1069 } 1070 1071 /** 1072 * <p>Wrapper class for a ListView. This wrapper can hijack the focus to 1073 * make sure the list uses the appropriate drawables and states when 1074 * displayed on screen within a drop down. The focus is never actually 1075 * passed to the drop down in this mode; the list only looks focused.</p> 1076 */ 1077 private static class DropDownListView extends ListView { 1078 private static final String TAG = ListPopupWindow.TAG + ".DropDownListView"; 1079 /* 1080 * WARNING: This is a workaround for a touch mode issue. 1081 * 1082 * Touch mode is propagated lazily to windows. This causes problems in 1083 * the following scenario: 1084 * - Type something in the AutoCompleteTextView and get some results 1085 * - Move down with the d-pad to select an item in the list 1086 * - Move up with the d-pad until the selection disappears 1087 * - Type more text in the AutoCompleteTextView *using the soft keyboard* 1088 * and get new results; you are now in touch mode 1089 * - The selection comes back on the first item in the list, even though 1090 * the list is supposed to be in touch mode 1091 * 1092 * Using the soft keyboard triggers the touch mode change but that change 1093 * is propagated to our window only after the first list layout, therefore 1094 * after the list attempts to resurrect the selection. 1095 * 1096 * The trick to work around this issue is to pretend the list is in touch 1097 * mode when we know that the selection should not appear, that is when 1098 * we know the user moved the selection away from the list. 1099 * 1100 * This boolean is set to true whenever we explicitly hide the list's 1101 * selection and reset to false whenever we know the user moved the 1102 * selection back to the list. 1103 * 1104 * When this boolean is true, isInTouchMode() returns true, otherwise it 1105 * returns super.isInTouchMode(). 1106 */ 1107 private boolean mListSelectionHidden; 1108 1109 /** 1110 * True if this wrapper should fake focus. 1111 */ 1112 private boolean mHijackFocus; 1113 1114 /** 1115 * <p>Creates a new list view wrapper.</p> 1116 * 1117 * @param context this view's context 1118 */ 1119 public DropDownListView(Context context, boolean hijackFocus) { 1120 super(context, null, com.android.internal.R.attr.dropDownListViewStyle); 1121 mHijackFocus = hijackFocus; 1122 // TODO: Add an API to control this 1123 setCacheColorHint(0); // Transparent, since the background drawable could be anything. 1124 } 1125 1126 /** 1127 * <p>Avoids jarring scrolling effect by ensuring that list elements 1128 * made of a text view fit on a single line.</p> 1129 * 1130 * @param position the item index in the list to get a view for 1131 * @return the view for the specified item 1132 */ 1133 @Override 1134 View obtainView(int position, boolean[] isScrap) { 1135 View view = super.obtainView(position, isScrap); 1136 1137 if (view instanceof TextView) { 1138 ((TextView) view).setHorizontallyScrolling(true); 1139 } 1140 1141 return view; 1142 } 1143 1144 @Override 1145 public boolean isInTouchMode() { 1146 // WARNING: Please read the comment where mListSelectionHidden is declared 1147 return (mHijackFocus && mListSelectionHidden) || super.isInTouchMode(); 1148 } 1149 1150 /** 1151 * <p>Returns the focus state in the drop down.</p> 1152 * 1153 * @return true always if hijacking focus 1154 */ 1155 @Override 1156 public boolean hasWindowFocus() { 1157 return mHijackFocus || super.hasWindowFocus(); 1158 } 1159 1160 /** 1161 * <p>Returns the focus state in the drop down.</p> 1162 * 1163 * @return true always if hijacking focus 1164 */ 1165 @Override 1166 public boolean isFocused() { 1167 return mHijackFocus || super.isFocused(); 1168 } 1169 1170 /** 1171 * <p>Returns the focus state in the drop down.</p> 1172 * 1173 * @return true always if hijacking focus 1174 */ 1175 @Override 1176 public boolean hasFocus() { 1177 return mHijackFocus || super.hasFocus(); 1178 } 1179 } 1180 1181 private class PopupDataSetObserver extends DataSetObserver { 1182 @Override 1183 public void onChanged() { 1184 if (isShowing()) { 1185 // Resize the popup to fit new content 1186 show(); 1187 } 1188 } 1189 1190 @Override 1191 public void onInvalidated() { 1192 dismiss(); 1193 } 1194 } 1195 1196 private class ListSelectorHider implements Runnable { 1197 public void run() { 1198 clearListSelection(); 1199 } 1200 } 1201 1202 private class ResizePopupRunnable implements Runnable { 1203 public void run() { 1204 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); 1205 show(); 1206 } 1207 } 1208 1209 private class PopupTouchInterceptor implements OnTouchListener { 1210 public boolean onTouch(View v, MotionEvent event) { 1211 final int action = event.getAction(); 1212 final int x = (int) event.getX(); 1213 final int y = (int) event.getY(); 1214 1215 if (action == MotionEvent.ACTION_DOWN && 1216 mPopup != null && mPopup.isShowing() && 1217 (x >= 0 && x < getWidth() && y >= 0 && y < getHeight())) { 1218 mHandler.postDelayed(mResizePopupRunnable, EXPAND_LIST_TIMEOUT); 1219 } else if (action == MotionEvent.ACTION_UP) { 1220 mHandler.removeCallbacks(mResizePopupRunnable); 1221 } 1222 return false; 1223 } 1224 } 1225 1226 private class PopupScrollListener implements ListView.OnScrollListener { 1227 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 1228 int totalItemCount) { 1229 1230 } 1231 1232 public void onScrollStateChanged(AbsListView view, int scrollState) { 1233 if (scrollState == SCROLL_STATE_TOUCH_SCROLL && 1234 !isInputMethodNotNeeded() && mPopup.getContentView() != null) { 1235 mHandler.removeCallbacks(mResizePopupRunnable); 1236 mResizePopupRunnable.run(); 1237 } 1238 } 1239 } 1240} 1241