• 使用Fragment填充ViewPager


          在上一篇文章中,讲解了使用PagerAdapter作为适配器时的ViewPager的使用方法。然后在实际项目中更多的使用Fragment作为页卡,因为实际开发中每一个页卡要复杂的多。而使用Fragment有利于切断每一个页卡与MainActivity的联系,在各自的Fragment上实现自己更复杂的功能。其实使用Fragment作为填充,大部分的代码都与使用View作为填充类似,如果你还不知道怎么使用View来作为页卡,可以点击下面的链接,进行阅读学习:

    http://www.cnblogs.com/fuly550871915/p/4922953.html

           下面我们以一个完整的例子,来讲一下使用FragmenStatePagerAdapter作为适配器,ViewPager使用方法。由于大部分的知识都在上一篇文章(使用View作为填充ViewPager)中讲过,因此具体的代码解释就不再进行了。

            新建项目,然后新建布局,作为Fragment的填充物。首先是layout1.xml。如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     android:gravity="center" >
     7     <TextView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:text="我是第一个界面"
    11         android:textSize="30sp"/>
    12 
    13 </LinearLayout>

           代码很简单,不解释。然后再建立同样的三个布局,分别命名为layout2.xml,layout3.xml,layout4.xml。唯一不同的就是第10行文本的显示不同而已。具体代码同上,就不贴出来了。然后就可以建立Fragment类了。

          新建类Fragment1,继承自Fragment。其中的代码如下:

     1 package com.example.viewpager;
     2 
     3 import android.os.Bundle;
     4 import android.support.v4.app.Fragment;
     5 import android.view.LayoutInflater;
     6 import android.view.View;
     7 import android.view.ViewGroup;
     8 
     9 public class Fragment1 extends Fragment{
    10     
    11     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    12             Bundle savedInstanceState) {
    13 
    14         return inflater.inflate(R.layout.layout1, null);
    15     }
    16 }

             从代码中发现,将layout1这个布局加载给了Fragment1.同样的道理,新建Fragment2,Fragment3,Fragment4,分别用来加载layout2,layout3,layout4.

            就这样子,所有的Fragment都准备好了。下面开始写适配器,就采用FragmentStatePagerAdapter吧。代码如下:

     1 package com.example.viewpager;
     2 
     3 import java.util.List;
     4 
     5 import android.support.v4.app.Fragment;
     6 import android.support.v4.app.FragmentManager;
     7 import android.support.v4.app.FragmentStatePagerAdapter;
     8 import android.view.ViewGroup;
     9 
    10 /**
    11  * ViewPager的适配器
    12  * @author fuly1314
    13  *
    14  */
    15 public class FragmentAdapter extends FragmentStatePagerAdapter{
    16 
    17     private List<Fragment> fragmentList;//数据源
    18     private List<String> titles;//标题
    19     
    20     public FragmentAdapter(FragmentManager fm,List<Fragment> fragmentList,List<String> titles) {
    21         super(fm);
    22         this.fragmentList = fragmentList;
    23         this.titles = titles;
    24     }
    25 
    26     //相应页卡设定相应的Fragment
    27     public Fragment getItem(int arg0) {
    28     
    29         return fragmentList.get(arg0);
    30     }
    31 
    32     //数据源的数目
    33     public int getCount() {
    34     
    35         return fragmentList.size();
    36     }
    37 
    38     /*
    39      * 对于FragmentStatePagerAdapter,它的销毁和实例方法,即
    40      * destroyItem和instantiateItem方法保持默认即可
    41      */
    42     public void destroyItem(ViewGroup container, int position, Object object) {
    43 
    44         super.destroyItem(container, position, object);
    45     }
    46 
    47     public Object instantiateItem(ViewGroup arg0, int arg1) {
    48 
    49         return super.instantiateItem(arg0, arg1);
    50     }
    51 
    52     //设定标题
    53     public CharSequence getPageTitle(int position) {
    54         
    55         return titles.get(position);
    56     }
    57     
    58 }

          注意的红色的代码。应该注意的是,FragmentStatePagerAdapter的销毁item和显示item的方法都保持默认的写法即可。

          然后就是MainActivity中的代码了,如下:

     1 package com.example.viewpager;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import android.os.Bundle;
     7 import android.support.v4.app.Fragment;
     8 import android.support.v4.app.FragmentActivity;
     9 import android.support.v4.view.PagerTabStrip;
    10 import android.support.v4.view.ViewPager;
    11 import android.support.v4.view.ViewPager.OnPageChangeListener;
    12 import android.view.LayoutInflater;
    13 import android.widget.Toast;
    14 import android.graphics.Color;
    15 
    16 public class MainActivity extends FragmentActivity implements OnPageChangeListener{
    17     
    18     private ViewPager pager;
    19     private List<Fragment> fragmentList = new ArrayList<Fragment>();//数据源
    20     private FragmentAdapter fragmentAdapter;
    21     
    22     private List<String> titles = new ArrayList<String>();//标题
    23     
    24     private PagerTabStrip pagerTitle;//ViewPager的标题
    25     
    26 
    27     protected void onCreate(Bundle savedInstanceState) {
    28         super.onCreate(savedInstanceState);
    29         setContentView(R.layout.activity_main);
    30         
    31         //获取ViewPager
    32         pager = (ViewPager) findViewById(R.id.view_pager);
    33         
    34         pager.setOnPageChangeListener(this);//设置监听器
    35         
    36         //获取pagerTitle
    37         pagerTitle = (PagerTabStrip) findViewById(R.id.pager_title);
    38         
    39         //为标题设置属性,比如背景,颜色线等
    40         pagerTitle.setBackgroundColor(Color.RED);//设置背景颜色
    41         pagerTitle.setTextColor(Color.YELLOW);//设置标题文字的颜色
    42         pagerTitle.setDrawFullUnderline(false);//将标题下的长分割线去掉
    43         pagerTitle.setTabIndicatorColor(Color.BLUE);//设置标题下粗一点的短分割线的颜色
    44         
    45         //添加标题
    46         titles.add("第一页");
    47         titles.add("第二页");
    48         titles.add("第三页");
    49         titles.add("第四页");
    50         
    51         
    52         
    53         
    54    55         
    56         //获取四个Fragment
    57         Fragment1 fragment1 = new Fragment1();
    58         Fragment2 fragment2 = new Fragment2();
    59         Fragment3 fragment3 = new Fragment3();
    60         Fragment4 fragment4 = new Fragment4();
    61         
    62         //将四个Fragment加入到集合
    63         fragmentList.add(fragment1);
    64         fragmentList.add(fragment2);
    65         fragmentList.add(fragment3);
    66         fragmentList.add(fragment4);
    67         
    68         //实例化适配器
    69         fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),fragmentList,titles);
    70         
    71         //设置适配器
    72         pager.setAdapter(fragmentAdapter);
    73     }
    74 
    75 
    76 
    77     //当滚动状态改变时被调用
    78     public void onPageScrollStateChanged(int arg0) {
    79     
    80         
    81     }
    82 
    83 
    84     //滚动时调用
    85     public void onPageScrolled(int arg0, float arg1, int arg2) {
    86     
    87         
    88     }
    89 
    90 
    91     //当页卡被选中时调用
    92     public void onPageSelected(int arg0) {
    93         
    94         Toast.makeText(this, "这是第"+(arg0+1)+"个界面", Toast.LENGTH_LONG).show();
    95         
    96     }
    97 
    98     
    99 }

          注意红色的代码,此时继承的是FragmentActivity。而且所有导入的包,都应该是android.support.v4下的。

            好了,现在都写好了,运行程序即可,效果图如下:

    总结:
    如果用Fragment作为页卡,需要注意:
    (1)活动继承的是FragmentActivity,而不是Activity。
    (2)相关的导包都应该是android.support.v4下的包。
    (3)对于FragmentStatePagerAdapter,它的destroyItem和instantiateItem方法保持默认即可,不用编写。

  • 相关阅读:
    Matplotlib Date Index Formatter 日期索引格式化学习
    Matplotlib 日期格式转换
    Matplotlib基础 可视化绘图 学习笔记
    Python 修饰符@用法
    Linux下基于shell脚本实现学生信息管理系统
    JavaScript的popup框
    HTML语言发展史
    CSS grid 模板
    JavaScript中的正则表达式
    position的四个属性值
  • 原文地址:https://www.cnblogs.com/fuly550871915/p/4923701.html
Copyright © 2020-2023  润新知