Search in sources :

Example 1 with FragmentStateAdapter

use of androidx.viewpager2.adapter.FragmentStateAdapter in project banner by youth5201314.

the class Vp2FragmentRecyclerviewActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vp2_fragment_recyclerview);
    ButterKnife.bind(this);
    viewPager2.setAdapter(new FragmentStateAdapter(this) {

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            if (position == 0) {
                return BannerListFragment.newInstance(position);
            } else if (position == 1) {
                return BlankFragment.newInstance();
            } else {
                return BannerFragment.newInstance();
            }
        }

        @Override
        public int getItemCount() {
            return 3;
        }
    });
    new TabLayoutMediator(mTabLayout, viewPager2, (tab, position) -> {
        tab.setText("页面" + position);
    }).attach();
    mBanner.addBannerLifecycleObserver(this).setAdapter(new ImageAdapter(DataBean.getTestData())).setIntercept(false).setIndicator(new CircleIndicator(this));
}
Also used : R(com.test.banner.R) Bundle(android.os.Bundle) TabLayout(com.google.android.material.tabs.TabLayout) ButterKnife(butterknife.ButterKnife) NonNull(androidx.annotation.NonNull) TabLayoutMediator(com.test.banner.util.TabLayoutMediator) CircleIndicator(com.youth.banner.indicator.CircleIndicator) ViewPager2(androidx.viewpager2.widget.ViewPager2) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) BindView(butterknife.BindView) DataBean(com.test.banner.bean.DataBean) Fragment(androidx.fragment.app.Fragment) Banner(com.youth.banner.Banner) ImageAdapter(com.test.banner.adapter.ImageAdapter) ImageAdapter(com.test.banner.adapter.ImageAdapter) NonNull(androidx.annotation.NonNull) CircleIndicator(com.youth.banner.indicator.CircleIndicator) TabLayoutMediator(com.test.banner.util.TabLayoutMediator) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) Fragment(androidx.fragment.app.Fragment)

Example 2 with FragmentStateAdapter

use of androidx.viewpager2.adapter.FragmentStateAdapter in project Android-skin-support by ximsfei.

the class MainActivity method initViews.

private void initViews() {
    mFragments = new ArrayList<>();
    mFragments.add(new HomeFragment());
    mFragments.add(new DiscoveryFragment());
    mFragments.add(new MineFragment());
    mContainerVp.setAdapter(new FragmentStateAdapter(this) {

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

        @Override
        public int getItemCount() {
            return mFragments.size();
        }
    });
    mContainerVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

        @Override
        public void onPageSelected(int position) {
            mMenuBnv.getMenu().getItem(position).setChecked(true);
            setTitle(mMenuBnv.getMenu().getItem(position).getTitle());
        }
    });
    mMenuBnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()) {
                case R.id.bnv_home:
                    mContainerVp.setCurrentItem(0);
                    return true;
                case R.id.bnv_discovery:
                    mContainerVp.setCurrentItem(1);
                    return true;
                case R.id.bnv_mine:
                    mContainerVp.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    });
}
Also used : MineFragment(skin.support.mobile.demo.fragment.MineFragment) MenuItem(android.view.MenuItem) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) HomeFragment(skin.support.mobile.demo.fragment.HomeFragment) Fragment(androidx.fragment.app.Fragment) HomeFragment(skin.support.mobile.demo.fragment.HomeFragment) DiscoveryFragment(skin.support.mobile.demo.fragment.DiscoveryFragment) MineFragment(skin.support.mobile.demo.fragment.MineFragment) ViewPager2(androidx.viewpager2.widget.ViewPager2) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) NonNull(androidx.annotation.NonNull) DiscoveryFragment(skin.support.mobile.demo.fragment.DiscoveryFragment)

Example 3 with FragmentStateAdapter

use of androidx.viewpager2.adapter.FragmentStateAdapter 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

NonNull (androidx.annotation.NonNull)3 Fragment (androidx.fragment.app.Fragment)3 FragmentStateAdapter (androidx.viewpager2.adapter.FragmentStateAdapter)3 Bundle (android.os.Bundle)2 MenuItem (android.view.MenuItem)2 ViewPager2 (androidx.viewpager2.widget.ViewPager2)2 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 Nullable (androidx.annotation.Nullable)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 BindView (butterknife.BindView)1 ButterKnife (butterknife.ButterKnife)1 BottomNavigationView (com.google.android.material.bottomnavigation.BottomNavigationView)1 TabLayout (com.google.android.material.tabs.TabLayout)1 TabLayoutMediator (com.google.android.material.tabs.TabLayoutMediator)1 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)1