Android Open Source - hpush Sort Action View Provider






From Project

Back to project page hpush.

License

The source code is released under:

MIT License

If you think the Android project hpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hpush.views;
/*from   w ww .  j  a  v a  2  s  .  c  o  m*/
import android.content.Context;
import android.support.v4.view.ActionProvider;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.PopupMenu.OnDismissListener;
import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.hpush.R;
import com.hpush.bus.SortAllEvent;
import com.hpush.utils.Prefs;

import de.greenrobot.event.EventBus;

/**
 * A popup-menu to sort views.
 *
 * @author Xinyue Zhao
 */
public final class SortActionViewProvider extends ActionProvider implements OnDismissListener, OnMenuItemClickListener {
  /**
   * Layout Id for the provider.
   */
  private static final int LAYOUT = R.layout.action_view_provider_sort;
  /**
   * Menu-resource of the popup.
   */
  private static final int MENU_RES = R.menu.sort;
  /**
   * A {@link android.view.View} for this provider.
   */
  private View mProviderV;
  /**
   * A popup with list of all sort-types.
   */
  private PopupMenu mPopupMenu;
  /**
   * Show/Hidden status of menu.
   */
  private boolean mShow;

  public SortActionViewProvider(Context context) {
    super(context);
    mProviderV = LayoutInflater.from(context).inflate(LAYOUT, null, false);
    mProviderV.setOnClickListener(new OnViewAnimatedClickedListener() {
      @Override
      public void onClick() {
        if (!mShow) {
          mShow = true;
          mPopupMenu.show();
          updateMenuItems(mPopupMenu.getMenu());
        } else {
          mPopupMenu.dismiss();
        }
      }
    });
    mPopupMenu = new PopupMenu(context, mProviderV);
    mPopupMenu.inflate(MENU_RES);
    mPopupMenu.setOnDismissListener(this);mPopupMenu.setOnMenuItemClickListener(this);
    updateMenuItems(mPopupMenu.getMenu());
  }

  @Override
  public View onCreateActionView() {
    return mProviderV;
  }


  @Override
  public void onDismiss(PopupMenu popupMenu) {
    mShow = false;
  }

  /**
   * Update check-status for menu.
   *
   * @param menu
   *     The host of all menu-items.
   */
  private void updateMenuItems(Menu menu) {
    menu.findItem(R.id.action_sort_scores).setChecked(0 == selectedSortTypeValue());
    menu.findItem(R.id.action_sort_arrival).setChecked(1 == selectedSortTypeValue());
    menu.findItem(R.id.action_sort_creation).setChecked(2 == selectedSortTypeValue());
    menu.findItem(R.id.action_sort_comments).setChecked(3 == selectedSortTypeValue());
  }

  /**
   * @return Get current selected sort-type {@code int}
   */
  private int selectedSortTypeValue() {
    String sortTypeValue = Prefs.getInstance(getContext().getApplicationContext()).getSortTypeValue();
    return Integer.valueOf(sortTypeValue);
  }

  @Override
  public boolean onMenuItemClick(MenuItem menuItem) {
    Prefs prefs = Prefs.getInstance(getContext().getApplicationContext());
    switch (menuItem.getItemId()) {
    case R.id.action_sort_scores:
      prefs.setSortTypeValue("0");
    break;
    case R.id.action_sort_arrival:
      prefs.setSortTypeValue("1");
    break;
    case R.id.action_sort_creation:
      prefs.setSortTypeValue("2");
    break;
    case R.id.action_sort_comments:
      prefs.setSortTypeValue("3");
    break;
    }
    updateMenuItems(mPopupMenu.getMenu());
    EventBus.getDefault().post(new SortAllEvent());
    return true;
  }
}




Java Source Code List

com.hpush.app.App.java
com.hpush.app.activities.BasicActivity.java
com.hpush.app.activities.DailiesActivity.java
com.hpush.app.activities.MainActivity.java
com.hpush.app.activities.SettingActivity.java
com.hpush.app.activities.WebViewActivity.java
com.hpush.app.adapters.DailiesListAdapter.java
com.hpush.app.adapters.MainViewPagerAdapter.java
com.hpush.app.adapters.MessagesListAdapter.java
com.hpush.app.fragments.AboutDialogFragment.java
com.hpush.app.fragments.AdFragment.java
com.hpush.app.fragments.AppListImpFragment.java
com.hpush.app.fragments.BookmarksListFragment.java
com.hpush.app.fragments.DailiesLstFragment.java
com.hpush.app.fragments.GPlusFragment.java
com.hpush.app.fragments.MessagesListFragment.java
com.hpush.bus.BookmarkAllEvent.java
com.hpush.bus.BookmarkMessageEvent.java
com.hpush.bus.BookmarkedEvent.java
com.hpush.bus.ClickMessageCommentsEvent.java
com.hpush.bus.ClickMessageEvent.java
com.hpush.bus.ClickMessageLinkEvent.java
com.hpush.bus.DeleteAccountEvent.java
com.hpush.bus.EULAConfirmedEvent.java
com.hpush.bus.EULARejectEvent.java
com.hpush.bus.EditSettingsEvent.java
com.hpush.bus.GCMRegistedEvent.java
com.hpush.bus.InsertAccountEvent.java
com.hpush.bus.LoadAllEvent.java
com.hpush.bus.LoginedGPlusEvent.java
com.hpush.bus.LogoutGPlusEvent.java
com.hpush.bus.RemoveAllEvent.java
com.hpush.bus.SelectMessageEvent.java
com.hpush.bus.ShareMessageEvent.java
com.hpush.bus.ShowActionBar.java
com.hpush.bus.SortAllEvent.java
com.hpush.bus.UpdateCurrentTotalMessagesEvent.java
com.hpush.data.DailyListItem.java
com.hpush.data.Daily.java
com.hpush.data.FunctionType.java
com.hpush.data.MessageListItem.java
com.hpush.data.Message.java
com.hpush.data.Status.java
com.hpush.data.SyncList.java
com.hpush.db.BookmarksTbl.java
com.hpush.db.DB.java
com.hpush.db.DailyTbl.java
com.hpush.db.DatabaseHelper.java
com.hpush.db.MessagesTbl.java
com.hpush.gcm.ChangeSettingsTask.java
com.hpush.gcm.GcmBroadcastReceiver.java
com.hpush.gcm.GcmIntentService.java
com.hpush.gcm.RegGCMTask.java
com.hpush.gcm.SyncTask.java
com.hpush.gcm.UnregGCMTask.java
com.hpush.utils.Prefs.java
com.hpush.utils.Utils.java
com.hpush.views.FontTextView.java
com.hpush.views.OnViewAnimatedClickedListener2.java
com.hpush.views.OnViewAnimatedClickedListener3.java
com.hpush.views.OnViewAnimatedClickedListener.java
com.hpush.views.SortActionViewProvider.java
com.hpush.views.WebViewEx.java