• Activity和Fragment页面 TAB


    Activity和Fragment页面 TAB

    1. private TabLayout myTab;
    2. private ViewPager2 myPager2;
    3. List titles=new ArrayList<>();
    4. // List fragments=new ArrayList<>();
    5. List fragments=new ArrayList<>();
    6. @Override
    7. protected void onCreate(Bundle savedInstanceState) {
    8. super.onCreate(savedInstanceState);
    9. setContentView(R.layout.activity_milkpayback);
    10. myTab = findViewById(R.id.my_tab);
    11. myPager2 = findViewById(R.id.my_pager2);
    12. //添加标题
    13. titles.add("销售");
    14. titles.add("进货");
    15. //添加Fragment进去
    16. fragments.add(new sellFragment());
    17. fragments.add(new inkuspragment());
    18. //实例化适配器
    19. FgTabAdapter myAdapter= new FgTabAdapter(getSupportFragmentManager(),getLifecycle(),fragments);//getSupportFragmentManager()
    20. //设置适配器
    21. myPager2.setAdapter(myAdapter);
    22. //TabLayout和Viewpager2进行关联
    23. new TabLayoutMediator(myTab, myPager2, new TabLayoutMediator.TabConfigurationStrategy() {
    24. @Override
    25. public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
    26. tab.setText(titles.get(position));
    27. }
    28. }).attach();
    29. // myTab.getTabAt(1).select(); //默认选中某项放在加载viewpager之后 TabLayout 默认某个选项卡选中
    30. }

    Fragment页面

    1. //设置所在activity的引用
    2. Activity activity;
    3. View view;
    4. private TabLayout myTab;
    5. private ViewPager2 myPager2;
    6. List titles=new ArrayList<>();
    7. // List fragments=new ArrayList<>();
    8. List fragments=new ArrayList<>();
    9. @Override
    10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
    11. Bundle savedInstanceState) {
    12. // Inflate the layout for this fragment
    13. // return inflater.inflate(R.layout.fragment_sell, container, false);
    14. // view = inflater.inflate(R.layout.fragment_sell, null);
    15. view = inflater.inflate(R.layout.fragment_sell, null);
    16. activity=getActivity();
    17. myTab= (TabLayout) view.findViewById(R.id.tab_main);
    18. myPager2 = (ViewPager2) view.findViewById(R.id.vp_main);
    19. //添加标题
    20. titles.add("收银");
    21. titles.add("退货");
    22. titles.add("订单");
    23. titles.add("充值");
    24. //添加Fragment进去
    25. fragments.add(new OnespsellpayFragment());
    26. fragments.add(new onespsellreturnSPFragment());
    27. fragments.add(new OnespsellorderSPFragment());
    28. fragments.add(new onespsellInmoneyFragment());
    29. //实例化适配器
    30. ViewPageFragAdapter myAdapter= new ViewPageFragAdapter(getActivity().getSupportFragmentManager(),getLifecycle(),fragments);//getSupportFragmentManager()
    31. //设置适配器
    32. myPager2.setAdapter(myAdapter);
    33. //TabLayout和Viewpager2进行关联
    34. new TabLayoutMediator(myTab, myPager2, new TabLayoutMediator.TabConfigurationStrategy() {
    35. @Override
    36. public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
    37. tab.setText(titles.get(position));
    38. }
    39. }).attach();
    40. return view;
    41. }

  • 相关阅读:
    redis set zset key 常用命令
    Rust 从入门到精通07-函数
    为什么选择微服务架构? 微服务架构的10个核心优势 总结
    实现按钮悬停动画
    【JUC并发编程--java线程】
    2023 年高教社杯全国大学生数学建模竞赛题目 B 题 多波束测线问题
    Web前端入门(十三)CSS复合选择器
    dedecms织梦快照被挟持和篡改入侵漏洞修复
    C51 - 定时器
    RabbitMQ的LazyQueue
  • 原文地址:https://blog.csdn.net/wushijun5200/article/details/133821349