Android rolling Tab

Source: Internet
Author: User

View results:

 

 

If you are interested in the effect, please be patient. It is not complicated.
Android-support-v4.jar support is required.


--------------------------------------------------------------------------------
Main layout file activity_main.xml


1234567 <android. support. v4.view. ViewPager xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/pager"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Tools: context = ". MainActivity">
</Android. support. v4.view. ViewPager>

 

--------------------------------------------------------------------------------
Main Activity, the core is to set an Adapter for mViewPager -- mFragmentAdapter

1234567891011121314151617 public class MainActivity extends FragmentActivity {
Private FragmentAdapter mFragmentAdapter;
Private ViewPager mViewPager;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MFragmentAdapter = new FragmentAdapter (MainActivity. this, getSupportFragmentManager ());
MViewPager = (ViewPager) findViewById (R. id. pager );
MViewPager. setAdapter (mFragmentAdapter );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}

 

--------------------------------------------------------------------------------

Next let's take a look at how the FragmentAdapter is implemented.

123456789101112131415161718192021222324252627282930313233 public class FragmentAdapter extends FragmentPagerAdapter {
Private Context mContext;
Public FragmentAdapter (Context context, FragmentManager fm ){
Super (fm );
MContext = context;
}
@ Override
Public Fragment getItem (int position ){
Fragment fragment;
If (position = 2 ){
Fragment = new AboutMeFragment ();
} Else {
Fragment = new TabFragment ();
Bundle args = new Bundle ();
Args. putInt (TabFragment. TAB_POSITION, position + 1 );
Fragment. setArguments (args );
}
Return fragment;
}
@ Override
Public int getCount (){
Return 3;
}
@ Override
Public CharSequence getPageTitle (int position ){
If (position = 2 ){
Return mContext. getString (R. string. about_me_title );
} Else {
Locale l = Locale. getDefault ();
Return mContext. getString (R. string. tab_fragment_title, position + 1). toUpperCase (l );
}
}
}

In getCount (), three tabs are set. When position = 2 is the last page, I set an about_me page different from the first two pages. it mainly indicates that different types of Fragment can be set to the Adapter as needed. the two types of Fragment are TabFragment () and AboutMeFragment ()

 

--------------------------------------------------------------------------------
The layout file of TabFragment has only one TextView, so it will not be pasted. You can directly view the java file.


1234567891011121314151617181920 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. TextView;
Import com. licd. remind. R;
Public class TabFragment extends Fragment {
Public static final String TAB_POSITION = "tab_position ";
Public TabFragment (){
}
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
View rootView = inflater. inflate (R. layout. fragment_tab, container, false );
TextView tabLabel = (TextView) rootView. findViewById (R. id. tab_label );
TabLabel. setText (getString (R. string. tab_fragment_content, getArguments (). getInt (TAB_POSITION )));
Return rootView;
}
}

 

--------------------------------------------------------------------------------
The layout file fragment_tab.xml of AboutMeFragment is also a TextView, but android: drawableTop = "" attribute is used.

123456789101112 <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center_horizontal | center_vertical"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: drawableTop = "@ drawable/me"
Android: text = "@ string/about_me_email"/>
</LinearLayout>

Then AboutMeFragment. java


1234567891011121314 import android. OS. Bundle;
Import android. support. v4.app. Fragment;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import com. licd. remind. R;
Public class AboutMeFragment extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
View rootView = inflater. inflate (R. layout. fragment_about_me, container, false );
Return rootView;
}
}

 

--------------------------------------------------------------------------------
The replacement method is used in the resource file strings. xml.

123456789 <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources xmlns: xliff = "urn: oasis: names: tc: xliff: document: 1.2">
<String name = "app_name"> Remind </string>
<String name = "action_settings"> Settings </string>
<String name = "about_me_title"> about me </string>
<String name = "about_me_email"> chain_li7@163.com </string>
<String name = "tab_fragment_title"> Number of <xliff: gid = "tab_fragment_title_position"> % 1 $ d </xliff: g> tabs </string>
<String name = "tab_fragment_content"> <xliff: gid = "tab_fragment_content_position"> % 1 $ d </xliff: g> Tab pages </string>
</Resources>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.