Ejemplo n.º 1
0
 /**
  * 展开SearchView进行查询
  *
  * @param key
  */
 private void search(String key) {
   if (!TextUtils.isEmpty(key)) {
     MenuItemCompat.expandActionView(searchMenuItem);
     searchView.setQuery(key, true);
     saveSearchHistory(key);
   }
 }
Ejemplo n.º 2
0
  private void performSearch(String query) {
    if (searchMenuItem == null) {
      // Store this for later when we do actually have a search menu ready to use.
      pendingSearchQuery = query;
      return;
    }

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    MenuItemCompat.expandActionView(searchMenuItem);
    searchView.setQuery(query, true);
  }
  @SuppressWarnings("ConstantConditions")
  private void initSearchView(final MenuItem searchMenuItem) {
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchView.setQueryHint(getString(R.string.hint_enter_user_name));
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {

          @Override
          public boolean onQueryTextSubmit(final String s) {
            final String nickName = s.trim();
            if (USER_NAME_PATTERN.matcher(nickName).matches()) {
              MenuItemCompat.collapseActionView(searchMenuItem);
              onUserNickSelected(nickName);
            }
            return true;
          }

          @Override
          public boolean onQueryTextChange(final String s) {
            final String nickName = s.trim();
            if (USER_NAME_PATTERN.matcher(nickName).matches()) {
              onUserNickSelected(nickName);
            }
            return true;
          }
        });
    MenuItemCompat.setOnActionExpandListener(
        searchMenuItem,
        new MenuItemCompat.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionExpand(final MenuItem item) {
            mExpandMenu = true;
            return true;
          }

          @Override
          public boolean onMenuItemActionCollapse(final MenuItem item) {
            mExpandMenu = false;
            return true;
          }
        });
    if (mExpandMenu) {
      MenuItemCompat.expandActionView(searchMenuItem);
      ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
          .toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
    } else {
      MenuItemCompat.collapseActionView(searchMenuItem);
    }
  }
  @SuppressLint("NewApi")
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate called again");
    super.onCreate(savedInstanceState);

    // check for single pane activity change
    mTwoPane = getResources().getBoolean(R.bool.has_two_panes);

    resolveIntentAction(getIntent());

    if (mSelectedTaskUri != null) {
      if (mShouldShowDetails && mShouldSwitchToDetail) {
        Intent viewTaskIntent = new Intent(Intent.ACTION_VIEW);
        viewTaskIntent.setData(mSelectedTaskUri);
        startActivity(viewTaskIntent);
        mSwitchedToDetail = true;
        mShouldSwitchToDetail = false;
        mTransientState = true;
      }
    } else {
      mShouldShowDetails = false;
    }

    setContentView(R.layout.activity_task_list);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mAuthority = TaskContract.taskAuthority(this);
    mSearchHistoryHelper = new SearchHistoryHelper(this);

    if (findViewById(R.id.task_detail_container) != null) {
      // In two-pane mode, list items should be given the
      // 'activated' state when touched.

      // get list fragment
      // mTaskListFrag = (TaskListFragment)
      // getSupportFragmentManager().findFragmentById(R.id.task_list);
      // mTaskListFrag.setListViewScrollbarPositionLeft(true);

      // mTaskListFrag.setActivateOnItemClick(true);

      /*
       * Create a detail fragment, but don't load any URL yet, we do that later when the fragment gets attached
       */
      mTaskDetailFrag = ViewTaskFragment.newInstance(mSelectedTaskUri);
      getSupportFragmentManager()
          .beginTransaction()
          .replace(R.id.task_detail_container, mTaskDetailFrag, DETAIL_FRAGMENT_TAG)
          .commit();
    } else {
      FragmentManager fragmentManager = getSupportFragmentManager();
      Fragment detailFragment = fragmentManager.findFragmentByTag(DETAIL_FRAGMENT_TAG);
      if (detailFragment != null) {
        fragmentManager.beginTransaction().remove(detailFragment).commit();
      }
    }

    mGroupingFactories =
        new AbstractGroupingFactory[] {
          new ByList(mAuthority),
          new ByDueDate(mAuthority),
          new ByStartDate(mAuthority),
          new ByPriority(mAuthority),
          new ByProgress(mAuthority),
          new BySearch(mAuthority, mSearchHistoryHelper)
        };

    // set up pager adapter
    try {
      mPagerAdapter =
          new TaskGroupPagerAdapter(
              getSupportFragmentManager(), mGroupingFactories, this, R.xml.listview_tabs);
    } catch (XmlPullParserException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    } catch (XmlObjectPullParserException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    }

    // Setup ViewPager
    mPagerAdapter.setTwoPaneLayout(mTwoPane);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);

    int currentPageIndex = mPagerAdapter.getPagePosition(mCurrentPageId);

    if (currentPageIndex >= 0) {
      mCurrentPagePosition = currentPageIndex;
      mViewPager.setCurrentItem(currentPageIndex);
      if (VERSION.SDK_INT >= 14 && mCurrentPageId == R.id.task_group_search) {
        if (mSearchItem != null) {
          // that's actually quite impossible to happen
          MenuItemCompat.expandActionView(mSearchItem);
        } else {
          mAutoExpandSearchView = true;
        }
      }
    }
    updateTitle(currentPageIndex);

    // Bind the tabs to the ViewPager
    mTabs = (TabLayout) findViewById(R.id.tabs);
    mTabs.setupWithViewPager(mViewPager);

    // set up the tab icons
    for (int i = 0, count = mPagerAdapter.getCount(); i < count; ++i) {
      mTabs.getTabAt(i).setIcon(mPagerAdapter.getTabIcon(i));
    }

    mViewPager.addOnPageChangeListener(
        new OnPageChangeListener() {

          @Override
          public void onPageSelected(int position) {
            mSelectedTaskUri = null;
            mCurrentPagePosition = position;

            int newPageId = mPagerAdapter.getPageId(mCurrentPagePosition);

            if (newPageId == R.id.task_group_search) {
              int oldPageId = mCurrentPageId;
              mCurrentPageId = newPageId;

              // store the page position we're coming from
              mPreviousPagePosition = mPagerAdapter.getPagePosition(oldPageId);
            } else if (mCurrentPageId == R.id.task_group_search) {
              // we've been on the search page before, so commit the search and close the search
              // view
              mSearchHistoryHelper.commitSearch();
              mHandler.post(mSearchUpdater);
              mCurrentPageId = newPageId;
              hideSearchActionView();
            }
            mCurrentPageId = newPageId;
            updateTitle(mCurrentPageId);
          }

          @Override
          public void onPageScrolled(
              int position, float positionOffset, int positionOffsetPixels) {}

          @Override
          public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE && mCurrentPageId == R.id.task_group_search) {
              // the search page is selected now, expand the search view
              mHandler.postDelayed(
                  new Runnable() {
                    @Override
                    public void run() {
                      MenuItemCompat.expandActionView(mSearchItem);
                    }
                  },
                  50);
            }
          }
        });

    mFloatingActionButton = (FloatingActionButton) findViewById(R.id.floating_action_button);
    if (mFloatingActionButton != null) {
      mFloatingActionButton.setOnClickListener(
          new OnClickListener() {

            @Override
            public void onClick(View v) {
              onAddNewTask();
            }
          });
    }
  }
 public void search(String query) {
   if (mSearchView != null && mSearchItem != null) {
     MenuItemCompat.expandActionView(mSearchItem);
     mSearchView.setQuery(query, true);
   }
 }
Ejemplo n.º 6
0
  private void showSearch(boolean visible) {

    if (visible) MenuItemCompat.expandActionView(searchItem);
    else MenuItemCompat.collapseActionView(searchItem);
  }