4.14

所花时间(包括上课):2

打码量(行):300

博客量(篇):1

了解到知识点:学习Fragment静态注册

 

 <!-- activity_guide.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">

 

    <androidx.viewpager.widget.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

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;

 

public class GuideFragment1 extends Fragment {

 

    public GuideFragment1() {

        // Required empty public constructor

    }

 

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_guide_1, container, false);

    }

}

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;

 

public class GuideFragment2 extends Fragment {

 

    public GuideFragment2() {

        // Required empty public constructor

    }

 

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_guide_2, container, false);

    }

}

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;

 

public class GuidePagerAdapter extends FragmentPagerAdapter {

 

    private static final int NUM_PAGES = 2;

 

    public GuidePagerAdapter(FragmentManager fm) {

        super(fm);

    }

 

    @Override

    public Fragment getItem(int position) {

        switch (position) {

            case 0:

                return new GuideFragment1();

            case 1:

                return new GuideFragment2();

            default:

                return null;

        }

    }

 

    @Override

    public int getCount() {

        return NUM_PAGES;

    }

}

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.viewpager.widget.ViewPager;

 

public class GuideActivity extends AppCompatActivity {

 

    private ViewPager mViewPager;

    private GuidePagerAdapter mAdapter;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide);

 

        mViewPager = findViewById(R.id.viewPager);

        mAdapter = new GuidePagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mAdapter);

    }

}

posted @ 2024-04-14 19:55  赵千万  阅读(3)  评论(0编辑  收藏  举报