1
0
Fork 0

First approach to Tabs with PagerView

Not working multiple menus and animations and theme of the TabWidget
This commit is contained in:
Carlos Galindo 2015-05-13 12:58:21 +02:00
parent 6ab167fb6d
commit 63ae84f1f5
9 changed files with 312 additions and 25 deletions

View file

@ -16,7 +16,7 @@
</activity> </activity>
<activity <activity
android:name=".TabsViewPagerFragmentActivity" android:name=".Display"
android:parentActivityName=".Welcome"> android:parentActivityName=".Welcome">
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"

View file

@ -3,29 +3,31 @@ package com.kauron.dungeonmanager;
import android.content.Context; import android.content.Context;
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.FragmentActivity;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TabHost; import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory; import android.widget.TabHost.TabContentFactory;
import android.widget.Toast;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
/** /**
* The <code>TabsViewPagerFragmentActivity</code> class implements the Fragment activity that maintains a TabHost using a ViewPager. * The <code>Display</code> class implements the Fragment activity that maintains a TabHost using a ViewPager.
* @author mwho
*/ */
public class TabsViewPagerFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { public class Display extends ActionBarActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private TabHost mTabHost; private TabHost mTabHost;
private ViewPager mViewPager; private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>(); private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, Display.TabInfo>();
private PagerAdapter mPagerAdapter; private PagerAdapter mPagerAdapter;
/** /**
* *
* @author mwho
* Maintains extrinsic info of a tab's construct * Maintains extrinsic info of a tab's construct
*/ */
private class TabInfo { private class TabInfo {
@ -42,7 +44,6 @@ public class TabsViewPagerFragmentActivity extends FragmentActivity implements T
} }
/** /**
* A simple factory that returns dummy views to the Tabhost * A simple factory that returns dummy views to the Tabhost
* @author mwho
*/ */
class TabFactory implements TabContentFactory { class TabFactory implements TabContentFactory {
@ -72,16 +73,47 @@ public class TabsViewPagerFragmentActivity extends FragmentActivity implements T
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Inflate the layout // Inflate the layout
setContentView(R.layout.tabs_viewpager_layout); setContentView(R.layout.display);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Kauron");
toolbar.setSubtitle("Brujo Tiflin");
setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
// Initialise the TabHost // Initialise the TabHost
this.initialiseTabHost(savedInstanceState); this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) { if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
} }
// Intialise ViewPager // Initialise ViewPager
this.intialiseViewPager(); this.intialiseViewPager();
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
if ( mTabHost.getCurrentTabTag().equals("Stats") ) {
getMenuInflater().inflate(R.menu.menu_welcome, menu);
} else if ( mTabHost.getCurrentTabTag().equals("Powers") ) {
getMenuInflater().inflate(R.menu.menu_show_player, menu);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if ( id == R.id.action_add_player ) {
Toast.makeText(getApplicationContext(), "it works", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
/** (non-Javadoc) /** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle) * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/ */
@ -96,9 +128,8 @@ public class TabsViewPagerFragmentActivity extends FragmentActivity implements T
private void intialiseViewPager() { private void intialiseViewPager() {
List<Fragment> fragments = new Vector<>(); List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, ShowPlayer.class.getName())); fragments.add(Fragment.instantiate(this, PlayerDisplay.class.getName()));
fragments.add(Fragment.instantiate(this, PowerEditor.class.getName())); fragments.add(Fragment.instantiate(this, PowerDisplay.class.getName()));
fragments.add(Fragment.instantiate(this, PlayerEditor.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
// //
this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager); this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
@ -113,11 +144,9 @@ public class TabsViewPagerFragmentActivity extends FragmentActivity implements T
mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(); mTabHost.setup();
TabInfo tabInfo = null; TabInfo tabInfo = null;
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", ShowPlayer.class, args))); Display.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Stats").setIndicator("Stats"), (tabInfo = new TabInfo("Stats", PlayerDisplay.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo); 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))); Display.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Powers").setIndicator("Powers"), (tabInfo = new TabInfo("Powers", PowerDisplay.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); this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab // Default to first tab
//this.onTabChanged("Tab1"); //this.onTabChanged("Tab1");
@ -131,7 +160,7 @@ public class TabsViewPagerFragmentActivity extends FragmentActivity implements T
* @param tabHost * @param tabHost
* @param tabSpec * @param tabSpec
*/ */
private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { private static void AddTab(Display activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec // Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity)); tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec); tabHost.addTab(tabSpec);

View file

@ -0,0 +1,28 @@
package com.kauron.dungeonmanager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class PlayerDisplay extends Fragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout) inflater.inflate(R.layout.fragment_player_display, container, false);
}
}

View file

@ -0,0 +1,28 @@
package com.kauron.dungeonmanager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class PowerDisplay extends Fragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout) inflater.inflate(R.layout.fragment_power_display, container, false);
}
}

View file

@ -166,7 +166,7 @@ public class Welcome extends ActionBarActivity {
//TODO: export as files //TODO: export as files
/**TEMP*/ /**TEMP*/
startActivity(new Intent( startActivity(new Intent(
getApplicationContext(), TabsViewPagerFragmentActivity.class getApplicationContext(), Display.class
).putExtra("player", position)); ).putExtra("player", position));
Toast.makeText( Toast.makeText(
activity, "Exporting feature not implemented yet", Toast.LENGTH_LONG) activity, "Exporting feature not implemented yet", Toast.LENGTH_LONG)

View file

@ -1,9 +1,19 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent"> android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<TabHost <TabHost
android:id="@android:id/tabhost" android:id="@android:id/tabhost"
android:layout_width="fill_parent" android:layout_width="fill_parent"
@ -16,6 +26,7 @@
android:id="@android:id/tabs" android:id="@android:id/tabs"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:background="@color/primary"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0" /> android:layout_weight="0" />

View file

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/attackContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/titleAttack"
android:text="Attack"
android:textAllCaps="true"
android:drawableBottom="?android:attr/listChoiceBackgroundIndicator"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="?android:textColorSecondary"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:paddingRight="8dip"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/attackGrid"
android:columnCount="3"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="0"
android:layout_column="0"
android:id="@+id/STR"
android:text="@string/STR" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="1"
android:layout_column="0"
android:id="@+id/CON"
android:text="@string/CON" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="0"
android:layout_column="1"
android:id="@+id/DEX"
android:text="@string/DEX" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="1"
android:layout_column="1"
android:id="@+id/INT"
android:text="@string/INT" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="0"
android:layout_column="2"
android:id="@+id/WIS"
android:text="@string/WIS" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="4"
android:layout_row="1"
android:layout_column="2"
android:id="@+id/CHA"
android:text="@string/CHA" />
</GridLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/defenseContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/titleDefense"
android:text="Defense"
android:textAllCaps="true"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="?android:textColorSecondary"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:paddingRight="8dip"/>
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/defenseGrid"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="5"
android:layout_row="0"
android:layout_column="0"
android:id="@+id/CA"
android:text="AC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="5"
android:layout_row="0"
android:layout_column="1"
android:id="@+id/FORT"
android:text="FORT" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="5"
android:layout_row="1"
android:layout_column="0"
android:id="@+id/REF"
android:text="REF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="monospace"
android:ems="5"
android:layout_row="1"
android:layout_column="1"
android:id="@+id/VOL"
android:text="VOL" />
</GridLayout>
</LinearLayout>
</LinearLayout>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red">
</LinearLayout>

View file

@ -10,8 +10,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:orientation="vertical" android:orientation="vertical">
android:visibility="gone">
<TextView <TextView
android:id="@+id/titleAttack" android:id="@+id/titleAttack"
@ -108,12 +107,11 @@
</HorizontalScrollView> </HorizontalScrollView>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/defenseContainer" android:id="@+id/defenseContainer"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:visibility="gone"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView