@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // add necessary intent values to be matched. mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); // Initialise LocalServiceReciever mLIntentFilter.addAction(SocketService.UPDATE_LIST); mLReciever = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateMap(); } }; // Initialise mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mManager.initialize(this, getMainLooper(), null); // Initialise mViewPager = (ViewPager) findViewById(R.id.pager); mActionBar = getActionBar(); mAdaptor = new TabsPagerAdaptor(getFragmentManager()); // Set the Tabs Pager mViewPager.setAdapter(mAdaptor); mActionBar.setHomeButtonEnabled(false); mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Add tabs for (int i = 0; i < mAdaptor.getCount(); i++) { mActionBar.addTab(mActionBar.newTab().setText(mAdaptor.getPageTitle(i)).setTabListener(this)); } // Bind to SocketService bindSocketService(); // ensure that tabs work mViewPager.setOnPageChangeListener( new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int position, float arg1, int arg2) {} @Override public void onPageScrollStateChanged(int index) {} }); }
/** * Get the Fragment at index * * @param index index of the fragment within the ViewPager * @return the fragment at index, or null if not found * @see TabsPagerAdaptor#getFragmentByPosition(int) * @see TabsPagerAdaptor#makeFragmentName(int, int) */ protected Fragment getFragment(int index) { /* * Safe(st) way to get Fragment. Makes no assumption on tagging * convention. */ return mAdaptor.getFragmentByPosition(index); /* * Safe(r) way to get Fragment. This might change in future as it is * based upon how the Adaptor tags the fragments itself. */ // String tag = TabsPagerAdaptor.makeFragmentName(mViewPager.getId(), // index); // return getFragmentManager().findFragmentByTag(tag); }