diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 75e5917..9fb401f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,16 +9,23 @@ android:theme="@style/AppTheme" > + + + + + android:parentActivityName=".Welcome" > @@ -28,6 +35,7 @@ android:label="@string/app_name" > + diff --git a/app/src/main/java/com/kauron/dungeonmanager/PagerAdapter.java b/app/src/main/java/com/kauron/dungeonmanager/PagerAdapter.java new file mode 100644 index 0000000..d83c8ed --- /dev/null +++ b/app/src/main/java/com/kauron/dungeonmanager/PagerAdapter.java @@ -0,0 +1,40 @@ +package com.kauron.dungeonmanager; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +import java.util.List; + +/** + * The PagerAdapter serves the fragments when paging. + * @author mwho + */ +public class PagerAdapter extends FragmentPagerAdapter { + + private List fragments; + /** + * @param fm + * @param fragments + */ + public PagerAdapter(FragmentManager fm, List fragments) { + super(fm); + this.fragments = fragments; + } + + /* (non-Javadoc) + * @see android.support.v4.app.FragmentPagerAdapter#getItem(int) + */ + @Override + public Fragment getItem(int position) { + return this.fragments.get(position); + } + + /* (non-Javadoc) + * @see android.support.v4.view.PagerAdapter#getCount() + */ + @Override + public int getCount() { + return this.fragments.size(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kauron/dungeonmanager/TabsViewPagerFragmentActivity.java b/app/src/main/java/com/kauron/dungeonmanager/TabsViewPagerFragmentActivity.java new file mode 100644 index 0000000..a2ecca2 --- /dev/null +++ b/app/src/main/java/com/kauron/dungeonmanager/TabsViewPagerFragmentActivity.java @@ -0,0 +1,176 @@ +package com.kauron.dungeonmanager; + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.view.ViewPager; +import android.view.View; +import android.widget.TabHost; +import android.widget.TabHost.TabContentFactory; + +import java.util.HashMap; +import java.util.List; +import java.util.Vector; + +/** + * The TabsViewPagerFragmentActivity class implements the Fragment activity that maintains a TabHost using a ViewPager. + * @author mwho + */ +public class TabsViewPagerFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { + + private TabHost mTabHost; + private ViewPager mViewPager; + private HashMap mapTabInfo = new HashMap(); + private PagerAdapter mPagerAdapter; + /** + * + * @author mwho + * Maintains extrinsic info of a tab's construct + */ + private class TabInfo { + private String tag; + private Class clss; + private Bundle args; + private Fragment fragment; + TabInfo(String tag, Class clazz, Bundle args) { + this.tag = tag; + this.clss = clazz; + this.args = args; + } + + } + /** + * A simple factory that returns dummy views to the Tabhost + * @author mwho + */ + class TabFactory implements TabContentFactory { + + private final Context mContext; + + /** + * @param context + */ + public TabFactory(Context context) { + mContext = context; + } + + /** (non-Javadoc) + * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String) + */ + public View createTabContent(String tag) { + View v = new View(mContext); + v.setMinimumWidth(0); + v.setMinimumHeight(0); + return v; + } + + } + /** (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) + */ + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Inflate the layout + setContentView(R.layout.tabs_viewpager_layout); + // Initialise the TabHost + this.initialiseTabHost(savedInstanceState); + if (savedInstanceState != null) { + mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state + } + // Intialise ViewPager + this.intialiseViewPager(); + } + + /** (non-Javadoc) + * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle) + */ + protected void onSaveInstanceState(Bundle outState) { + outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected + super.onSaveInstanceState(outState); + } + + /** + * Initialise ViewPager + */ + private void intialiseViewPager() { + + List fragments = new Vector<>(); + fragments.add(Fragment.instantiate(this, ShowPlayer.class.getName())); + fragments.add(Fragment.instantiate(this, PowerEditor.class.getName())); + fragments.add(Fragment.instantiate(this, PlayerEditor.class.getName())); + this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); + // + this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager); + this.mViewPager.setAdapter(this.mPagerAdapter); + this.mViewPager.setOnPageChangeListener(this); + } + + /** + * Initialise the Tab Host + */ + private void initialiseTabHost(Bundle args) { + mTabHost = (TabHost)findViewById(android.R.id.tabhost); + mTabHost.setup(); + TabInfo tabInfo = null; + TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", ShowPlayer.class, args))); + this.mapTabInfo.put(tabInfo.tag, tabInfo); + TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", PowerEditor.class, args))); + this.mapTabInfo.put(tabInfo.tag, tabInfo); + TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", PlayerEditor.class, args))); + this.mapTabInfo.put(tabInfo.tag, tabInfo); + // Default to first tab + //this.onTabChanged("Tab1"); + // + mTabHost.setOnTabChangedListener(this); + } + + /** + * Add Tab content to the Tabhost + * @param activity + * @param tabHost + * @param tabSpec + */ + private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { + // Attach a Tab view factory to the spec + tabSpec.setContent(activity.new TabFactory(activity)); + tabHost.addTab(tabSpec); + } + + /** (non-Javadoc) + * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String) + */ + public void onTabChanged(String tag) { + //TabInfo newTab = this.mapTabInfo.get(tag); + int pos = this.mTabHost.getCurrentTab(); + this.mViewPager.setCurrentItem(pos); + } + + /* (non-Javadoc) + * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int) + */ + @Override + public void onPageScrolled(int position, float positionOffset, + int positionOffsetPixels) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int) + */ + @Override + public void onPageSelected(int position) { + // TODO Auto-generated method stub + this.mTabHost.setCurrentTab(position); + } + + /* (non-Javadoc) + * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int) + */ + @Override + public void onPageScrollStateChanged(int state) { + // TODO Auto-generated method stub + + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/tabs_viewpager_layout.xml b/app/src/main/res/layout/tabs_viewpager_layout.xml new file mode 100644 index 0000000..0cdd38f --- /dev/null +++ b/app/src/main/res/layout/tabs_viewpager_layout.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 30ce477..5f45f8b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -191,5 +191,14 @@ Use There are no powers, try creating one Show attack/defense + Display + + Section 1 + Section 2 + Section 3 + + Hello world! + + Settings