4.12

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

打码量(行):600

博客量(篇):1

了解到知识点:学习翻页标签栏PagerTabStrip

 

 

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

 

        <androidx.viewpager.widget.PagerTabStrip

            android:id="@+id/pagerTabStrip"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_gravity="top"

            android:paddingTop="16dp"

            android:paddingBottom="16dp"

            android:textColor="#000000"

            android:textSize="16sp"

            android:background="#FFFFFF"/>

 

    </androidx.viewpager.widget.ViewPager>

 

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

    }

}

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 SecondFragment extends Fragment {

 

    public SecondFragment() {

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

    }

}

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;

 

public class PagerAdapter extends FragmentPagerAdapter {

 

    private static final int NUM_PAGES = 2;

    private static final String[] TITLES = {"Page 1", "Page 2"};

 

    public PagerAdapter(FragmentManager fm) {

        super(fm);

    }

 

    @Override

    public Fragment getItem(int position) {

        switch (position) {

            case 0:

                return new FirstFragment();

            case 1:

                return new SecondFragment();

            default:

                return null;

        }

    }

 

    @Override

    public CharSequence getPageTitle(int position) {

        return TITLES[position];

    }

 

    @Override

    public int getCount() {

        return NUM_PAGES;

    }

}

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.viewpager.widget.ViewPager;

import androidx.viewpager.widget.PagerTabStrip;

 

public class MainActivity extends AppCompatActivity {

 

    private ViewPager mViewPager;

    private PagerTabStrip mPagerTabStrip;

    private PagerAdapter mAdapter;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mViewPager = findViewById(R.id.viewPager);

        mPagerTabStrip = findViewById(R.id.pagerTabStrip);

        mAdapter = new PagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mAdapter);

    }

}

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.viewpager.widget.ViewPager;

import androidx.viewpager.widget.PagerTabStrip;

 

public class MainActivity extends AppCompatActivity {

 

    private ViewPager mViewPager;

    private PagerTabStrip mPagerTabStrip;

    private PagerAdapter mAdapter;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mViewPager = findViewById(R.id.viewPager);

        mPagerTabStrip = findViewById(R.id.pagerTabStrip);

        mAdapter = new PagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mAdapter);

    }

}

posted @ 2024-04-12 21:00  赵千万  阅读(4)  评论(0编辑  收藏  举报