4.17

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

打码量(行):230

博客量(篇):1

了解到知识点:学习使用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;

 

public class GuideFragment1 extends Fragment {

 

    @Nullable

    @Override

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

        // 加载并返回 fragment_guide_1 布局

        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 {

 

    @Nullable

    @Override

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

        // 加载并返回 fragment_guide_2 布局

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

    }

}

<!-- fragment_guide_1.xml -->

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 页面1的内容 -->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="引导页1"

        android:layout_gravity="center" />

</FrameLayout>

<!-- fragment_guide_2.xml -->

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 页面2的内容 -->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="引导页2"

        android:layout_gravity="center" />

</FrameLayout>

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;

 

public class GuidePagerAdapter extends FragmentPagerAdapter {

 

    public GuidePagerAdapter(@NonNull FragmentManager fm) {

        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

    }

 

    @NonNull

    @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 2;

    }

}

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);

 

        // 初始化 ViewPager 和适配器

        mViewPager = findViewById(R.id.viewPager);

        mAdapter = new GuidePagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mAdapter);

    }

}

<!-- 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>

posted @ 2024-04-17 15:34  赵千万  阅读(3)  评论(0编辑  收藏  举报