tablayout手动添加数据

layout



    
    

fragment

public class MsgFragment extends Fragment {
    private TabLayout tab_layout;
    private ViewPager vp_news;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);
        tab_layout = view.findViewById(R.id.tab_layout);
        vp_news = view.findViewById(R.id.vp_news);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final List list = new ArrayList<>();
        list.add(new AndFragment());
        list.add(new IOSFragment());
        list.add(new WelfareFragment());

        vp_news.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        tab_layout.setupWithViewPager(vp_news);
        tab_layout.getTabAt(0).setText("Android");
        tab_layout.getTabAt(1).setText("IOS");
        tab_layout.getTabAt(2).setText("福利");
    }
}

你可能感兴趣的:(tablayout手动添加数据)