Android ViewPager+Fragment组合使用

废话就不多说了,直接开始。
1、准备好两个布局为Fragment使用
布局一:

layout_model1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:background="#f00"
            android:layout_height="0dp"
            android:layout_weight="6"
            android:gravity="center">

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:background="#0f0"
            android:layout_height="0dp"
            android:layout_weight="10.42"
            android:gravity="center">

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

布局二:

layout_model2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#675"
    android:orientation="vertical">


</LinearLayout>

这布局我什么都没加

2、自定义两个Fragment,当然自定义多个也是可以的,不自定义也行,代码如下:
第一个Fragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import megvii.testfacepass.R;

public class Model1 extends Fragment {

    private View view;

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

第二个Fragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import megvii.testfacepass.R;

public class Model2 extends Fragment {

    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.layout_model2,container,false);
        return view;
    }
}

3、把Fragment放入到一个集合中

private List<Fragment> fragments = new ArrayList<>();
fragments.add(model1);
fragments.add(model2);

4、弄一个FragmentPagerAdapter来管理Fragment,代码如下:

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> mfragmentList;

    public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.mfragmentList = fragmentList;
    }

    //获取集合中的某个项
    @Override
    public Fragment getItem(int position) {
        return mfragmentList.get(position);
    }

    //返回绘制项的数目
    @Override
    public int getCount() {
        return mfragmentList.size();
    }
}

5、把Fragment和ViewPager关联起来

MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);
ViewPager viewPager = findViewById(R.id.noScrollViewPager);
viewPager .setAdapter(myFragmentPagerAdapter);

效果如下:


gif_20190916_185656.gif
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容