5.5

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

打码量(行):150

博客量(篇):1

了解到知识点:学习Action Tab

 

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

<androidx.constraintlayout.widget.ConstraintLayout 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">

 

    <com.google.android.material.tabs.TabLayout

        android:id="@+id/tabLayout"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"/>

 

    <androidx.viewpager.widget.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="0dp"

        android:layout_height="0dp"

        app:layout_constraintTop_toBottomOf="@id/tabLayout"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

package com.example.myapp;

 

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentPagerAdapter;

import androidx.viewpager.widget.ViewPager;

 

import android.os.Bundle;

 

import com.google.android.material.tabs.TabLayout;

 

public class MainActivity extends AppCompatActivity {

 

    private TabLayout tabLayout;

    private ViewPager viewPager;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        tabLayout = findViewById(R.id.tabLayout);

        viewPager = findViewById(R.id.viewPager);

 

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override

            public Fragment getItem(int position) {

                switch (position) {

                    case 0:

                        return new FirstFragment();

                    case 1:

                        return new SecondFragment();

                    case 2:

                        return new ThirdFragment();

                    default:

                        return null;

                }

            }

 

            @Override

            public int getCount() {

                return 3;

            }

 

            @Override

            public CharSequence getPageTitle(int position) {

                switch (position) {

                    case 0:

                        return "Tab 1";

                    case 1:

                        return "Tab 2";

                    case 2:

                        return "Tab 3";

                    default:

                        return null;

                }

            }

        });

 

        tabLayout.setupWithViewPager(viewPager);

    }

}

package com.example.myapp;

 

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

 

public class FirstFragment extends Fragment {

 

    public FirstFragment() {

        // 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_first, container, false);

    }

}

posted @ 2024-05-05 21:26  赵千万  阅读(1)  评论(0编辑  收藏  举报