简体   繁体   中英

Very simple viewPager2

I am new to app development and I want to create fluid tabbed transitions between pages in my app. Problem is that this requires fragments and viewpager2 which I am unfamiliar with (I've been working with activities for the last few months). In order to learn how to do this, I am trying to create a very simple use-case of viewPager2 (just two simple fragments with textViews) and ran into my first problem: In the code below, the "getSupportFragmentManager is underlined red. It gives me the suggestion to change the parameter in the adapter constructor from FragmentActivity to FragmentManager. However, when I do this, the "fa" in "super(fa)" is then underlined red and I don't know how to fix this plus most of the tutorials online are for the previous version of viewPager and not for ViewPager2. The ones that are about viewPager2 are for the most part written in Kotlin, which I don't care to learn, partly because of the name and partly because I plan to learn Dart next since it allows for the same code to be applied to both android and apple devices. Does anyone know how to fix that error or can point me to a very simple Z93F725A07423FE1C889F4 48B33D21F46Z implementation of viewPager2 that I can use as a template? I'm sure that could even be written up here! Thank you so much!

MainActivity including Adapter:

    public class MainActivity extends AppCompatActivity {

    FragmentStateAdapter fragmentStateAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 vp2 = findViewById(R.id.viewPager);
        fragmentStateAdapter = new ScreenSlidePagerAdapter(this);
    }

    private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
        private static final int NUM_PAGES = 2;

        public ScreenSlidePagerAdapter(FragmentActivity fa) {
            super(fa);
        }

        @Override
        public Fragment createFragment(int position) {

            switch (position) {
                case 0:
                    return FragmentOne.newInstance(0,"I'm Fragment 1");
                case 1 :
                    return  FragmentTwo.newInstance(1, "I'm fragment 2");
                default: return null;

            }
        }

        @Override
        public int getItemCount() {
            return NUM_PAGES;
        }
    }

}

Fragment Example:

    public class FragmentOne extends Fragment {

    private String mParam1;
    private int page;


    public FragmentOne() {

    }

    public static FragmentOne newInstance(int page, String param1) {
        FragmentOne fragment = new FragmentOne();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(mParam1);
            page = getArguments().getInt(mParam1, 0);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_one, container, false);
        TextView textView = view.findViewById(R.id.fragmentOne);
        textView.setText(mParam1);
        return view;
    }
}

Fragment XML:

<FrameLayout 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"
    tools:context=".FragmentOne">


    <TextView
        android:id="@+id/fragmentOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragmentOne"
        android:textColor="@android:color/black"/>

</FrameLayout>

Change your code to this, your adapter take FragmentActivity to parameter

fragmentStateAdapter = new ScreenSlidePagerAdapter(this);
mParam1 = getArguments().getString("someTitle");
page = getArguments().getInt("someInt", 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM