Activity和Fragment页面 TAB
- private TabLayout myTab;
- private ViewPager2 myPager2;
-
- List
titles=new ArrayList<>(); - // List
fragments=new ArrayList<>(); - List
fragments=new ArrayList<>(); -
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_milkpayback);
-
-
- myTab = findViewById(R.id.my_tab);
- myPager2 = findViewById(R.id.my_pager2);
-
- //添加标题
- titles.add("销售");
-
- titles.add("进货");
- //添加Fragment进去
- fragments.add(new sellFragment());
- fragments.add(new inkuspragment());
-
- //实例化适配器
- FgTabAdapter myAdapter= new FgTabAdapter(getSupportFragmentManager(),getLifecycle(),fragments);//getSupportFragmentManager()
- //设置适配器
- myPager2.setAdapter(myAdapter);
- //TabLayout和Viewpager2进行关联
- new TabLayoutMediator(myTab, myPager2, new TabLayoutMediator.TabConfigurationStrategy() {
- @Override
- public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
- tab.setText(titles.get(position));
- }
- }).attach();
-
- // myTab.getTabAt(1).select(); //默认选中某项放在加载viewpager之后 TabLayout 默认某个选项卡选中
-
- }
Fragment页面
- //设置所在activity的引用
- Activity activity;
- View view;
- private TabLayout myTab;
- private ViewPager2 myPager2;
-
- List
titles=new ArrayList<>(); - // List
fragments=new ArrayList<>(); - List
fragments=new ArrayList<>(); -
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- // return inflater.inflate(R.layout.fragment_sell, container, false);
- // view = inflater.inflate(R.layout.fragment_sell, null);
- view = inflater.inflate(R.layout.fragment_sell, null);
-
- activity=getActivity();
-
-
-
-
- myTab= (TabLayout) view.findViewById(R.id.tab_main);
- myPager2 = (ViewPager2) view.findViewById(R.id.vp_main);
- //添加标题
- titles.add("收银");
-
- titles.add("退货");
- titles.add("订单");
- titles.add("充值");
- //添加Fragment进去
- fragments.add(new OnespsellpayFragment());
- fragments.add(new onespsellreturnSPFragment());
- fragments.add(new OnespsellorderSPFragment());
- fragments.add(new onespsellInmoneyFragment());
- //实例化适配器
- ViewPageFragAdapter myAdapter= new ViewPageFragAdapter(getActivity().getSupportFragmentManager(),getLifecycle(),fragments);//getSupportFragmentManager()
- //设置适配器
- myPager2.setAdapter(myAdapter);
- //TabLayout和Viewpager2进行关联
- new TabLayoutMediator(myTab, myPager2, new TabLayoutMediator.TabConfigurationStrategy() {
- @Override
- public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
- tab.setText(titles.get(position));
- }
- }).attach();
-
- return view;
-
-
-
- }