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));
}
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;
}
});
}
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();
}
Aggregations