예제 #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entry);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mEntryPagerAdapter =
        new EntryPagerAdapter(getSupportFragmentManager(), getApplicationContext());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mEntryPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(
        new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
          }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mEntryPagerAdapter.getCount(); i++) {
      // Create a tab with text corresponding to the page title defined by
      // the adapter. Also specify this Activity object, which implements
      // the TabListener interface, as the callback (listener) for when
      // this tab is selected.
      actionBar.addTab(
          actionBar.newTab().setText(mEntryPagerAdapter.getPageTitle(i)).setTabListener(this));
    }

    performSearch(getIntent());
  }
예제 #2
0
  private void performSearch(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      int i = mViewPager.getCurrentItem();
      int returned = 0;

      String query = intent.getStringExtra(SearchManager.QUERY);

      switch (i) {
        case 0:
          returned = ((RestaurantFragment) mEntryPagerAdapter.getItem(i)).filterName(query);
          break;
        case 2:
          returned = ((CuisineFragment) mEntryPagerAdapter.getItem(i)).filter(query);
          break;
        default:
          return;
      }

      if (returned == 0) {
        Toast.makeText(getApplicationContext(), "No records found.", Toast.LENGTH_SHORT).show();
      }
    }
  }