使用ViewPager和Fragment实现滑动导航

ViewPage是android-support-v4.jar包提供的用于页面滑动的库,android-support-v4.jar是google推荐使用的一个类库,在项目中使用之前,你必须其添加到项目中(项目点右键Build path->configure build path,然后找到jar进行添加)

1.在xml布局文件中添加android.support.v4.view.ViewPager容器及显示导航所用标签android.support.v4.view.PagerTitleStrip,如我添加的xml内容如下

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
< 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" >
 
     <!--
     This title strip will display the currently visible page title, as well as the page
     titles for adjacent pages.
     -->
 
     < android.support.v4.view.PagerTitleStrip
         android:id = "@+id/pager_title_strip"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:layout_gravity = "top"
         android:background = "#33b5e5"
         android:paddingBottom = "4dp"
         android:paddingTop = "4dp"
         android:textColor = "#fff" />
 
</ android.support.v4.view.ViewPager >

 

2.在activity中导入以下包 

?
1
2
3
4
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

3.声明变量

?
1
2
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

 

4.在onCreate中对其进行初始化 

?
1
2
3
4
5
6
mSectionsPagerAdapter =  new SectionsPagerAdapter(
                 getSupportFragmentManager());
 
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

5.添加类SectionsPagerAdapter,我这里使用了3个标签 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class SectionsPagerAdapter  extends FragmentPagerAdapter {
 
         public SectionsPagerAdapter(FragmentManager fm) {
             super (fm);
         }
 
         @Override
         public Fragment getItem( int position) {
             // getItem is called to instantiate the fragment for the given page.
             // Return a DummySectionFragment (defined as a static inner class
             // below) with the page number as its lone argument.
             Fragment fragment =  new HjFragment();
             Bundle args =  new Bundle();
             args.putInt( "no" , position +  1 );
             fragment.setArguments(args);
 
             return fragment;
         }
 
         @Override
         public int getCount() {
             // Show 3 total pages.
             return 3 ;
         }
 
         @Override
         public CharSequence getPageTitle( int position) {
             switch (position) {
             case 0 :
                 return "标签1" ;
             case 1 :
                 return "标签2" ;
             case 2 :
                 return "标签3" ;
             }
             return null ;
         }
     }

可以看到在getItem中返回了一个Fragment,这个就是当滑动到不同标签时显示在ViewPager中的内容,Fragment相当于一个Activity,在可以其中的onCreateView函数中构造需要显示的内容并返回

比如,以下代码将显示一个文本信息

?
1
2
3
4
5
6
7
8
9
10
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     TextView textView =  new TextView(getActivity());
     textView.setGravity(Gravity.CENTER);
     textView.setText( "你选择了标签:" +Integer.toString(getArguments().getInt(
             "no" )));
     return textView;
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值