|
| 1 | +package xyz.airquality; |
| 2 | + |
| 3 | +import android.support.design.widget.TabLayout; |
| 4 | +import android.support.v4.app.Fragment; |
| 5 | +import android.support.v4.app.FragmentManager; |
| 6 | +import android.support.v4.app.FragmentPagerAdapter; |
| 7 | +import android.support.v4.view.ViewPager; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.support.v7.widget.Toolbar; |
| 11 | +import android.view.Menu; |
| 12 | +import android.view.MenuItem; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import xyz.airquality.Fragments.ComparisonDaily; |
| 18 | +import xyz.airquality.Fragments.ComparisonHourly; |
| 19 | + |
| 20 | +public class ComparisonActivity extends AppCompatActivity { |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + setContentView(R.layout.activity_comparison); |
| 26 | + |
| 27 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 28 | + setSupportActionBar(toolbar); |
| 29 | + toolbar.setSubtitle("Comparing region1 and region2"); |
| 30 | + |
| 31 | + ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); |
| 32 | + if (viewPager != null) { |
| 33 | + setupViewPager(viewPager); |
| 34 | + } |
| 35 | + |
| 36 | + TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); |
| 37 | + tabLayout.setupWithViewPager(viewPager); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + private void setupViewPager(ViewPager viewPager) { |
| 42 | + Adapter adapter = new Adapter(getSupportFragmentManager()); |
| 43 | + adapter.addFragment(new ComparisonHourly(), "Hourly"); |
| 44 | + adapter.addFragment(new ComparisonDaily(), "Daily"); |
| 45 | + viewPager.setAdapter(adapter); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 50 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 51 | + getMenuInflater().inflate(R.menu.menu_comparison, menu); |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 57 | + // Handle action bar item clicks here. The action bar will |
| 58 | + // automatically handle clicks on the Home/Up button, so long |
| 59 | + // as you specify a parent activity in AndroidManifest.xml. |
| 60 | + int id = item.getItemId(); |
| 61 | + |
| 62 | + //noinspection SimplifiableIfStatement |
| 63 | + if (id == R.id.action_settings) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + return super.onOptionsItemSelected(item); |
| 68 | + } |
| 69 | + |
| 70 | + static class Adapter extends FragmentPagerAdapter { |
| 71 | + private final List<Fragment> mFragments = new ArrayList<>(); |
| 72 | + private final List<String> mFragmentTitles = new ArrayList<>(); |
| 73 | + |
| 74 | + public Adapter(FragmentManager fm) { |
| 75 | + super(fm); |
| 76 | + } |
| 77 | + |
| 78 | + public void addFragment(Fragment fragment, String title) { |
| 79 | + mFragments.add(fragment); |
| 80 | + mFragmentTitles.add(title); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Fragment getItem(int position) { |
| 85 | + return mFragments.get(position); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int getCount() { |
| 90 | + return mFragments.size(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public CharSequence getPageTitle(int position) { |
| 95 | + return mFragmentTitles.get(position); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments