我开始了解ViewPager2并尝试实现它,但没有找到任何合适的示例。

谁能告诉我如何使用它。

我正在寻找正确的用法,而不是示例。

最佳答案

更新7

检查:Migrate from ViewPager to ViewPager2

检查:Create swipe views with tabs using ViewPager2

更新6

查看my answer if you want to implement Carousel using View Pager2

更新5



样本代码

在下面使用 dependencies

implementation 'com.google.android.material:material:1.1.0-alpha08'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'

样本代码


<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        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">

    <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager"
            app:layout_anchor="@id/tabs"
            app:layout_anchorGravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    />


</androidx.coordinatorlayout.widget.CoordinatorLayout>


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import com.google.android.material.tabs.TabLayoutMediator

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


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        setSupportActionBar(toolbar)
        viewpager.adapter = AppViewPagerAdapter(supportFragmentManager, lifecycle)

        TabLayoutMediator(tabs, viewpager, object : TabLayoutMediator.OnConfigureTabCallback {
            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                // Styling each tab here
                tab.text = "Tab $position"
            }
        }).attach()


    }
}



TabLayout with ViewPager2

从文档

ViewPager2


  • 从右到左(RTL)布局支持
  • 垂直方向支持
  • notifyDataSetChanged全功能


  • FragmentStateAdapter替换FragmentStatePagerAdapter
  • RecyclerView.Adapter替换PagerAdapter
  • registerOnPageChangeCallback替换addPageChangeListener

  • 样本代码


    implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'
    


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    


    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager2.widget.ViewPager2;
    
    import java.util.ArrayList;
    
    public class MyActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        MyAdapter MyAdapter;
        private ArrayList<String> arrayList = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
            myViewPager2 = findViewById(R.id.view_pager);
    
            arrayList.add("Item 1");
            arrayList.add("Item 2");
            arrayList.add("Item 3");
            arrayList.add("Item 4");
            arrayList.add("Item 5");
    
            MyAdapter = new MyAdapter(this, arrayList);
    
    
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
    
            myViewPager2.setAdapter(MyAdapter);
        }
    }
    


    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.ArrayList;
    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
        private Context context;
        private ArrayList<String> arrayList = new ArrayList<>();
    
        public MyAdapter(Context context, ArrayList<String> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            return new MyViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.tvName.setText(arrayList.get(position));
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvName;
    
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tvName);
            }
        }
    }
    

    新的功能



    示例代码
        myViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
    
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
    
                Log.e("Selected_Page", String.valueOf(position));
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    



    样本代码

    对于HORIZONTAL Orientation使用
    myViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
    

    对于VERTICAL Orientation使用
    myViewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
    



    样本代码以添加新项目
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                arrayList.add("New ITEM ADDED");
                MyAdapter.notifyDataSetChanged();
            }
        });
    

    样本代码以删除新项目
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                arrayList.remove(3);
                MyAdapter.notifyItemRemoved(3);
            }
        });
    

    更新

    如果您想将FragmentViewPager2一起使用,请尝试此操作


    import java.util.ArrayList;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.viewpager2.adapter.FragmentStateAdapter;
    
    public class ViewPagerFragmentAdapter extends FragmentStateAdapter {
    
        private ArrayList<Fragment> arrayList = new ArrayList<>();
    
        public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager) {
            super(fragmentManager);
        }
    
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return arrayList.get(position);
        }
    
        public void addFragment(Fragment fragment) {
            arrayList.add(fragment);
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    }
    


    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager2.widget.ViewPager2;
    import neel.com.bottomappbar.R;
    
    public class MainActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        ViewPagerFragmentAdapter myAdapter;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myViewPager2 = findViewById(R.id.view_pager);
    
            myAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager());
    
            // add Fragments in your ViewPagerFragmentAdapter class
            myAdapter.addFragment(new FragmentOne());
            myAdapter.addFragment(new Fragmenttwo());
            myAdapter.addFragment(new FragmentThree());
    
            // set Orientation in your ViewPager2
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
    
            myViewPager2.setAdapter(myAdapter);
    
        }
    
    }
    

    有关更多信息,请检查此
  • ViewPager2
  • ViewPager2 under the Hood
  • Hands on With ViewPager2

  • 更新2



    新功能
  • 禁用用户输入(setUserInputEnabledisUserInputEnabled)的能力

  • API更改
  • ViewPager2类最终

  • 错误修复了
  • FragmentStateAdapter稳定性修复

  • 样本代码以禁止在viewpager2中滑动
    myViewPager2.setUserInputEnabled(false);// SAMPLE CODE to disable swiping in viewpager2
    
    
    myViewPager2.setUserInputEnabled(true);//SAMPLE CODE to enable swiping in viewpager2
    

    更新3


  • 以编程方式滚动ViewPager2的能力: fakeDragBy(offsetPx)


  • FragmentStateAdapter现在需要一个Lifecycle对象。添加了两个实用程序构造函数以从主机FragmentActivity或主机片段
  • 中获取它

    样本代码


    import java.util.ArrayList;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.lifecycle.Lifecycle;
    import androidx.viewpager2.adapter.FragmentStateAdapter;
    
    public class ViewPagerFragmentAdapter extends FragmentStateAdapter {
    
        private ArrayList<Fragment> arrayList = new ArrayList<>();
    
    
        public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }
    
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return arrayList.get(position);
        }
    
        public void addFragment(Fragment fragment) {
            arrayList.add(fragment);
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    }
    


    import android.os.Bundle;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager2.widget.ViewPager2;
    import neel.com.bottomappbar.R;
    
    public class MainActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        ViewPagerFragmentAdapter myAdapter;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myViewPager2=findViewById(R.id.view_pager);
            myAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), getLifecycle());
    
            // add Fragments in your ViewPagerFragmentAdapter class
            myAdapter.addFragment(new FragmentOne());
            myAdapter.addFragment(new Fragmenttwo());
            myAdapter.addFragment(new FragmentThree());
    
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
    
            myViewPager2.setAdapter(myAdapter);
        }
    }
    

    更新4


  • ItemDecorator引入的行为与RecyclerView一致。
  • 引入
  • MarginPageTransformer可以在页面之间(页面插图之外)创建空间。
  • 引入
  • CompositePageTransformer来提供组合多个PageTransformers的能力


  • FragmentStateAdapter#getItem方法重命名为FragmentStateAdapter#createFragment-以前的方法名称在过去被证明是错误的来源。
  • OFFSCREEN_PAGE_LIMIT_DEFAULT值从0更改为-1。如果使用了OFFSCREEN_PAGE_LIMIT_DEFAULTconstant,则无需更改客户端代码。

  • 样本代码


    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.recyclerview.widget.RecyclerView;
    import androidx.viewpager2.widget.MarginPageTransformer;
    import androidx.viewpager2.widget.ViewPager2;
    import neel.com.bottomappbar.R;
    
    public class MainActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        ViewPagerFragmentAdapter myAdapter;
        private ArrayList<Fragment> arrayList = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myViewPager2 = findViewById(R.id.myViewPager2);
    
            // add Fragments in your ViewPagerFragmentAdapter class
            arrayList.add(new FragmentOne());
            arrayList.add(new Fragmenttwo());
            arrayList.add(new FragmentThree());
    
            myAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), getLifecycle());
            // set Orientation in your ViewPager2
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
    
            myViewPager2.setAdapter(myAdapter);
    
            myViewPager2.setPageTransformer(new MarginPageTransformer(1500));
    
    
        }
    }
    


    import java.util.ArrayList;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.lifecycle.Lifecycle;
    import androidx.viewpager2.adapter.FragmentStateAdapter;
    
    public class ViewPagerFragmentAdapter extends FragmentStateAdapter {
    
        private ArrayList<Fragment> arrayList = new ArrayList<>();
    
    
        public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }
    
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new FragmentOne();
                case 1:
                    return new Fragmenttwo();
                case 2:
                    return new FragmentThree();
    
            }
            return null;
        }
    
        @Override
        public int getItemCount() {
            return 3;
        }
    }
    

    10-08 16:21