ViewPager2用法上下滑动

ViewPager2用法上下滑动

界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </com.google.android.material.tabs.TabLayout>
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.viewpager2.widget.ViewPager2>

</LinearLayout>

主界面

package com.bw.a620lx;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private TabLayout tab;
    private ViewPager2 vp2;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> titls=new ArrayList<>();

   private Fragment1 fragment1;
   private Fragment2 fragment2;
    //设置自定义的适配器必需继承FragmentStateAdapter
   private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tab = (TabLayout) findViewById(R.id.tab);
        vp2 = (ViewPager2) findViewById(R.id.vp2);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragments=new ArrayList<>();
        fragments.add(fragment1);
        fragments.add(fragment2);
//        vp2.setRotation(-90);
        //设置上下滑动
        vp2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        //设置标题
        titls.add("首页");
        titls.add("第二页");
        //定义适配器
        myAdapter=new MyAdapter(this,fragments,titls);
        vp2.setAdapter(myAdapter);
        //通过TabLayoutMediator 方法参数类型 tablayout   viewpager  new new TabLayoutMediator.TabConfigurationStrategy()
        //tab赋值.settext(集合.get(position))
         new TabLayoutMediator(tab,vp2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull Tab tab, int position) {
                tab.setText(titls.get(position));
            }
        }).attach();



    }
}

继承FragmentStateAdapter

package com.bw.a620lx;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;

public class MyAdapter extends FragmentStateAdapter {
    private ArrayList<Fragment> fragments;
    private ArrayList<String> titls;


    public MyAdapter(@NonNull FragmentActivity fragmentActivity, ArrayList<Fragment> fragments, ArrayList<String> titls) {
        super(fragmentActivity);
        this.fragments = fragments;
        this.titls = titls;
    }

    public MyAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }


}

你可能感兴趣的:(ViewPager2运用)