相关文章推荐
健壮的充值卡  ·  java list ...·  4 天前    · 
可爱的可乐  ·  Java list remove ...·  4 天前    · 
逼格高的饭盒  ·  python 泛型约束 ...·  1 周前    · 
茫然的猴子  ·  ComVisibleAttribute 类 ...·  1 年前    · 
爱逃课的剪刀  ·  ThinkPHP6 ...·  1 年前    · 
考研的松树  ·  pycharm中出现module ...·  1 年前    · 
仗义的自行车  ·  Apache Camel ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
public class QRBluetoothSwipeActivity extends AppCompatActivity {
    private ViewPager2 myViewPager2;
    private ViewPagerFragmentAdapter myAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // Make sure this is before calling super.onCreate
        setTheme(R.style.AppTheme); // show splash screen
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qr_bluetooth_swipe_activity);
        init();
    private void init() {
        myAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager());
        myAdapter.addFragment(new BluetoothPageFragment());
        myAdapter.addFragment(new QrPageFragment());
        myViewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        myViewPager2.setAdapter(myAdapter);

here ViewPagerFragmentAdapter

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
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();

but I get compile error:

ViewPagerFragmentAdapter is not abstract and does not override abstract method createFragment(int) in FragmentStateAdapter

How I can fix this and use FRAGMENTS in ViewPager2 ?

Thanks

FragmentStateAdapter in ViewPager2 is a little bit different than in ViewPager as follows:

  • Instead of implementing getCount(), implement getItemCount()

  • Instead of implementing getItem(int position), implement createFragment(int position)

  • Constructor signature is different by adding Lifecycle argument which should be included in super as well

    So replace your ViewPagerFragmentAdapter with below

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

    Also make sure to import/extend the right adapter of ViewPager2

    import androidx.viewpager2.adapter.FragmentStateAdapter;
    

    UPDATE 2021

    This answered the original issue of the question; However creating the FragmentStateAdapter this way is not that right generally speaking; because of:

  • To have a high performant ViewPager, it should keep only a few instantiated off-screen page fragments.

  • Keeping all the page fragments into a list is not good in terms of wasting device resources.

  • createFragment(), as its name implies, is intended to create a new fragment for a particular page and return it; not to return a fragment from a list that has already instantiated fragments.

  • Above all as per documentation createFragment()::

    Provide a new Fragment associated with the specified position.

    The adapter will be responsible for the Fragment lifecycle:

  • The Fragment will be used to display an item.
  • The Fragment will be destroyed when it gets too far from the viewport, and its state will be saved. When the item is close to the viewport again, a new Fragment will be requested, and a previously saved state will be used to initialize it.
  • So, I'd suggest to change this design to include that into consideration:

    Assuming your page fragment is PageFragment; then you can have single instance pattern by having static PageFragment newInstance(int position) {} method in the PageFragment, and then the createFragment() should be then:

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // return a brand new fragment that corresponds to this 'position'
        return PageFragment.newInstance(position); // Create a new instance of the page fragment
                    Use this constructor:  public ViewPagerFragmentAdapter(FragmentActivity fa) {         super(fa);     }
    – Alexei
                    Oct 23, 2019 at 16:45
                    This is a VERY wrong implementation. As naming imply, FragmentStateAdapter's createFragment should be responsible for instantiate Fragment. Creation of Fragment shouldn't happen outside FragmentStateAdapter. FragmentStateAdapter will be responsible for handling memory usage efficiency, even you need to create a large number of Fragments. If you create Fragment outside FragmentStateAdapter, it is highly not memory efficiency.
    – Cheok Yan Cheng
                    May 5, 2020 at 6:23
    
  •