Android: Use a PagerTabStrip instead of the ActionBar.

The ActionBar method of doing the tabular layout is deprecated on Android 5.0.
This method alleviates those deprecations while providing the same functionality.
This commit is contained in:
Lioncash 2014-11-16 21:11:38 -05:00
parent c0291d5b88
commit 5396a48d70
3 changed files with 36 additions and 136 deletions

View File

@ -1,5 +1,15 @@
<android.support.v4.view.ViewPager <?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android" <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
</android.support.v4.view.ViewPager>

View File

@ -9,13 +9,9 @@ package org.dolphinemu.dolphinemu.about;
import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.utils.EGLHelper; import org.dolphinemu.dolphinemu.utils.EGLHelper;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle; import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
@ -25,9 +21,8 @@ import android.support.v4.view.ViewPager;
* Activity for the about menu, which displays info * Activity for the about menu, which displays info
* related to the CPU and GPU. Along with misc other info. * related to the CPU and GPU. Along with misc other info.
*/ */
public final class AboutActivity extends Activity implements TabListener public final class AboutActivity extends Activity
{ {
private ViewPager viewPager;
private final EGLHelper eglHelper = new EGLHelper(EGLHelper.EGL_OPENGL_ES2_BIT); private final EGLHelper eglHelper = new EGLHelper(EGLHelper.EGL_OPENGL_ES2_BIT);
@Override @Override
@ -37,32 +32,10 @@ public final class AboutActivity extends Activity implements TabListener
// Set the view pager // Set the view pager
setContentView(R.layout.viewpager); setContentView(R.layout.viewpager);
viewPager = (ViewPager) findViewById(R.id.pager);
// Initialize the ViewPager adapter. // Initialize the viewpager.
final ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager()); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter); viewPager.setAdapter(new ViewPagerAdapter(getFragmentManager()));
// Set up the ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.general).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.cpu).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.gles_two).setTabListener(this));
if (eglHelper.supportsGLES3())
actionBar.addTab(actionBar.newTab().setText(R.string.gles_three).setTabListener(this));
if (eglHelper.supportsOpenGL())
actionBar.addTab(actionBar.newTab().setText(R.string.desktop_gl).setTabListener(this));
// Set the page change listener
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
actionBar.setSelectedNavigationItem(position);
}
});
} }
@Override @Override
@ -73,31 +46,20 @@ public final class AboutActivity extends Activity implements TabListener
eglHelper.closeHelper(); eglHelper.closeHelper();
} }
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// When the given tab is selected, switch to the corresponding page in the ViewPager.
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
// Do nothing.
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
// Do nothing.
}
/** /**
* {@link FragmentPagerAdapter} subclass responsible for handling * {@link FragmentPagerAdapter} subclass responsible for handling
* the individual {@link Fragment}s within the {@link ViewPager}. * the individual {@link Fragment}s within the {@link ViewPager}.
*/ */
private final class ViewPagerAdapter extends FragmentPagerAdapter private final class ViewPagerAdapter extends FragmentPagerAdapter
{ {
private final String[] pageTitles = {
getString(R.string.general),
getString(R.string.cpu),
getString(R.string.gles_two),
getString(R.string.gles_three),
getString(R.string.desktop_gl),
};
public ViewPagerAdapter(FragmentManager fm) public ViewPagerAdapter(FragmentManager fm)
{ {
super(fm); super(fm);
@ -154,26 +116,7 @@ public final class AboutActivity extends Activity implements TabListener
@Override @Override
public CharSequence getPageTitle(int position) public CharSequence getPageTitle(int position)
{ {
switch (position) return pageTitles[position];
{
case 0:
return getString(R.string.general);
case 1:
return getString(R.string.cpu);
case 2:
return getString(R.string.gles_two);
case 3:
return getString(R.string.gles_three);
case 4:
return getString(R.string.desktop_gl);
default: // Should never happen
return null;
}
} }
} }
} }

View File

@ -11,12 +11,9 @@ import org.dolphinemu.dolphinemu.settings.cpu.CPUSettingsFragment;
import org.dolphinemu.dolphinemu.settings.input.InputConfigFragment; import org.dolphinemu.dolphinemu.settings.input.InputConfigFragment;
import org.dolphinemu.dolphinemu.settings.video.VideoSettingsFragment; import org.dolphinemu.dolphinemu.settings.video.VideoSettingsFragment;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle; import android.os.Bundle;
@ -28,64 +25,21 @@ import android.support.v4.view.ViewPager;
* Main activity that manages all of the preference fragments used to display * Main activity that manages all of the preference fragments used to display
* the settings to the user. * the settings to the user.
*/ */
public final class PrefsActivity extends Activity implements ActionBar.TabListener, OnSharedPreferenceChangeListener public final class PrefsActivity extends Activity implements OnSharedPreferenceChangeListener
{ {
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Set the ViewPager.
setContentView(R.layout.viewpager); setContentView(R.layout.viewpager);
mViewPager = (ViewPager) findViewById(R.id.pager);
// Set the ViewPager adapter. // Set the ViewPager adapter.
final ViewPagerAdapter mSectionsPagerAdapter = new ViewPagerAdapter(getFragmentManager()); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter); viewPager.setAdapter(new ViewPagerAdapter(getFragmentManager()));
// Register the preference change listener. // Register the preference change listener.
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sPrefs.registerOnSharedPreferenceChangeListener(this); sPrefs.registerOnSharedPreferenceChangeListener(this);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.cpu_settings).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.input_settings).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.video_settings).setTabListener(this));
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
actionBar.setSelectedNavigationItem(position);
}
} );
}
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
// Do nothing.
}
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
// Do nothing.
} }
@Override @Override
@ -101,6 +55,12 @@ public final class PrefsActivity extends Activity implements ActionBar.TabListen
*/ */
private final class ViewPagerAdapter extends FragmentPagerAdapter private final class ViewPagerAdapter extends FragmentPagerAdapter
{ {
private final String[] pageTitles = {
getString(R.string.cpu_settings),
getString(R.string.input_settings),
getString(R.string.video_settings)
};
public ViewPagerAdapter(FragmentManager fm) public ViewPagerAdapter(FragmentManager fm)
{ {
super(fm); super(fm);
@ -135,20 +95,7 @@ public final class PrefsActivity extends Activity implements ActionBar.TabListen
@Override @Override
public CharSequence getPageTitle(int position) public CharSequence getPageTitle(int position)
{ {
switch(position) return pageTitles[position];
{
case 0:
return getString(R.string.cpu_settings);
case 1:
return getString(R.string.input_settings);
case 2:
return getString(R.string.video_settings);
default: // Should never happen.
return null;
}
} }
} }
} }