Java tutorial
package com.appdynamics.demo.gasp.activity; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.NavUtils; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import com.appdynamics.demo.gasp.R; import com.appdynamics.demo.gasp.adapter.RestaurantDataAdapter; import com.appdynamics.demo.gasp.adapter.TabPagerAdapter; /** * Copyright (c) 2013 Mark Prichard * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class GaspDataActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private static final String TAG = GaspDataActivity.class.getName(); private ViewPager viewPager; private ActionBar actionBar; // Tab titles private final String[] tabs = { "Restaurants", "Reviews", "Users" }; private RestaurantDataAdapter restaurantAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gasp_data_layout); viewPager = (ViewPager) findViewById(R.id.pager); actionBar = getActionBar(); TabPagerAdapter mAdapter = new TabPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrollStateChanged(int state) { } }); // Adding Tabs for (String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this)); } } @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu_short, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; case R.id.gasp_settings: Intent intent = new Intent(); intent.setClass(this, SetPreferencesActivity.class); startActivityForResult(intent, 0); return true; default: return super.onOptionsItemSelected(item); } } }