3

I want this type of design :: Here Is My design

My mainactivity.xml only contain a frameLayout which is Replaced by Selected Fragment from bottombar menu item.

It's code is here:

b = BottomBar.attach(this,savedInstanceState);
    b.setItemsFromMenu(R.xml.menu_main, new OnMenuTabClickListener() {
        @Override
        public void onMenuTabSelected(@IdRes int menuItemId) {
            if(menuItemId == R.id.BottomBarItemOne){
                PeopleFragment p = new PeopleFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,p).commit();
            }
            else if(menuItemId == R.id.BottomBarItemTwo){
                LocationFragment l = new LocationFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,l).commit();
            }
            else if(menuItemId == R.id.BottomBarItemThree){
                HistoryFragment h = new HistoryFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,h).commit();
            }
            else if(menuItemId == R.id.BottomBarItemFour){
                LikesFragment li = new LikesFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,li).commit();
            }
        }

Now I want to create a tabview in on one of the Fragment : For that I implement toolbar,tablayout and viewpager in required fragment xml file. I created a separate java file contain viewPagerAdapter class.

public class viewPagerAdapter extends FragmentPagerAdapter {

ArrayList<Fragment> fragments = new ArrayList<>(); // this line can cause crashes
ArrayList<String> tabTitles = new ArrayList<>();

   public void addFragment(Fragment fragment,String tabTitle){
      fragments.add(fragment); // this line can cause crashes
      tabTitles.add(tabTitle);
   }

   public viewPagerAdapter(FragmentManager fm) {
      super(fm);
   }

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

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

   @Override
   public CharSequence getPageTitle(int position) {
      return tabTitles.get(position);
   }
}

Now I want to add this code to implement tablayout ::

 toolbar = (Toolbar) findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    viewPagerAdapter = new viewPagerAdapter(getSupportFragmentManager());
    viewPagerAdapter.addFragment(new HomeFragment(),"Home"); // this line can cause crashes
    viewPagerAdapter.addFragment(new MessageFragment(),"Message"); // this line can cause crashes
    viewPagerAdapter.addFragment(new ContectFragment(),"Contect"); // this line can cause crashes
    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

In Fragment's onCreate method Given below ::

public class HomeFragment extends Fragment {


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

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

       // TabLyout Code Here

        return inflater.inflate(R.layout.fragment_home, container, false);
    }
 }

But Android Studio doesn't allow Tablayout Fragment code in Fragment.
What Can I do ? How can I implement tablayout in one of the fragment?

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
VISHAL TANK
  • 215
  • 4
  • 9

1 Answers1

0

Try the following approach : Add this to your fragment layout as per your requirement.

 <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:tabIndicatorColor="@color/tab_indicator"
            app:tabIndicatorHeight="2.5dp"
            app:tabPaddingBottom="-1dp"
            app:tabPaddingEnd="-1dp"
            app:tabPaddingStart="-1dp"
            app:tabPaddingTop="-1dp"
            app:tabBackground="@color/white"/>
  1. Create one model for your tabs which you want to show.

  2. Now add the following code in your fragment:

    this.navigationData = new ArrayList<>();
    this.navigationData.add(new MainNavigationViewModel("TAB-1",     R.drawable.TAB-1, R.drawable.TAB-1, true));
    this.navigationData.add(new MainNavigationViewModel("TAB-2", R.drawable.TAB-2, R.drawable.TAB-2, false));
    this.navigationData.add(new MainNavigationViewModel("TAB-3", R.drawable.TAB-3, R.drawable.TAB-23, false));
    
    for (MainNavigationViewModel vm : this.navigationData) {
        tabLayout.addTab(tabLayout.newTab().setCustomView(ViewHelper.buildTabWithText(getContext(), vm.getText(), vm.getIsSelected())));
    }
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    
    adapter = new MainFragmentPagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    
  3. Now create FragmentPagerAdapter (New class) like this.

    public class MainFragmentPagerAdapter extends FragmentPagerAdapter {
        private interface FragmentFactory {
            Fragment create();
        }
    
        private static final FragmentFactory[] fragments = new FragmentFactory[] {
                () -> LiveFeedFragment.getInstance(),
                () -> GroupsFragment.getInstance(),
                () -> NewsFragment.getInstance()
        };
    
    
        public MainFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragments[position].create();
        }
    
        @Override
        public int getCount() {
            return fragments.length;
        }
    }
    

And access the fragment like this way.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Parin Parikh
  • 385
  • 1
  • 6
  • I just started learning Android. So can please tell me How can I create model ( MainNavigationViewModel ) of tabs ? what it contain ? – VISHAL TANK Jan 07 '17 at 12:08
  • You have to create model as per your requirement.For ex. If you only have tab with text then you can create model like this way : public class MainNavigationViewModel { int id; String text; public String getText() { return text; } public void setText(String text) { this.text = text; } } Model contains getter or setter methods which you have to access from your activity. Please give upvote if this post helpful to you. – Parin Parikh Jan 07 '17 at 12:20
  • this.navigationData.add(new MainNavigationViewModel("TAB-1", R.drawable.TAB-1, R.drawable.TAB-1, true)); What is 2nd and 3rd argument of MainNavigationViewModel constructor ? what this file contain? and tabLayout.addTab(tabLayout.newTab().setCustomView(ViewHelper.buildTabWithText(getContext(), vm.getText(), vm.getIsSelected()))); and what is ViewHelper ? what it contain ? – VISHAL TANK Jan 07 '17 at 13:10
  • 1. 2nd argument in the constructor is used to display image of the tab along with the text and 3rd argument in the constructor is used to change the selected image.Ignore the second and third argument if you have only text in the tab. 2. The role of this file is to change the fragment along with you change the tab. 3. ViewHelper file contains the code to add the tab. I will post the code for viewhelper file. – Parin Parikh Jan 09 '17 at 06:03
  • ohk, Thanks a lot. – VISHAL TANK Jan 14 '17 at 11:24