Skip to content

Instantly share code, notes, and snippets.

@TheLittleNaruto
Last active May 12, 2016 11:29
Show Gist options
  • Save TheLittleNaruto/6ed21a42d82a85e148b1c689c20cf0b9 to your computer and use it in GitHub Desktop.
Save TheLittleNaruto/6ed21a42d82a85e148b1c689c20cf0b9 to your computer and use it in GitHub Desktop.
PagerAdapter with custom tabs
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center" />
<TextView
android:id="@+id/text1"
style="@style/Base.TextAppearance.AppCompat.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="@android:color/white" />
</LinearLayout>
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOffscreenPageLimit(NUM_PAGES);
footerView = findViewById(R.id.footer);
errorView = findViewById(R.id.errorMessage);
footerView.setVisibility(View.GONE);
initializeTabs();
mPager.requestTransparentRegion(mPager);
public View addTabView(String text, int icon){
View view = LayoutInflater.from(this).inflate(R.layout.custom_tab_view, null, false);
TextView textView = (TextView) view.findViewById(R.id.text1);
ImageView imageView = (ImageView) view.findViewById(R.id.img);
textView.setText(text);
imageView.setImageResource(icon);
return view;
}
private void initializeTabs() {
tabLayout = (AutoWrapTabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(mPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(mPagerAdapter.getTabView(i));
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
private String tabTitles[] = new String[] { getString(R.string.action_title1), getString(R.string.action_title2), getString(R.string.action_title3) };
private int[] imageResId = { R.mipmap.ic_1, R.mipmap.ic_2, R.mipmap.ic_3 };
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if(position == 1) {
fragment = new Fragment1();
} else if (position == 0){
fragment = new Fragment2();
} else if (position == 2){
fragment = new Fragment3();
}
return fragment;
}
// Register the fragment when the item is instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View v = addTabView(tabTitles[position], imageResId[position]);
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment