Search in sources :

Example 1 with MyPostsFragment

use of com.google.firebase.quickstart.database.java.listfragments.MyPostsFragment in project quickstart-android by firebase.

the class MainFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
    // Create the adapter that will return a fragment for each section
    FragmentStateAdapter mPagerAdapter = new FragmentStateAdapter(getParentFragmentManager(), getViewLifecycleOwner().getLifecycle()) {

        private final Fragment[] mFragments = new Fragment[] { new RecentPostsFragment(), new MyPostsFragment(), new MyTopPostsFragment() };

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mFragments[position];
        }

        @Override
        public int getItemCount() {
            return mFragments.length;
        }
    };
    // Set up the ViewPager with the sections adapter.
    binding.container.setAdapter(mPagerAdapter);
    String[] mFragmentNames = new String[] { getString(R.string.heading_recent), getString(R.string.heading_my_posts), getString(R.string.heading_my_top_posts) };
    new TabLayoutMediator(binding.tabs, binding.container, (tab, position) -> tab.setText(mFragmentNames[position])).attach();
}
Also used : MyTopPostsFragment(com.google.firebase.quickstart.database.java.listfragments.MyTopPostsFragment) Bundle(android.os.Bundle) MyPostsFragment(com.google.firebase.quickstart.database.java.listfragments.MyPostsFragment) LayoutInflater(android.view.LayoutInflater) NonNull(androidx.annotation.NonNull) ViewGroup(android.view.ViewGroup) MenuItem(android.view.MenuItem) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) Nullable(androidx.annotation.Nullable) MyTopPostsFragment(com.google.firebase.quickstart.database.java.listfragments.MyTopPostsFragment) R(com.google.firebase.quickstart.database.R) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) Fragment(androidx.fragment.app.Fragment) RecentPostsFragment(com.google.firebase.quickstart.database.java.listfragments.RecentPostsFragment) View(android.view.View) FragmentMainBinding(com.google.firebase.quickstart.database.databinding.FragmentMainBinding) NavHostFragment(androidx.navigation.fragment.NavHostFragment) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) RecentPostsFragment(com.google.firebase.quickstart.database.java.listfragments.RecentPostsFragment) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) MyPostsFragment(com.google.firebase.quickstart.database.java.listfragments.MyPostsFragment)

Aggregations

Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 Fragment (androidx.fragment.app.Fragment)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 FragmentStateAdapter (androidx.viewpager2.adapter.FragmentStateAdapter)1 TabLayoutMediator (com.google.android.material.tabs.TabLayoutMediator)1 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)1 R (com.google.firebase.quickstart.database.R)1 FragmentMainBinding (com.google.firebase.quickstart.database.databinding.FragmentMainBinding)1 MyPostsFragment (com.google.firebase.quickstart.database.java.listfragments.MyPostsFragment)1 MyTopPostsFragment (com.google.firebase.quickstart.database.java.listfragments.MyTopPostsFragment)1 RecentPostsFragment (com.google.firebase.quickstart.database.java.listfragments.RecentPostsFragment)1