Escolar Documentos
Profissional Documentos
Cultura Documentos
You can implement lateral navigation with tabs that represent each screen. Tabs appear across the
top of a screen, as shown on the left side of the figure above, in order to provide navigation to other
screens. Tab navigation is a very popular solution for lateral navigation from one child screen to
another child screen that is a sibling—in the same position in the hierarchy and sharing the same
parent screen. Tab navigation is often combined with the ability to swipe child screens left-to-right
and right-to-left.
The primary class used for displaying tabs is TabLayout in the Android Design Support Library. It
provides a horizontal layout to display tabs. You can show the tabs below the app bar, and use
the PagerAdapter class to populate screens "pages" inside of a ViewPager. ViewPager is a layout
manager that lets the user flip left and right through screens. This is a common pattern for
presenting different screens of content within an Activity—use an adapter to fill the content screen to
Page 1 of 8
show in the Activity, and a layout manager that changes the content screens depending on which tab
is selected.
You implement PagerAdapter to generate the screens that the view shows. ViewPager is most often
used in conjunction with Fragment. By using a Fragment, you have a convenient way to manage the
lifecycle of a screen "page".
The following are standard adapters for using fragments with the ViewPager:
If Android Studio suggests a version with a higher number, edit the line above to update the version.
3. In order to use a Toolbar rather than an app bar and app title, add the following attributes to
the res > values > styles.xml file to hide the app bar and the title:
4. Open activity_main.xml layout file, and click the Text tab to view the XML code.
5. Change the ConstraintLayout to RelativeLayout, as you've done in previous exercises.
6. Add the android:id attribute and android:padding of 16dp to the RelativeLayout.
7. Remove the TextView supplied by the template, and add a Toolbar, a TabLayout, and
a ViewPagerwithin the RelativeLayout as shown in the code below.
Page 2 of 8
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="com.example.android.tabexperiment.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
As you enter the app:popupTheme attribute for Toolbar, app will be in red if you didn't add the
following statement to RelativeLayout:
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
You can click app and press Option+Enter (or Alt+Enter), and Android Studio automatically adds the
statement.
Page 3 of 8
5. Change the Fragment Layout Name for the XML file to tab_fragment1.
6. Clear the Include fragment factory methods? option and the Include interface
callbacks? option. You don't need these methods.
7. Click Finish.
Repeat the steps above, using TabFragment2 and TabFragment3 for Step 3,
and tab_fragment2 and tab_fragment3 for Step 4.
Each fragment is created with its class definition set to extend Fragment. Also,
each Fragment inflates the layout associated with the screen (tab_fragment1, tab_fragment2,
and tab_fragment3), using the familiar resource-inflate design pattern you learned in a previous
chapter with the options menu.
public TabFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment.
return inflater.inflate(R.layout.tab_fragment1, container, false);
}
Repeat the steps above for each fragment layout XML file, entering different text for the TextView in
step 2:
Text for the TextView in tab_fragment2.xml: "Tech news you can use: "
Text for the TextView in tab_fragment3.xml: "Cooking tips: "
Examine each fragment layout XML file. For example, tab_fragment1 should look like this:
Page 4 of 8
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.tabexperiment.TabFragment1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="These are the top stories: "
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
4. In the Fragment layout XML file tab_fragment1, extract the string for "These are the top
stories:" into the string resource tab_1. Do the same for the strings in tab_fragment2,
and tab_fragment3.
Follow these steps to add a new PagerAdapter class to the app that
extends FragmentStatePagerAdapter and defines the number of tabs (mNumOfTabs):
/**
Page 5 of 8
* Return the Fragment associated with a specified position.
*
* @param position
*/
@Override
public Fragment getItem(int position) {
return null;
}
/**
* Return the number of views available.
*/
@Override
public int getCount() {
return 0;
}
}
While entering the code above, Android Studio automatically imports the following:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
If FragmentManager in the code is in red, a red light bulb icon should appear when you click it. Click
the bulb icon and choose Import class. Import choices appear. Select FragmentManager
(android.support.v4).
8. Change the newly added getItem() method to the following, which uses a switch case block to
return the Fragment to show based on which tab is clicked:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new TabFragment1();
case 1: return new TabFragment2();
case 2: return new TabFragment3();
default: return null;
}
}
9. Change the newly added getCount() method to the following to return the number of tabs:
@Override
public int getCount() {
return mNumOfTabs;
}
Page 6 of 8
1. Open MainActivity and add the following code inside the onCreate() method to inflate
the Toolbar using setSupportActionBar():
protected void onCreate(Bundle savedInstanceState) {
// ... Code inside onCreate() method
android.support.v7.widget.Toolbar toolbar =
findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create an instance of the tab layout from the view.
}
2. Open strings.xml, and create the following string resources:
<string name="tab_label1">Top Stories</string>
<string name="tab_label2">Tech News</string>
<string name="tab_label3">Cooking</string>
3. At the end of the onCreate() method, create an instance of the tab layout from
the tab_layout element in the layout, and set the text for each tab using addTab():
// Create an instance of the tab layout from the view.
TabLayout tabLayout = findViewById(R.id.tab_layout);
// Set the text for each tab.
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label2));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label3));
// Set the tabs to fill the entire layout.
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// Use PagerAdapter to manage page views in fragments.
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Page 7 of 8
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
3. Run the app. Tap each tab to see each "page" (screen). You should also be able to swipe
left and right to visit the different "pages".
Page 8 of 8