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_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/main_fragment"
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</FrameLayout>
</android.support.v4.view.ViewPager>
</LinearLayout>

View File

@ -7,7 +7,10 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
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.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -34,10 +37,8 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
private static final String TAG = "PodfetcherActivity";
private FeedManager manager;
private FeedlistFragment feedlist;
ItemlistFragment unreadList;
private ViewPager viewpager;
private MainPagerAdapter pagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -47,38 +48,13 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
setContentView(R.layout.main);
// Set up tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar
.newTab()
.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);
pagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(pagerAdapter);
}
@Override
protected void onPause() {
super.onPause();
@ -94,7 +70,7 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED);
registerReceiver(contentUpdate, filter);
}
private BroadcastReceiver contentUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -102,99 +78,88 @@ public class PodfetcherActivity extends SherlockFragmentActivity {
updateProgressBarVisibility();
}
};
private void updateProgressBarVisibility() {
if (DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds()) {
if (DownloadService.isRunning
&& DownloadRequester.getInstance().isDownloadingFeeds()) {
setSupportProgressBarIndeterminateVisibility(true);
} else {
setSupportProgressBarIndeterminateVisibility(false);
}
invalidateOptionsMenu();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add_feed:
startActivity(new Intent(this, AddFeedActivity.class));
return true;
case R.id.all_feed_refresh:
manager.refreshAllFeeds(this);
return true;
case R.id.show_downloads:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.show_preferences:
startActivity(new Intent(this, PreferenceActivity.class));
return true;
case R.id.show_player:
startActivity(new Intent(this, MediaplayerActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
switch (item.getItemId()) {
case R.id.add_feed:
startActivity(new Intent(this, AddFeedActivity.class));
return true;
case R.id.all_feed_refresh:
manager.refreshAllFeeds(this);
return true;
case R.id.show_downloads:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.show_preferences:
startActivity(new Intent(this, PreferenceActivity.class));
return true;
case R.id.show_player:
startActivity(new Intent(this, MediaplayerActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem refreshAll = menu.findItem(R.id.all_feed_refresh);
if (DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds()) {
if (DownloadService.isRunning
&& DownloadRequester.getInstance().isDownloadingFeeds()) {
refreshAll.setVisible(false);
} else {
refreshAll.setVisible(true);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.podfetcher, menu);
return true;
inflater.inflate(R.menu.podfetcher, menu);
return true;
}
/** TabListener for navigating between the main lists. */
private class TabListener<T extends Fragment> implements
ActionBar.TabListener {
public static class MainPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_ITEMS = 3;
private final Activity activity;
private final String tag;
private final Class<T> fClass;
private Fragment fragment;
private static final int POS_FEEDLIST = 0;
private static final int POS_NEW_ITEMS = 1;
private static final int POS_QUEUE = 2;
public TabListener(Activity activity, String tag, Class<T> fClass) {
this.activity = activity;
this.tag = tag;
this.fClass = fClass;
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@SuppressWarnings("unused")
public TabListener(Activity activity, String tag, Fragment fragment,
Class<T> fClass) {
super();
this.activity = activity;
this.tag = tag;
this.fragment = fragment;
this.fClass = fClass;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
fragment = Fragment.instantiate(activity, fClass.getName());
ft.replace(R.id.main_fragment, fragment);
} else {
ft.attach(fragment);
@Override
public Fragment getItem(int position) {
switch (position) {
case POS_FEEDLIST:
return new FeedlistFragment();
case POS_NEW_ITEMS:
return new UnreadItemlistFragment();
case POS_QUEUE:
return new QueueFragment();
default:
return null;
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Do nothing
}
}
}