Java tutorial
/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 http://www.apache.org/licenses/LICENSE-2.0 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. */ package sintef.android.gravity.advanced; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.util.SparseArray; import android.view.Menu; import android.view.MenuItem; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.Switch; import com.astuetz.PagerSlidingTabStrip; import de.greenrobot.event.EventBus; import sintef.android.controller.EventTypes; import sintef.android.controller.utils.PreferencesHelper; import sintef.android.gravity.AlarmFragment; import sintef.android.gravity.R; /** * Created by samyboy89 on 23/02/15. */ public class AdvancedActivity extends ActionBarActivity { private static final int NUM_PAGES = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_advanced); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); final ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new PagerAdapter(getSupportFragmentManager())); pager.setOffscreenPageLimit(NUM_PAGES - 1); PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); tabs.setTextColor(Color.WHITE); tabs.setViewPager(pager); tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { Fragment fragment = ((PagerAdapter) pager.getAdapter()).getRegisteredFragment(position); if (fragment instanceof RecordHistoryFragment) { ((RecordHistoryFragment) fragment).notifyOnDataSetChanged(); } } @Override public void onPageScrollStateChanged(int state) { } }); } @Override protected void onResume() { super.onResume(); EventBus.getDefault().post(EventTypes.ONRESUME); } @Override protected void onPause() { super.onPause(); EventBus.getDefault().post(EventTypes.ONPAUSE); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_advanced, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { Switch fall_detection_switch = (Switch) menu.findItem(R.id.fall_detection_switch).getActionView() .findViewById(R.id.switch_toolbar); fall_detection_switch .setChecked(PreferencesHelper.getBoolean(PreferencesHelper.FALL_DETECTION_ENABLED, true)); fall_detection_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { PreferencesHelper.putBoolean(PreferencesHelper.FALL_DETECTION_ENABLED, b); } }); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } private class PagerAdapter extends FragmentStatePagerAdapter { SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>(); public PagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new GraphFragment(); case 1: return new ThresholdsFragment(); case 2: return new RecordFragment(); case 3: return new RecordHistoryFragment(); } return new AlarmFragment(); } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.fragment_graph); case 1: return getString(R.string.fragment_thresholds); case 2: return getString(R.string.fragment_record); case 3: return getString(R.string.fragment_record_history); } return super.getPageTitle(position); } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); registeredFragments.put(position, fragment); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { registeredFragments.remove(position); super.destroyItem(container, position, object); } public Fragment getRegisteredFragment(int position) { return registeredFragments.get(position); } @Override public int getCount() { return NUM_PAGES; } } }