Stephen Kong
- Forum posts: 17
Oct 11, 2011, 2:40:30 AM via Website
Oct 11, 2011 2:40:30 AM via Website
I am new to Android development. Recently I have been trying to rewrite the following tutorial with Fragment class. The tutorial utilizs the old TabActivity class and TabHost and TabWidget annontations.
TabLayout Google Tutorial
I following some online Fragment sample codes, I converted all my Activity class with Fragment class, but I couldn't make it to work.
Here are few questions
1. Should I define the <Fragment> class or calling ActionBar.addBar(...), or both?
2. What would a complete res/layout/main.xml look like? What would be the root element (i.e. LinearLayout, Framelayout..etc)?
3. Any additional info would be grateful.
res/layout/main.xml
HelloTabWidget.java
TabLayout Google Tutorial
I following some online Fragment sample codes, I converted all my Activity class with Fragment class, but I couldn't make it to work.
Here are few questions
1. Should I define the <Fragment> class or calling ActionBar.addBar(...), or both?
2. What would a complete res/layout/main.xml look like? What would be the root element (i.e. LinearLayout, Framelayout..etc)?
3. Any additional info would be grateful.
res/layout/main.xml
1<?xml version="1.0" encoding="utf-8"?>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <LinearLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:padding="5dp">
11 <TabWidget
12 android:id="@android:id/tabs"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content" />
15
16 <FrameLayout
17 android:id="@android:id/tabcontent"
18 android:layout_width="fill_parent"
19 android:layout_height="fill_parent"
20 android:padding="5dp" >
21
22 <fragment class="home.net.AlbumsActivity"
23 android:id="@+id/Albums"
24 android:tag="Albums"
25 android:name="Albums"
26 android:layout_width="match_parent" android:layout_height="match_parent" />
27 <fragment class="home.net.ArtistsActivity"
28 android:id="@+id/Artists"
29 android:tag="Artists"
30 android:name="Artists"
31 android:layout_width="match_parent" android:layout_height="match_parent" />
32 <fragment class="home.net.SongsActivity"
33 android:id="@+id/Songs"
34 android:tag="Songs"
35 android:name="Songs"
36 android:layout_width="match_parent" android:layout_height="match_parent" />
37
38 </FrameLayout>
39 </LinearLayout>
40</TabHost>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <LinearLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:padding="5dp">
11 <TabWidget
12 android:id="@android:id/tabs"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content" />
15
16 <FrameLayout
17 android:id="@android:id/tabcontent"
18 android:layout_width="fill_parent"
19 android:layout_height="fill_parent"
20 android:padding="5dp" >
21
22 <fragment class="home.net.AlbumsActivity"
23 android:id="@+id/Albums"
24 android:tag="Albums"
25 android:name="Albums"
26 android:layout_width="match_parent" android:layout_height="match_parent" />
27 <fragment class="home.net.ArtistsActivity"
28 android:id="@+id/Artists"
29 android:tag="Artists"
30 android:name="Artists"
31 android:layout_width="match_parent" android:layout_height="match_parent" />
32 <fragment class="home.net.SongsActivity"
33 android:id="@+id/Songs"
34 android:tag="Songs"
35 android:name="Songs"
36 android:layout_width="match_parent" android:layout_height="match_parent" />
37
38 </FrameLayout>
39 </LinearLayout>
40</TabHost>
HelloTabWidget.java
1public class HelloTabWidget extends Activity {
2
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.main);
6
7 ActionBar actionBar = getActionBar();
8 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
9
10 actionBar.addTab(actionBar.newTab()
11 .setText("Albums")
12 .setTabListener(new TabListener<AlbumsActivity>(
13 this, "Albums", AlbumsActivity.class)));
14 actionBar.addTab(actionBar.newTab()
15 .setText("Artists")
16 .setTabListener(new TabListener<ArtistsActivity>(
17 this, "Artists", ArtistsActivity.class)));
18 actionBar.addTab(actionBar.newTab()
19 .setText("Songs")
20 .setTabListener(new TabListener<SongsActivity>(
21 this, "Songs", SongsActivity.class)));
22 /*
23 if (savedInstanceState != null) {
24 actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
25 }
26*/
27 }
28
29 protected void onSaveInstanceState(Bundle outState) {
30 super.onSaveInstanceState(outState);
31 outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
32 }
33
34 public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
35 private final Activity mActivity;
36 private final String mTag;
37 private final Class<T> mClass;
38 private final Bundle mArgs;
39 private Fragment mFragment;
40
41 public TabListener(Activity activity, String tag, Class<T> clz) {
42 this(activity, tag, clz, null);
43 }
44
45 public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
46 mActivity = activity;
47 mTag = tag;
48 mClass = clz;
49 mArgs = args;
50
51 // Check to see if we already have a fragment for this tab, probably
52 // from a previously saved state. If so, deactivate it, because our
53 // initial state is that a tab isn't shown.
54 mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
55
56 if (mFragment != null && !mFragment.isDetached()) {
57 FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
58 ft.detach(mFragment);
59 ft.commit();
60 }
61
62 }
63
64 public void onTabSelected(Tab tab, FragmentTransaction ft) {
65 if (mFragment == null) {
66 mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
67 ft.add(android.R.id.content, mFragment, mTag);
68 } else {
69 ft.attach(mFragment);
70 }
71 }
72
73 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
74 if (mFragment != null) {
75 ft.detach(mFragment);
76 }
77 }
78
79 public void onTabReselected(Tab tab, FragmentTransaction ft) {
80 Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
81 }
82 }
83
84}
2
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.main);
6
7 ActionBar actionBar = getActionBar();
8 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
9
10 actionBar.addTab(actionBar.newTab()
11 .setText("Albums")
12 .setTabListener(new TabListener<AlbumsActivity>(
13 this, "Albums", AlbumsActivity.class)));
14 actionBar.addTab(actionBar.newTab()
15 .setText("Artists")
16 .setTabListener(new TabListener<ArtistsActivity>(
17 this, "Artists", ArtistsActivity.class)));
18 actionBar.addTab(actionBar.newTab()
19 .setText("Songs")
20 .setTabListener(new TabListener<SongsActivity>(
21 this, "Songs", SongsActivity.class)));
22 /*
23 if (savedInstanceState != null) {
24 actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
25 }
26*/
27 }
28
29 protected void onSaveInstanceState(Bundle outState) {
30 super.onSaveInstanceState(outState);
31 outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
32 }
33
34 public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
35 private final Activity mActivity;
36 private final String mTag;
37 private final Class<T> mClass;
38 private final Bundle mArgs;
39 private Fragment mFragment;
40
41 public TabListener(Activity activity, String tag, Class<T> clz) {
42 this(activity, tag, clz, null);
43 }
44
45 public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
46 mActivity = activity;
47 mTag = tag;
48 mClass = clz;
49 mArgs = args;
50
51 // Check to see if we already have a fragment for this tab, probably
52 // from a previously saved state. If so, deactivate it, because our
53 // initial state is that a tab isn't shown.
54 mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
55
56 if (mFragment != null && !mFragment.isDetached()) {
57 FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
58 ft.detach(mFragment);
59 ft.commit();
60 }
61
62 }
63
64 public void onTabSelected(Tab tab, FragmentTransaction ft) {
65 if (mFragment == null) {
66 mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
67 ft.add(android.R.id.content, mFragment, mTag);
68 } else {
69 ft.attach(mFragment);
70 }
71 }
72
73 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
74 if (mFragment != null) {
75 ft.detach(mFragment);
76 }
77 }
78
79 public void onTabReselected(Tab tab, FragmentTransaction ft) {
80 Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
81 }
82 }
83
84}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.