From 9fe8750ecba38e2c2033d8ef964775a40ecee921 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sat, 5 Sep 2015 17:17:05 -0400 Subject: [PATCH] changed method of tab use --- app/build.gradle | 1 + .../adapter/EpisodesPagerAdapter.java | 59 +++++++++++++++++++ .../antennapod/fragment/EpisodesFragment.java | 23 +++----- app/src/main/res/layout/episodes_fragment.xml | 38 ++++-------- core/build.gradle | 2 +- 5 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/adapter/EpisodesPagerAdapter.java diff --git a/app/build.gradle b/app/build.gradle index 90d0c108e..9c5d97a59 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,6 +12,7 @@ dependencies { compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:gridlayout-v7:22.2.1' compile 'com.android.support:cardview-v7:22.2.1' + compile 'com.android.support:design:22.2.1' compile 'org.apache.commons:commons-lang3:3.3.2' compile('org.shredzone.flattr4j:flattr4j-core:2.12') { exclude group: 'org.json', module: 'json' diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/EpisodesPagerAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/EpisodesPagerAdapter.java new file mode 100644 index 000000000..8114bc123 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/adapter/EpisodesPagerAdapter.java @@ -0,0 +1,59 @@ +package de.danoeh.antennapod.adapter; + +import android.content.Context; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.MainActivity; +import de.danoeh.antennapod.fragment.AllEpisodesFragment; +import de.danoeh.antennapod.fragment.NewEpisodesFragment; + +public class EpisodesPagerAdapter extends FragmentPagerAdapter { + + private final Context context; + + private String tabTags[] = new String[] {AllEpisodesFragment.TAG, NewEpisodesFragment.TAG}; + private String tabTitles[] = new String[tabTags.length]; + + public EpisodesPagerAdapter(FragmentManager fm, Context context) { + super(fm); + this.context = context; + tabTitles = new String[tabTags.length]; + for (int i = 0; i < tabTags.length; i++) { + String title = null; + switch (tabTags[i]) { + case AllEpisodesFragment.TAG: + title = context.getResources().getString(R.string.all_episodes_label); + break; + case NewEpisodesFragment.TAG: + title = context.getResources().getString(R.string.new_episodes_label); + break; + } + tabTitles[i] = title; + } + } + + @Override + public Fragment getItem(int position) { + String tag = tabTags[position]; + switch (tag) { + case AllEpisodesFragment.TAG: + return new AllEpisodesFragment(); + case NewEpisodesFragment.TAG: + return new NewEpisodesFragment(); + } + return null; + } + + @Override + public int getCount() { + return tabTags.length; + } + + @Override + public CharSequence getPageTitle(int position) { + return tabTitles[position]; + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesFragment.java index 698e1660b..a9ccd4cc1 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesFragment.java @@ -1,20 +1,21 @@ package de.danoeh.antennapod.fragment; import android.os.Bundle; +import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTabHost; +import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.adapter.EpisodesPagerAdapter; public class EpisodesFragment extends Fragment { public static final String TAG = "EpisodesFragment"; - private FragmentTabHost mTabHost; - //Mandatory Constructor public EpisodesFragment() { } @@ -22,26 +23,18 @@ public class EpisodesFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); - } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.episodes_fragment, container, false); + ViewPager pager = (ViewPager)rootView.findViewById(R.id.viewpager); + pager.setAdapter(new EpisodesPagerAdapter(getChildFragmentManager(), getActivity())); - - mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost); - mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); - - - mTabHost.addTab(mTabHost.newTabSpec(NewEpisodesFragment.TAG).setIndicator( - getResources().getString(R.string.new_episodes_label)), - NewEpisodesFragment.class, null); - - mTabHost.addTab(mTabHost.newTabSpec(AllEpisodesFragment.TAG).setIndicator( - getResources().getString(R.string.all_episodes_label)), - AllEpisodesFragment.class, null); + // Give the TabLayout the ViewPager + TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.sliding_tabs); + tabLayout.setupWithViewPager(pager); return rootView; } diff --git a/app/src/main/res/layout/episodes_fragment.xml b/app/src/main/res/layout/episodes_fragment.xml index 70b288cdf..736a874b8 100644 --- a/app/src/main/res/layout/episodes_fragment.xml +++ b/app/src/main/res/layout/episodes_fragment.xml @@ -1,33 +1,21 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - - - + app:tabMode="scrollable" /> - - - + android:layout_height="0px" + android:layout_weight="1" + android:background="@android:color/white" /> - - \ No newline at end of file + \ No newline at end of file diff --git a/core/build.gradle b/core/build.gradle index b88780772..4b1ab98a6 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -36,7 +36,7 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:22.2.1' compile 'com.android.support:appcompat-v7:22.2.1' - compile 'com.android.support:support-v4:22.2.1' + compile 'com.android.support:design:22.2.1' compile 'org.apache.commons:commons-lang3:3.3.2' compile ('org.shredzone.flattr4j:flattr4j-core:2.12') { exclude group: 'org.json', module: 'json'