Replaced tabs in the main Activity with viewpager

This commit is contained in:
daniel oeh 2012-07-01 12:34:10 +02:00
parent 8b5a7d8a41
commit 364e9e1c5c
2 changed files with 64 additions and 99 deletions

View File

@ -4,11 +4,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" > android:orientation="vertical" >
<FrameLayout <android.support.v4.view.ViewPager
android:id="@+id/main_fragment" android:id="@+id/viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0px" android:layout_height="0px"
android:layout_weight="1"> android:layout_weight="1">
</FrameLayout> </android.support.v4.view.ViewPager>
</LinearLayout> </LinearLayout>

View File

@ -7,7 +7,10 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
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.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -34,10 +37,8 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
private static final String TAG = "PodfetcherActivity"; private static final String TAG = "PodfetcherActivity";
private FeedManager manager; private FeedManager manager;
private ViewPager viewpager;
private FeedlistFragment feedlist; private MainPagerAdapter pagerAdapter;
ItemlistFragment unreadList;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -47,36 +48,11 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
setContentView(R.layout.main); setContentView(R.layout.main);
// Set up tabs // Set up tabs
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayShowTitleEnabled(false);
pagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
Tab tab = actionBar viewpager = (ViewPager) findViewById(R.id.viewpager);
.newTab() viewpager.setAdapter(pagerAdapter);
.setText(getText(R.string.feeds_label).toString())
.setTabListener(
new TabListener<FeedlistFragment>(this, getText(
R.string.feeds_label).toString(),
FeedlistFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText(getText(R.string.new_label).toString())
.setTabListener(
new TabListener<UnreadItemlistFragment>(this, getText(
R.string.new_label).toString(),
UnreadItemlistFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText(getText(R.string.queue_label).toString())
.setTabListener(
new TabListener<QueueFragment>(this, getText(
R.string.queue_label).toString(),
QueueFragment.class));
actionBar.addTab(tab);
} }
@Override @Override
@ -104,7 +80,8 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
}; };
private void updateProgressBarVisibility() { private void updateProgressBarVisibility() {
if (DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds()) { if (DownloadService.isRunning
&& DownloadRequester.getInstance().isDownloadingFeeds()) {
setSupportProgressBarIndeterminateVisibility(true); setSupportProgressBarIndeterminateVisibility(true);
} else { } else {
setSupportProgressBarIndeterminateVisibility(false); setSupportProgressBarIndeterminateVisibility(false);
@ -114,7 +91,7 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) { switch (item.getItemId()) {
case R.id.add_feed: case R.id.add_feed:
startActivity(new Intent(this, AddFeedActivity.class)); startActivity(new Intent(this, AddFeedActivity.class));
return true; return true;
@ -138,7 +115,8 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem refreshAll = menu.findItem(R.id.all_feed_refresh); MenuItem refreshAll = menu.findItem(R.id.all_feed_refresh);
if (DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds()) { if (DownloadService.isRunning
&& DownloadRequester.getInstance().isDownloadingFeeds()) {
refreshAll.setVisible(false); refreshAll.setVisible(false);
} else { } else {
refreshAll.setVisible(true); refreshAll.setVisible(true);
@ -153,48 +131,35 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
return true; return true;
} }
/** TabListener for navigating between the main lists. */ public static class MainPagerAdapter extends FragmentPagerAdapter {
private class TabListener<T extends Fragment> implements private static final int NUM_ITEMS = 3;
ActionBar.TabListener {
private final Activity activity; private static final int POS_FEEDLIST = 0;
private final String tag; private static final int POS_NEW_ITEMS = 1;
private final Class<T> fClass; private static final int POS_QUEUE = 2;
private Fragment fragment;
public TabListener(Activity activity, String tag, Class<T> fClass) { public MainPagerAdapter(FragmentManager fm) {
this.activity = activity; super(fm);
this.tag = tag;
this.fClass = fClass;
} }
@SuppressWarnings("unused") @Override
public TabListener(Activity activity, String tag, Fragment fragment, public Fragment getItem(int position) {
Class<T> fClass) { switch (position) {
super(); case POS_FEEDLIST:
this.activity = activity; return new FeedlistFragment();
this.tag = tag; case POS_NEW_ITEMS:
this.fragment = fragment; return new UnreadItemlistFragment();
this.fClass = fClass; case POS_QUEUE:
} return new QueueFragment();
default:
public void onTabSelected(Tab tab, FragmentTransaction ft) { return null;
if (fragment == null) {
fragment = Fragment.instantiate(activity, fClass.getName());
ft.replace(R.id.main_fragment, fragment);
} else {
ft.attach(fragment);
} }
} }
public void onTabUnselected(Tab tab, FragmentTransaction ft) { @Override
if (fragment != null) { public int getCount() {
ft.detach(fragment); return NUM_ITEMS;
}
} }
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Do nothing
}
} }
} }