本文主要是介绍ViewPager2+Fragment+TabLayout 实现页面滑动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Fragment
Frafment1中我还写了个Fragment切换,如果不需要可以删除,写成和Fragment2一样即可。
public class Fragment1 extends Fragment {public Fragment1() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_1, container, false);Button btn_random = (Button) view.findViewById(R.id.btn_random);getActivity().getSupportFragmentManager().beginTransaction()// 在指定的容器中添加新的Fragment.add(R.id.fragment_container,new FirstFragment())// 提交事务.commit();btn_random.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {FragmentManager fragmentManager = getActivity().getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// 创建数组Class<?>[] fragmentClasses = new Class<?>[]{FirstFragment.class,SecondFragment.class,ThirdFragment.class};// 生成一个随机索引int randomIndex = new Random().nextInt(fragmentClasses.length);// 根据随机索引创建对应的Fragment实例Fragment randomFragment = null;try {randomFragment = (Fragment) fragmentClasses[randomIndex].newInstance();} catch (InstantiationException | IllegalAccessException | java.lang.InstantiationException e) {e.printStackTrace();}// 替换为随机选择的Fragmentif (randomFragment != null) {fragmentTransaction.replace(R.id.fragment_container, randomFragment);fragmentTransaction.addToBackStack(null);fragmentTransaction.commit();}}});return view;}}
public class Fragment2 extends Fragment {public Fragment2() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_2, container, false);}
}
public class Fragment3 extends Fragment {public Fragment3() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_3, container, false);}
}
public class Fragment4 extends Fragment {public Fragment4() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_4, container, false);}
}
对于Fragment中的页面内容随便写个TextView就行。
FragmentAdapter
public class FragmentAdapter extends FragmentStateAdapter {List<Fragment> fragmentList = new ArrayList<>();public FragmentAdapter(@NonNull FragmentActivity fragmentActivity,List<Fragment> fragmentList) {super(fragmentActivity);this.fragmentList = fragmentList;}@NonNull@Overridepublic Fragment createFragment(int position) {return fragmentList.get(position);}@Overridepublic int getItemCount() {return fragmentList.size();}}
MainActivity
public class MainActivity extends AppCompatActivity {private ViewPager2 view_pager2;private TabLayout tab_layout;private List<Fragment> fragmentList;private static final String[] NAMES = {"Fragment1", "Fragment2", "Fragment3", "Fragment4"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//绑定空间view_pager2 = findViewById(R.id.view_pager2);tab_layout = findViewById(R.id.tab_layout);initFragmentList();FragmentAdapter adapter = new FragmentAdapter(MainActivity.this, fragmentList);view_pager2.setAdapter(adapter);view_pager2.setCurrentItem(0);//不支持viewpager2//tab_layout.setupWithViewPager(view_pager2);TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, view_pager2, false, true, (tab, position) -> tab.setText(NAMES[position]));tabLayoutMediator.attach();}public void initFragmentList() {fragmentList = new ArrayList<>();fragmentList.add(new Fragment1());fragmentList.add(new Fragment2());fragmentList.add(new Fragment3());fragmentList.add(new Fragment4());}
}
这篇关于ViewPager2+Fragment+TabLayout 实现页面滑动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!