Question Detail

the following is the java code

   private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.ic_dashboard_menubar_icon,
            R.drawable.ic_recent_menu_icon,
            R.drawable.ic_expences_menu_bar_icon,
            R.drawable.ic_group_menu_icon,
            R.drawable.ic_friends_menu_icon
    };

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);

        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
        num= num+1;

        Toast.makeText(getActivity(),"hello"+num,Toast.LENGTH_LONG).show();
        // Inflate the layout for this fragment
        return view;
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);
        tabLayout.getTabAt(4).setIcon(tabIcons[4]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
        adapter.addFrag(new Fragment_DashBoard());
        adapter.addFrag(new Fragment_Recent_Activities());
        adapter.addFrag(new Fragment_All_Expenses());
        adapter.addFrag(new Fragment_Groups());
        adapter.addFrag(new Fragments_Friends());
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }
        public void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);
        }
       /* @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }*/

    }



}


Thread Reply

Ankur Khandelwal

- 1 years ago

Where is the code for navigation bar? Are you treating tablayout as navigation bar?