• 团队冲刺第二阶段07


    今天主要学习并完成Navigation部分

    代码如下 

    public class NavigationDrawerFragment extends Fragment {
    
      static final int BURGER = 0;
      static final int ARROW = 1;
    
      ActionBarDrawerToggle mDrawerToggle;
      DrawerLayout mDrawerLayout;
      private MainActivity mActivity;
      private boolean alreadyInitialized;
    
    
      @Override
      public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
      }
    
    
      @Override
      public void onStart () {
        super.onStart();
        EventBus.getDefault().register(this);
      }
    
    
      @Override
      public void onStop () {
        super.onStop();
        EventBus.getDefault().unregister(this);
      }
    
    
      @Override
      public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
      }
    
    
      @Override
      public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (MainActivity) getActivity();
        init();
      }
    
    
      private MainActivity getMainActivity () {
        return (MainActivity) getActivity();
      }
    
    
      public void onEventMainThread (DynamicNavigationReadyEvent event) {
        if (alreadyInitialized) {
          alreadyInitialized = false;
        } else {
          refreshMenus();
        }
      }
    
    
      public void onEvent (CategoriesUpdatedEvent event) {
        refreshMenus();
      }
    
    
      public void onEventAsync (NotesUpdatedEvent event) {
        alreadyInitialized = false;
      }
    
    
      public void onEvent (NotesLoadedEvent event) {
        if (mDrawerLayout != null) {
          if (!isDoublePanelActive()) {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
          }
        }
        if (getMainActivity().getSupportFragmentManager().getBackStackEntryCount() == 0) {
          init();
        }
        refreshMenus();
        alreadyInitialized = true;
      }
    
    
      public void onEvent (SwitchFragmentEvent event) {
        if (CHILDREN.equals(event.direction)) {
          animateBurger(ARROW);
        } else {
          animateBurger(BURGER);
        }
      }
    
    
      public void onEvent (NavigationUpdatedEvent navigationUpdatedEvent) {
        if (navigationUpdatedEvent.navigationItem.getClass().isAssignableFrom(NavigationItem.class)) {
          mActivity.getSupportActionBar().setTitle(((NavigationItem) navigationUpdatedEvent.navigationItem).getText());
        } else {
          mActivity.getSupportActionBar().setTitle(((Category) navigationUpdatedEvent.navigationItem).getName());
        }
        if (mDrawerLayout != null) {
          if (!isDoublePanelActive()) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
          }
          new Handler().postDelayed(() -> EventBus.getDefault().post(new NavigationUpdatedNavDrawerClosedEvent
              (navigationUpdatedEvent.navigationItem)), 400);
        }
      }
    
    
      public void init () {
        LogDelegate.v("Started navigation drawer initialization");
    
        mDrawerLayout = mActivity.findViewById(R.id.drawer_layout);
        mDrawerLayout.setFocusableInTouchMode(false);
    
        View leftDrawer = getView().findViewById(R.id.left_drawer);
        int leftDrawerBottomPadding = Display.getNavigationBarHeightKitkat(getActivity());
        leftDrawer.setPadding(leftDrawer.getPaddingLeft(), leftDrawer.getPaddingTop(), leftDrawer.getPaddingRight(),
            leftDrawerBottomPadding);
    
        mDrawerToggle = new ActionBarDrawerToggle(mActivity,
            mDrawerLayout,
            getMainActivity().getToolbar(),
            R.string.drawer_open,
            R.string.drawer_close
        ) {
          public void onDrawerClosed (View view) {
            mActivity.supportInvalidateOptionsMenu();
          }
    
    
          public void onDrawerOpened (View drawerView) {
            mActivity.commitPending();
            mActivity.finishActionMode();
          }
        };
    
        if (isDoublePanelActive()) {
          mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
        }
    
        // Styling options
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    
        LogDelegate.v("Finished navigation drawer initialization");
      }
    
    
      private void refreshMenus () {
        buildMainMenu();
        LogDelegate.v("Finished main menu initialization");
        buildCategoriesMenu();
        LogDelegate.v("Finished categories menu initialization");
        mDrawerToggle.syncState();
      }
    
    
      private void buildCategoriesMenu () {
        CategoryMenuTask task = new CategoryMenuTask(this);
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
      }
    
    
      private void buildMainMenu () {
        MainMenuTask task = new MainMenuTask(this);
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
      }
    
    
      void animateBurger (int targetShape) {
        if (mDrawerToggle != null) {
          if (targetShape != BURGER && targetShape != ARROW) {
            return;
          }
          ValueAnimator anim = ValueAnimator.ofFloat((targetShape + 1) % 2, targetShape);
          anim.addUpdateListener(valueAnimator -> {
            float slideOffset = (Float) valueAnimator.getAnimatedValue();
            mDrawerToggle.onDrawerSlide(mDrawerLayout, slideOffset);
          });
          anim.setInterpolator(new DecelerateInterpolator());
          anim.setDuration(500);
          anim.start();
        }
      }
    
    
      public static boolean isDoublePanelActive () {
    resources.getDimension(R.dimen
        return false;
      }
    
    }
    

      

  • 相关阅读:
    智能算法浅介模拟退火,遗传算法,禁忌搜索,神经网络等
    shell 字符串操作(长度,查找,替换)详解
    如何实现两个文件相减的功能(剔除)
    shell数值操作(四则运算,浮点数,科学计数法)awk,bc
    CUDA程序优化的记录
    随机化算法模拟退火
    (转)AspNetPager 样式以及使用(漂亮)
    (转)做项目时,如何做比较美观大方的数据输入窗体
    (转)26个Jquery使用小技巧(jQuery tips, tricks & solutions)
    (转)【译】Asp.net MVC并不仅仅只是Linq to SQL
  • 原文地址:https://www.cnblogs.com/cfypd/p/13087967.html
Copyright © 2020-2023  润新知