Tabbed Activity的使用(Fragment)

1,首先file -> new -> Activity -> Tabbed Activity

 

2,创建完成后,发现会自动的创建一大堆代码,大部分我们是不需要关心的,关于页面切换的代码位置:

  在中间偏下的位置有这样一个类:

    这个getItem就是我们进行页面切换操作的位置:

1  public class SectionsPagerAdapter extends FragmentPagerAdapter {
2 
3         public SectionsPagerAdapter(FragmentManager fm) {
4             super(fm);
5         }
6 
7         @Override
8         //根据tab不同位置呈现不同的内容
9         public Fragment getItem(int position)

3,我们首先创建三个ImgFm,组成三个Fragment布局,下面我已其中一个为例:

很简单,我们需要在res —> drawable中添加几张图片,例如pic1

 1 public class Image1Fm extends Fragment{
 2 
 3     @Nullable
 4     @Override
 5     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 6         ImageView iv = new ImageView(getActivity());
 7         iv.setImageResource(R.drawable.pic1);
 8 
 9         return iv;
10     }
11 }

4,然后我们在第二步中的位置可以进行操作:

 1  public class SectionsPagerAdapter extends FragmentPagerAdapter {
 2 
 3         public SectionsPagerAdapter(FragmentManager fm) {
 4             super(fm);
 5         }
 6 
 7         @Override
 8         //根据tab不同位置呈现不同的内容
 9         public Fragment getItem(int position) {
10             switch (position){
11                 case 0:
12                     return new Image1Fm();
13                 case 1:
14                     return new Image2Fm();
15                 case 2:
16                     return new Image3Fm();
17             }
18             // getItem is called to instantiate the fragment for the given page.
19             // Return a PlaceholderFragment (defined as a static inner class below).
20             return null;
21         }

5,发布,运行(哈哈哈,黑一波我的姐)

 

 

最后,需要注意的一点是:我们添加的图片不可以过大,否则会提示下面的错误:

Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

转载于:https://www.cnblogs.com/icyhusky/p/6048228.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值