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;
 
}

使用ViewPager和Fragment实现滑动导航的更多相关文章

  1. ViewPager+tab+Fragment的滑动

    package teamhgl.xinwensudu; import android.os.Bundle;import android.support.v4.app.Fragment;import a ...

  2. Viewpager结合fragment实现底部导航

    具体实现如下: FindFragment.java package fbtt.com.fbtt.fragment; import android.os.Bundle; import android.s ...

  3. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  4. ViewPager+Fragment实现滑动显示,且Fragment里面又放Fragment+viewPager

    思路:新建一个Activity,且这个Activity要继承FragementActivity,在Activity的布局文件中放入了一个viewPager,为了效果好看,还做了个导航,使得ViewPa ...

  5. Android典型界面设计(6)——ActionBar Tab+ViewPager+Fagment实现滑动导航

    一.问题描述 在Android典型界面设计一文中,实现典型滑动导航界面,其实使用ActionBar 也可以轻松实现这一效果,甚至也可实现类似Android典型界面设计(3)的双导航效果.可见Actio ...

  6. 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  7. ViewPager结合Fragment进行无限滑动

    实现ViewPager结合Fragment实现无限循环切换,这里也是在适配器里面进行的,当然使用滑动监听也能够实现 import android.support.v4.app.Fragment; im ...

  8. Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  9. Android Viewpager+Fragment实现滑动标签页

    ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应. 主页布局 <?xml version="1.0" ...

随机推荐

  1. 【DeepLearning】Exercise:Convolution and Pooling

    Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...

  2. LNMP分离式部署实例[转]

    很多人在练习部署LNMP环境的时候,大都数是部署在同一个虚拟机上面的.但是实际工作中,我们一般都是分离部署的. 今天我就用3台虚拟机,部署下LNMP环境.以供参考! 网络拓扑图: 首先准备3台虚拟机: ...

  3. 解决编译Apache出现的问题:configure: error: APR not found

    今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...

  4. mysqlsla快速入门

    小强软件测试,因为不是天生丽质,所以必须天生励志. 性能.python自动化班长期招生,咨询QQ:2083503238 官网:http://xqtesting.sxl.cn QQ群:229390571 ...

  5. Redis学习之路(004)- 报错及问题

    在i配置编译的过程中,遇到一下问题: 1. /redis_test: error while loading shared libraries: libhiredis.so.0.13: cannot ...

  6. 【Android】Android如何实现手机震动

    实现手机震动其实很简单,手机震动使用是Vibrator类,然后震动也是需要权限的,在使用之前在AndroidManifest.xml文件中添加 <uses-permission android: ...

  7. Git 分支(分布式版本控制系统)

    前言 几乎所有的版本控制系统都以某种形式支持分支.使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线.在很多版本控制系统中,这是一个略微低效的过程--常常需要完全创建一个源代码目录的 ...

  8. Eclipse插件的安装与配置

    1.下载插件时注意要和Eclipse版本兼容. 2.安装Eclipse插件时注意是否要安装其他的插件,这一点很容易被忽视. 3.有时启动Eclipse未加载插件,解决方法很多,总结一下:     a ...

  9. tableView的用法具体解释

    1 tableView的类型   1.1 UITableViewStylePlain  没有区头 不显区头     向上滑动区头不会移动到屏幕外面 ' 1.2 UITableViewStyleGrou ...

  10. cucumber java从入门到精通(2)用代码定义步骤

    cucumber java从入门到精通(2)用代码定义步骤 上一节里我们定义了feature文件,feature文件就是自然语言描述的用例文件,它有一定的章法,具体的潜规则是: 使用Feature关键 ...