使用ViewPager和Fragment实现滑动导航

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

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;
 
}
作者:Bonker
出处:http://www.cnblogs.com/Bonker
QQ:519841366
       
本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
目录
相关文章
|
Android开发
Android TabLayout 加 ViewPager实现选项卡切换功能
今天讲一个很简单的功能,就是可以切换的选项卡功能,很多app都有类似这种效果,实现的方法也有很多,这里我采用TabLayout加上ViewPager来实现,这里我做了一个封装,相当于一个工具类来着,哪个地方需要用到都可以使用,使用上我的那个封装类就可以了。
388 0
Android TabLayout 加 ViewPager实现选项卡切换功能
|
缓存 Android开发
android 实现 搜索保存历史记录功能
android 实现 搜索保存历史记录功能
android 实现 搜索保存历史记录功能
|
6天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
15天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
10天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
617 215
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
859 61
|
8天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1305 157