Made MainActivity more concise.
This commit is contained in:
parent
eceb3f8025
commit
d5ead78aa3
2 changed files with 39 additions and 88 deletions
|
@ -1,39 +1,43 @@
|
||||||
package com.github.nutomic.controldlna;
|
package com.github.nutomic.controldlna;
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.support.v4.app.FragmentPagerAdapter;
|
import android.support.v4.app.FragmentPagerAdapter;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import com.actionbarsherlock.app.ActionBar;
|
import com.actionbarsherlock.app.ActionBar;
|
||||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main activity, with tabs for media servers and media renderers.
|
||||||
|
*
|
||||||
|
* @author Felix
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class MainActivity extends SherlockFragmentActivity implements
|
public class MainActivity extends SherlockFragmentActivity implements
|
||||||
ActionBar.TabListener {
|
ActionBar.TabListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
* Provides Fragments, holding all of them in memory.
|
||||||
* fragments for each of the sections. We use a
|
|
||||||
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
|
|
||||||
* will keep every loaded fragment in memory. If this becomes too memory
|
|
||||||
* intensive, it may be best to switch to a
|
|
||||||
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
|
|
||||||
*/
|
*/
|
||||||
private SectionsPagerAdapter mSectionsPagerAdapter;
|
private SectionsPagerAdapter mSectionsPagerAdapter =
|
||||||
|
new SectionsPagerAdapter(getSupportFragmentManager());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link ViewPager} that will host the section contents.
|
* Holds the section contents.
|
||||||
*/
|
*/
|
||||||
private ViewPager mViewPager;
|
private ViewPager mViewPager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fragment for first tab, holding media renderers.
|
||||||
|
*/
|
||||||
|
private RendererFragment mRendererFragment = new RendererFragment();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fragment for second tab, holding media servers.
|
||||||
|
*/
|
||||||
private ServerFragment mServerFragment = new ServerFragment();
|
private ServerFragment mServerFragment = new ServerFragment();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -47,36 +51,24 @@ public class MainActivity extends SherlockFragmentActivity implements
|
||||||
actionBar.setDisplayShowTitleEnabled(false);
|
actionBar.setDisplayShowTitleEnabled(false);
|
||||||
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
||||||
|
|
||||||
// Create the adapter that will return a fragment for each of the three
|
|
||||||
// primary sections of the app.
|
|
||||||
mSectionsPagerAdapter = new SectionsPagerAdapter(
|
|
||||||
getSupportFragmentManager());
|
|
||||||
|
|
||||||
// Set up the ViewPager with the sections adapter.
|
|
||||||
mViewPager = (ViewPager) findViewById(R.id.pager);
|
mViewPager = (ViewPager) findViewById(R.id.pager);
|
||||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||||
|
|
||||||
// When swiping between different sections, select the corresponding
|
// Select correct tab after swiping.
|
||||||
// tab. We can also use ActionBar.Tab#select() to do this if we have
|
mViewPager.setOnPageChangeListener(
|
||||||
// a reference to the Tab.
|
new ViewPager.SimpleOnPageChangeListener() {
|
||||||
mViewPager
|
|
||||||
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onPageSelected(int position) {
|
public void onPageSelected(int position) {
|
||||||
actionBar.setSelectedNavigationItem(position);
|
actionBar.setSelectedNavigationItem(position);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// For each of the sections in the app, add a tab to the action bar.
|
|
||||||
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
|
|
||||||
// Create a tab with text corresponding to the page title defined by
|
|
||||||
// the adapter. Also specify this Activity object, which implements
|
|
||||||
// the TabListener interface, as the callback (listener) for when
|
|
||||||
// this tab is selected.
|
|
||||||
actionBar.addTab(actionBar.newTab()
|
actionBar.addTab(actionBar.newTab()
|
||||||
.setText(mSectionsPagerAdapter.getPageTitle(i))
|
.setText(R.string.title_renderer)
|
||||||
|
.setTabListener(this));
|
||||||
|
actionBar.addTab(actionBar.newTab()
|
||||||
|
.setText(R.string.title_server)
|
||||||
.setTabListener(this));
|
.setTabListener(this));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -96,8 +88,7 @@ public class MainActivity extends SherlockFragmentActivity implements
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
|
* Returns Fragment corresponding to current tab.
|
||||||
* one of the sections/tabs/pages.
|
|
||||||
*/
|
*/
|
||||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
||||||
|
|
||||||
|
@ -108,63 +99,16 @@ public class MainActivity extends SherlockFragmentActivity implements
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(int position) {
|
public Fragment getItem(int position) {
|
||||||
switch (position) {
|
switch (position) {
|
||||||
case 0:
|
case 0: return mRendererFragment;
|
||||||
Fragment fragment = new DummySectionFragment();
|
case 1: return mServerFragment;
|
||||||
Bundle args = new Bundle();
|
default: return null;
|
||||||
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
case 1:
|
|
||||||
return mServerFragment;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
// Show 2 total pages.
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CharSequence getPageTitle(int position) {
|
|
||||||
Locale l = Locale.getDefault();
|
|
||||||
switch (position) {
|
|
||||||
case 0:
|
|
||||||
return getString(R.string.title_renderer).toUpperCase(l);
|
|
||||||
case 1:
|
|
||||||
return getString(R.string.title_server).toUpperCase(l);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A dummy fragment representing a section of the app, but that simply
|
|
||||||
* displays dummy text.
|
|
||||||
*/
|
|
||||||
public static class DummySectionFragment extends Fragment {
|
|
||||||
/**
|
|
||||||
* The fragment argument representing the section number for this
|
|
||||||
* fragment.
|
|
||||||
*/
|
|
||||||
public static final String ARG_SECTION_NUMBER = "section_number";
|
|
||||||
|
|
||||||
public DummySectionFragment() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
||||||
Bundle savedInstanceState) {
|
|
||||||
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
|
|
||||||
container, false);
|
|
||||||
TextView dummyTextView = (TextView) rootView
|
|
||||||
.findViewById(R.id.section_label);
|
|
||||||
dummyTextView.setText(Integer.toString(getArguments().getInt(
|
|
||||||
ARG_SECTION_NUMBER)));
|
|
||||||
return rootView;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
7
src/com/github/nutomic/controldlna/RendererFragment.java
Normal file
7
src/com/github/nutomic/controldlna/RendererFragment.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package com.github.nutomic.controldlna;
|
||||||
|
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
|
||||||
|
public class RendererFragment extends Fragment {
|
||||||
|
|
||||||
|
}
|
Reference in a new issue