ViewPager结合Fragment的使用

一、Fragment简介

  Android在3.0版本引入了Fragment(碎片)功能,它类似于Activity,也可以 包含布局,而且Fragment通常是嵌套在Activity中使用的。

  场景:有两个Fragment,其中Fragment1包含了一个ListView,每行显示一本 书的标题,而Fragment2包含了TextView和ImageView,显示书的详细内容和图片。

 想想,如果一个很大的界面,就设计成一个布局,写起界面来是不是会有很多麻 烦?如果组件多的话,是不是管理起来也很麻烦!

而使用Fragment,可以把屏幕划分成几块,然后进行分组,进行一个模块化的 管理,从而可以更加方便的在运行过程中动态地更新Activity的用户界面。

  1、Fragment的优势

 注意!!!

        Fragment并不能单独使用,它需要嵌套在Activity中使用,同时当宿主Activity 被destory销毁了,它也会跟着被销毁!

  2、Fragment的基本用法

   步骤1:定制Fragment的XML布局文件

   步骤2:创建一个自定义的Fragment,继承Fragment类,和创建一个Activity很类似, 不同的是需要重写一个onCreateView()方法来返回这个Fragment的布局。

 步骤3:定义Fragment适配器,管理所有的Fragment。

 步骤4:将Fragment添加到activity页面。

二、FragmentPagerAdapter适配器

1、FragmentPagerAdapter派生自PagerAdapter,它是专门用来呈现 Fragment页面的。

2、该类中每一个生成的Fragment都将保存在内存中,所以这个适配器更适合那些数量相对较少,静态的页面。对于存在多个fragment的情况,一般推荐使用FragmentStatePagerAdapter。

3、FragmentPagerAdapter重载了几个必须实现的函数:getItem()、 getCount()。

  1、Activity、FragmentActivity和AppCompatActivity的区别

1、Activity是最基础的页面类,对应getFragmentManager方法来控制Activity和 Fragment之间的交互。

2、FragmentActivity间接继承自Activity,并提供了对v4包中support Fragment的支 持。在FragmentActivity中必须使用getSupportFragmentManager方法来处理 support Fragment的交互。

3、AppCompatActivity继承自FragmentActivity,为Material Design风格控件提供 了便利。

三、程序代码

  1、布局文件代码

    activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v4.view.ViewPager>
</LinearLayout>

    layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view1"/>
</RelativeLayout>

    layout2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view2"/>
</RelativeLayout>

    layout3.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/view3"/>
</RelativeLayout>

  2、功能代码

    FragAdapter.java

package com.example.fragmenttest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class FragAdapter extends FragmentPagerAdapter {
    //1.创建Fragment数组
    private List<Fragment> mFragments;
    //2.接收从Activity页面传递过来的Fragment数组
    public FragAdapter(FragmentManager fm,List<Fragment> fragments){
        super(fm);
        mFragments = fragments;
    }
    @Override
    public Fragment getItem(int i) {
        return mFragments.get(i);
    }
    @Override
    public int getCount() {
        return mFragments.size();
    }
}

    FragMent1.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       //1.创建View视图
        View view = inflater.inflate(R.layout.layout1,container,false);
        //2.返回view视图
        return  view ;
    }
}

    FragMent2.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout2,container,false);
        return  view;
    }
}

    FragMent3.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout3,container,false);
        return  view;
    }
}

    MainActivity.java

package com.example.fragmenttest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.构造Fragment列表
        List<Fragment> fragments = new ArrayList<Fragment>();
        //2.将前面定义的三个Fragment类对应的实例放入Fragment列表
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        //3.构造Fragment适配器
        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(),fragments);
        //4.创建ViewPager实例,并绑定适配器
        viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

    }
}

  • 17
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
好的,我会为您讲解关于AndroidViewPager和Fragment使用ViewPager和FragmentAndroid中非常常用的组件,他们可以一起使用来实现滑动页面效果。ViewPager是一个可以左右滑动切换页面的布局容器,而Fragment作为ViewPager的子页面,可以在ViewPager中进行动态添加和移除。 下面我们将分别介绍ViewPager和Fragment使用。 ## ViewPager的使用 ### 1.布局文件 在布局文件中,我们需要使用ViewPager作为容器,将需要滑动切换的页面放入其中。如下所示: ``` <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` ### 2.创建Adapter 我们需要创建一个Adapter继承自PagerAdapter,并重写以下方法: ``` public class MyPagerAdapter extends PagerAdapter { private List<Fragment> mFragments; public MyPagerAdapter(List<Fragment> fragments) { mFragments = fragments; } @Override public int getCount() { return mFragments.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { Fragment fragment = mFragments.get(position); container.addView(fragment.getView()); return fragment.getView(); } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } } ``` ### 3.设置Adapter 在Activity或Fragment中,我们需要创建ViewPager的实例,并设置Adapter。如下所示: ``` ViewPager viewPager = findViewById(R.id.viewPager); List<Fragment> fragments = new ArrayList<>(); fragments.add(new Fragment1()); fragments.add(new Fragment2()); fragments.add(new Fragment3()); MyPagerAdapter adapter = new MyPagerAdapter(fragments); viewPager.setAdapter(adapter); ``` 这样,我们就完成了ViewPager的使用。 ## Fragment使用 ### 1.创建Fragment 我们需要创建一个继承自Fragment的类,并重写以下方法: ``` public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); return view; } } ``` ### 2.布局文件 我们需要在Fragment中添加布局文件,如下所示: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 这样,我们就完成了Fragment使用。 ## ViewPager和Fragment结合使用 通过以上介绍,我们已经知道了如何使用ViewPager和Fragment了。现在我们需要将它们结合起来使用。 ### 1.创建Fragment 我们需要创建多个Fragment作为ViewPager的子页面。 ### 2.创建Adapter 我们需要创建一个PagerAdapter,将Fragment添加到ViewPager中。如上所示,我们已经创建了一个MyPagerAdapter。 ### 3.设置Adapter 在Activity或Fragment中,我们需要创建ViewPager的实例,并设置Adapter。如上所示,我们已经使用ViewPager的setAdapter方法设置了MyPagerAdapter。 这样,我们就完成了ViewPager和Fragment结合使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撩得Android一次心动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值