コード例 #1
0
  @Override
  public void onClick(View v) {
    if (UiClosables.closeQuietly(mPopup)) {
      return;
    }

    mAdapter =
        new GroupMembershipAdapter<GroupSelectionItem>(
            getContext(), R.layout.group_membership_list_item);

    mGroupMetaData.moveToPosition(-1);
    while (mGroupMetaData.moveToNext()) {
      String accountName = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_NAME);
      String accountType = mGroupMetaData.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
      String dataSet = mGroupMetaData.getString(GroupMetaDataLoader.DATA_SET);
      if (accountName.equals(mAccountName)
          && accountType.equals(mAccountType)
          && Objects.equal(dataSet, mDataSet)) {
        long groupId = mGroupMetaData.getLong(GroupMetaDataLoader.GROUP_ID);
        if (groupId != mFavoritesGroupId && (groupId != mDefaultGroupId || mDefaultGroupVisible)) {
          String title = mGroupMetaData.getString(GroupMetaDataLoader.TITLE);
          boolean checked = hasMembership(groupId);
          mAdapter.add(new GroupSelectionItem(groupId, title, checked));
        }
      }
    }

    mAdapter.add(
        new GroupSelectionItem(
            CREATE_NEW_GROUP_GROUP_ID,
            getContext().getString(R.string.create_group_item_label),
            false));

    mPopup = new ListPopupWindow(getContext(), null);
    mPopup.setAnchorView(mGroupList);
    mPopup.setAdapter(mAdapter);
    mPopup.setModal(true);
    mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
    mPopup.show();

    ListView listView = mPopup.getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setOverScrollMode(OVER_SCROLL_ALWAYS);
    int count = mAdapter.getCount();
    for (int i = 0; i < count; i++) {
      listView.setItemChecked(i, mAdapter.getItem(i).isChecked());
    }

    listView.setOnItemClickListener(this);
  }
コード例 #2
0
 /**
  * Constructor for default vertical layout
  *
  * @param context Context
  */
 public PopupMenuNative(Context context, View view) {
   mContext = context;
   mView = view;
   mAdapter = new MenuAdapter(context);
   mMenu = new MenuImpl(mContext, mAdapter);
   mWindow = new ListPopupWindow(context);
   mWindow.setInputMethodMode(INPUT_METHOD_NOT_NEEDED);
   mWindow.setAnchorView(mView);
   mWindow.setWidth(mContext.getResources().getDimensionPixelSize(R.dimen.popup_window_width));
   mWindow.setAdapter(mAdapter);
   mWindow.setOnItemClickListener(this);
   mWindow.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
   mWindow.setModal(true);
 }
コード例 #3
0
  public static ListPopupWindow createPopupMenu(
      Context context, View anchorView, final Listener listener, int mode) {
    // Build choices, depending on the current mode. We assume this Dialog is never called
    // if there are NO choices (e.g. a read-only picture is already super-primary)
    final ArrayList<ChoiceListItem> choices = new ArrayList<ChoiceListItem>(4);
    // Use as Primary
    if (mode == MODE_PHOTO_ALLOW_PRIMARY || mode == MODE_READ_ONLY_ALLOW_PRIMARY) {
      choices.add(
          new ChoiceListItem(
              ChoiceListItem.ID_USE_AS_PRIMARY, context.getString(R.string.use_photo_as_primary)));
    }
    // Remove
    if (mode == MODE_PHOTO_DISALLOW_PRIMARY || mode == MODE_PHOTO_ALLOW_PRIMARY) {
      choices.add(
          new ChoiceListItem(ChoiceListItem.ID_REMOVE, context.getString(R.string.removePhoto)));
    }
    // Take photo (if there is already a photo, it says "Take new photo")
    if (mode == MODE_NO_PHOTO
        || mode == MODE_PHOTO_ALLOW_PRIMARY
        || mode == MODE_PHOTO_DISALLOW_PRIMARY) {
      final int resId = mode == MODE_NO_PHOTO ? R.string.take_photo : R.string.take_new_photo;
      choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, context.getString(resId)));
    }
    // Select from Gallery (or "Select new from Gallery")
    if (mode == MODE_NO_PHOTO
        || mode == MODE_PHOTO_ALLOW_PRIMARY
        || mode == MODE_PHOTO_DISALLOW_PRIMARY) {
      final int resId = mode == MODE_NO_PHOTO ? R.string.pick_photo : R.string.pick_new_photo;
      choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, context.getString(resId)));
    }
    final ListAdapter adapter =
        new ArrayAdapter<ChoiceListItem>(context, R.layout.select_dialog_item, choices);

    final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
    final OnItemClickListener clickListener =
        new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final ChoiceListItem choice = choices.get(position);
            listPopupWindow.dismiss();

            switch (choice.getId()) {
              case ChoiceListItem.ID_USE_AS_PRIMARY:
                listener.onUseAsPrimaryChosen();
                break;
              case ChoiceListItem.ID_REMOVE:
                listener.onRemovePictureChosen();
                break;
              case ChoiceListItem.ID_TAKE_PHOTO:
                listener.onTakePhotoChosen();
                break;
              case ChoiceListItem.ID_PICK_PHOTO:
                listener.onPickFromGalleryChosen();
                break;
            }
          }
        };

    listPopupWindow.setAnchorView(anchorView);
    listPopupWindow.setAdapter(adapter);
    listPopupWindow.setOnItemClickListener(clickListener);
    listPopupWindow.setWidth(
        context.getResources().getDimensionPixelSize(R.dimen.photo_action_popup_width));
    listPopupWindow.setModal(true);
    listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
    return listPopupWindow;
  }
コード例 #4
0
ファイル: AppMenu.java プロジェクト: joone/chromium-crosswalk
  /**
   * Creates and shows the app menu anchored to the specified view.
   *
   * @param context The context of the AppMenu (ensure the proper theme is set on this context).
   * @param anchorView The anchor {@link View} of the {@link ListPopupWindow}.
   * @param isByPermanentButton Whether or not permanent hardware button triggered it. (oppose to
   *     software button or keyboard).
   * @param screenRotation Current device screen rotation.
   * @param visibleDisplayFrame The display area rect in which AppMenu is supposed to fit in.
   * @param screenHeight Current device screen height.
   * @param footerResourceId The resource id for a view to add to the end of the menu list. Can be 0
   *     if no such view is required.
   */
  void show(
      Context context,
      View anchorView,
      boolean isByPermanentButton,
      int screenRotation,
      Rect visibleDisplayFrame,
      int screenHeight,
      int footerResourceId) {
    mPopup = new ListPopupWindow(context, null, android.R.attr.popupMenuStyle);
    mPopup.setModal(true);
    mPopup.setAnchorView(anchorView);
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);

    int footerHeight = 0;
    if (footerResourceId != 0) {
      mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
      View promptView = LayoutInflater.from(context).inflate(footerResourceId, null);
      mPopup.setPromptView(promptView);
      int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
      promptView.measure(measureSpec, measureSpec);
      footerHeight = promptView.getMeasuredHeight();
    }
    mPopup.setOnDismissListener(
        new OnDismissListener() {
          @Override
          public void onDismiss() {
            if (mPopup.getAnchorView() instanceof ImageButton) {
              ((ImageButton) mPopup.getAnchorView()).setSelected(false);
            }

            if (mMenuItemEnterAnimator != null) mMenuItemEnterAnimator.cancel();

            mHandler.appMenuDismissed();
            mHandler.onMenuVisibilityChanged(false);
          }
        });

    // Some OEMs don't actually let us change the background... but they still return the
    // padding of the new background, which breaks the menu height.  If we still have a
    // drawable here even though our style says @null we should use this padding instead...
    Drawable originalBgDrawable = mPopup.getBackground();

    // Need to explicitly set the background here.  Relying on it being set in the style caused
    // an incorrectly drawn background.
    if (isByPermanentButton) {
      mPopup.setBackgroundDrawable(
          ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.menu_bg));
    } else {
      mPopup.setBackgroundDrawable(
          ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.edge_menu_bg));
      mPopup.setAnimationStyle(R.style.OverflowMenuAnim);
    }

    // Turn off window animations for low end devices, and on Android M, which has built-in menu
    // animations.
    if (SysUtils.isLowEndDevice() || Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      mPopup.setAnimationStyle(0);
    }

    Rect bgPadding = new Rect();
    mPopup.getBackground().getPadding(bgPadding);

    int popupWidth =
        context.getResources().getDimensionPixelSize(R.dimen.menu_width)
            + bgPadding.left
            + bgPadding.right;

    mPopup.setWidth(popupWidth);

    mCurrentScreenRotation = screenRotation;
    mIsByPermanentButton = isByPermanentButton;

    // Extract visible items from the Menu.
    int numItems = mMenu.size();
    List<MenuItem> menuItems = new ArrayList<MenuItem>();
    for (int i = 0; i < numItems; ++i) {
      MenuItem item = mMenu.getItem(i);
      if (item.isVisible()) {
        menuItems.add(item);
      }
    }

    Rect sizingPadding = new Rect(bgPadding);
    if (isByPermanentButton && originalBgDrawable != null) {
      Rect originalPadding = new Rect();
      originalBgDrawable.getPadding(originalPadding);
      sizingPadding.top = originalPadding.top;
      sizingPadding.bottom = originalPadding.bottom;
    }

    // A List adapter for visible items in the Menu. The first row is added as a header to the
    // list view.
    mAdapter = new AppMenuAdapter(this, menuItems, LayoutInflater.from(context));
    mPopup.setAdapter(mAdapter);

    setMenuHeight(menuItems.size(), visibleDisplayFrame, screenHeight, sizingPadding, footerHeight);
    setPopupOffset(mPopup, mCurrentScreenRotation, visibleDisplayFrame, sizingPadding);
    mPopup.setOnItemClickListener(this);
    mPopup.show();
    mPopup.getListView().setItemsCanFocus(true);
    mPopup.getListView().setOnKeyListener(this);

    mHandler.onMenuVisibilityChanged(true);

    if (mVerticalFadeDistance > 0) {
      mPopup.getListView().setVerticalFadingEdgeEnabled(true);
      mPopup.getListView().setFadingEdgeLength(mVerticalFadeDistance);
    }

    // Don't animate the menu items for low end devices.
    if (!SysUtils.isLowEndDevice() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mPopup
          .getListView()
          .addOnLayoutChangeListener(
              new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(
                    View v,
                    int left,
                    int top,
                    int right,
                    int bottom,
                    int oldLeft,
                    int oldTop,
                    int oldRight,
                    int oldBottom) {
                  mPopup.getListView().removeOnLayoutChangeListener(this);
                  runMenuItemEnterAnimations();
                }
              });
    }
  }