|
@@ -1,75 +1,59 @@
|
|
|
package com.example.david.libretasker;
|
|
package com.example.david.libretasker;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
import android.content.Intent;
|
|
import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
-import android.support.design.widget.TabLayout;
|
|
|
|
|
-import android.support.v4.app.Fragment;
|
|
|
|
|
-import android.support.v4.app.FragmentManager;
|
|
|
|
|
-import android.support.v4.app.FragmentPagerAdapter;
|
|
|
|
|
import android.support.v4.view.ViewPager;
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
|
+import android.support.v7.app.ActionBar;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
-import android.support.v7.widget.Toolbar;
|
|
|
|
|
-
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import android.view.Window;
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
- private Toolbar toolbar;
|
|
|
|
|
- private TabLayout tabLayout;
|
|
|
|
|
- private ViewPager viewPager;
|
|
|
|
|
|
|
+ ViewPager mViewPager;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
+ requestWindowFeature(Window.FEATURE_ACTION_BAR); //Call before setContentView
|
|
|
setContentView(R.layout.activity_main);
|
|
setContentView(R.layout.activity_main);
|
|
|
- startService( new Intent(this, MainService.class) );
|
|
|
|
|
|
|
|
|
|
- toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
|
- setSupportActionBar(toolbar);
|
|
|
|
|
|
|
+ final ActionBar actionBar = getSupportActionBar();
|
|
|
|
|
+ // Specify that tabs should be displayed in the action bar.
|
|
|
|
|
+ actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
|
|
|
|
|
|
|
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
+ // Create a tab listener that is called when the user changes tabs.
|
|
|
|
|
+ ActionBar.TabListener tabListener = new ActionBar.TabListener() {
|
|
|
|
|
|
|
|
- viewPager = (ViewPager) findViewById(R.id.viewpager);
|
|
|
|
|
- setupViewPager(viewPager);
|
|
|
|
|
|
|
|
|
|
- tabLayout = (TabLayout) findViewById(R.id.tabs);
|
|
|
|
|
- tabLayout.setupWithViewPager(viewPager);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
|
|
|
|
|
|
|
|
- private void setupViewPager(ViewPager viewPager) {
|
|
|
|
|
- ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
|
|
|
|
|
- adapter.addFragment(new BlankFragment(), "ONE");
|
|
|
|
|
- adapter.addFragment(new ActionsFragment(), "TWO");
|
|
|
|
|
- viewPager.setAdapter(adapter);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- class ViewPagerAdapter extends FragmentPagerAdapter {
|
|
|
|
|
- private final List<Fragment> mFragmentList = new ArrayList<>();
|
|
|
|
|
- private final List<String> mFragmentTitleList = new ArrayList<>();
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
|
|
|
|
|
|
|
|
- public ViewPagerAdapter(FragmentManager manager) {
|
|
|
|
|
- super(manager);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public Fragment getItem(int position) {
|
|
|
|
|
- return mFragmentList.get(position);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public int getCount() {
|
|
|
|
|
- return mFragmentList.size();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
- public void addFragment(Fragment fragment, String title) {
|
|
|
|
|
- mFragmentList.add(fragment);
|
|
|
|
|
- mFragmentTitleList.add(title);
|
|
|
|
|
|
|
+ // Add 3 tabs, specifying the tab's text and TabListener
|
|
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
|
|
+ actionBar.addTab(
|
|
|
|
|
+ actionBar.newTab()
|
|
|
|
|
+ .setText("Tab " + (i + 1))
|
|
|
|
|
+ .setTabListener(tabListener));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public CharSequence getPageTitle(int position) {
|
|
|
|
|
- return mFragmentTitleList.get(position);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // ViewPager and its adapters use support library
|
|
|
|
|
+ // fragments, so use getSupportFragmentManager.
|
|
|
|
|
+
|
|
|
|
|
+ startService( new Intent(this, MainService.class) );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|