From 8e04ce3afa0a98f02505440a18ac417120fc4b26 Mon Sep 17 00:00:00 2001 From: George Venios Date: Thu, 3 Sep 2015 01:29:14 +0300 Subject: [PATCH] Initial replacement of deprecated actionbar tabs with TabLayout. Still uses default styling. --- build.gradle | 2 + .../activities/MainActivity.java | 48 +++++++------------ .../activities/SyncthingActivity.java | 4 +- src/main/res/layout/activity_main.xml | 19 ++++++-- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 71ba63d7..542b726c 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,8 @@ repositories { dependencies { compile 'eu.chainfire:libsuperuser:1.0.0.201504231659' compile 'com.android.support:appcompat-v7:22.2.1' + compile 'com.android.support:design:22.2.1' + compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:support-v4-preferencefragment:1.0.0@aar' androidTestCompile 'com.squareup.okhttp:mockwebserver:2.4.0' } diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 22fbafe0..38d3e6fa 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -13,16 +13,13 @@ import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.preference.PreferenceManager; +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.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; -import android.support.v7.app.ActionBar; -import android.support.v7.app.ActionBar.Tab; -import android.support.v7.app.ActionBar.TabListener; import android.support.v7.app.ActionBarDrawerToggle; import android.util.Log; import android.view.KeyEvent; @@ -172,6 +169,17 @@ public class MainActivity extends SyncthingActivity return 2; } + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: + return getResources().getString(R.string.folders_fragment_title); + case 1: + return getResources().getString(R.string.devices_fragment_title); + default: + return String.valueOf(position); + } + } }; private FoldersFragment mFolderFragment; @@ -182,6 +190,8 @@ public class MainActivity extends SyncthingActivity private ViewPager mViewPager; + private TabLayout mTabLayout; + private ActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; @@ -191,41 +201,15 @@ public class MainActivity extends SyncthingActivity */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final ActionBar actionBar = getSupportActionBar(); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); - mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { - @Override - public void onPageSelected(int position) { - actionBar.setSelectedNavigationItem(position); - } - }); - TabListener tabListener = new TabListener() { - public void onTabSelected(Tab tab, FragmentTransaction ft) { - mViewPager.setCurrentItem(tab.getPosition()); - } - - @Override - public void onTabReselected(Tab tab, FragmentTransaction ft) { - } - - @Override - public void onTabUnselected(Tab tab, FragmentTransaction ft) { - } - }; - - actionBar.addTab(actionBar.newTab() - .setText(R.string.folders_fragment_title) - .setTabListener(tabListener)); - actionBar.addTab(actionBar.newTab() - .setText(R.string.devices_fragment_title) - .setTabListener(tabListener)); + mTabLayout = (TabLayout) findViewById(R.id.tabContainer); + mTabLayout.setupWithViewPager(mViewPager); if (savedInstanceState != null) { FragmentManager fm = getSupportFragmentManager(); diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java index ba0b52d5..fc7dd06f 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java @@ -18,7 +18,7 @@ import java.util.LinkedList; /** * Connects to {@link SyncthingService} and provides access to it. */ -public class SyncthingActivity extends AppCompatActivity implements ServiceConnection { +public abstract class SyncthingActivity extends AppCompatActivity implements ServiceConnection { private SyncthingService mSyncthingService; @@ -28,7 +28,7 @@ public class SyncthingActivity extends AppCompatActivity implements ServiceConne * To be used for Fragments. */ public interface OnServiceConnectedListener { - public void onServiceConnected(); + void onServiceConnected(); } @Override diff --git a/src/main/res/layout/activity_main.xml b/src/main/res/layout/activity_main.xml index a81a8254..701502e1 100644 --- a/src/main/res/layout/activity_main.xml +++ b/src/main/res/layout/activity_main.xml @@ -4,10 +4,21 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:orientation="vertical"> + + + + + + android:elevation="16dp" />