Skip to content

Instantly share code, notes, and snippets.

@d4rkc0de
Created March 9, 2017 22:47
Show Gist options
  • Save d4rkc0de/1f3d7e0f25c79323a180f99f4fadb043 to your computer and use it in GitHub Desktop.
Save d4rkc0de/1f3d7e0f25c79323a180f99f4fadb043 to your computer and use it in GitHub Desktop.
ViewPager with tabs code
// dependency for TabLayout
compile 'com.android.support:design:24.1.1'
// xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" >
</include>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="72dp"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
// Java
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
TextView tabOne,tabTwo,tabThree,tabFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
/**
* Adding fragments to ViewPager
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FeedFragment(), "ONE");
adapter.addFrag(new NotifcationsFragment(), "TWO");
adapter.addFrag(new SearchThemeFragment(), "THREE");
adapter.addFrag(new ProfileFragment(), "FOUR");
viewPager.setAdapter(adapter);
}
/**
* Adding custom view to tab
*/
private void setupTabIcons() {
tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Questions");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_question_white_tint, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("Notifications");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_notifications, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("Recherche");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_search, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFour.setText("Profil");
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_profil, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
class ViewPagerAdapter extends FragmentPagerAdapter { // every fragment should be imported from v4.Fragment and not support.Fragment
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);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment