Last active
May 16, 2017 05:35
-
-
Save samuel22gj/8d74f8c84746bf1e270a852a55f20632 to your computer and use it in GitHub Desktop.
A wrap Fragment with TabLayout and ViewPager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/fragment_background" | |
android:orientation="vertical"> | |
<android.support.design.widget.TabLayout | |
android:id="@+id/tab_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@android:color/white" | |
app:tabMode="fixed" | |
app:tabGravity="fill" | |
app:tabMaxWidth="0dp" | |
app:tabBackground="@android:color/white" | |
app:tabIndicatorColor="@color/theme_color" | |
app:tabSelectedTextColor="@color/grey_dark" | |
app:tabTextColor="@color/font_dark_support" /> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/view_pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class TabViewPagerFragment extends ECFragment implements TabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener { | |
private static final String TAG = ECTabViewPagerFragment.class.getSimpleName(); | |
private TabLayout mTabLayout; | |
@TabLayout.Mode | |
private int mTabLayoutMode = TabLayout.MODE_FIXED; | |
private ViewPager mViewPager; | |
private TabPageAdapter mPagerAdapter; | |
/* | |
* Mandatory empty constructor for the fragment manager to instantiate the | |
* fragment (e.g. upon screen orientation changes). | |
*/ | |
public TabViewPagerFragment() { | |
} | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_tab_viewpager, container, false); | |
mTabLayout = ViewUtils.findViewById(view, R.id.tab_layout); | |
mViewPager = ViewUtils.findViewById(view, R.id.view_pager); | |
mTabLayout.setTabMode(mTabLayoutMode); | |
mTabLayout.setupWithViewPager(mViewPager); | |
mTabLayout.addOnTabSelectedListener(this); | |
mPagerAdapter = new TabPageAdapter(getChildFragmentManager()); | |
mViewPager.setAdapter(mPagerAdapter); | |
mViewPager.addOnPageChangeListener(this); | |
return view; | |
} | |
@Override | |
public void onDestroyView() { | |
super.onDestroyView(); | |
mTabLayout.removeOnTabSelectedListener(this); | |
mViewPager.removeOnPageChangeListener(this); | |
} | |
@Override | |
public void onTabSelected(TabLayout.Tab tab) { | |
} | |
@Override | |
public void onTabUnselected(TabLayout.Tab tab) { | |
} | |
@Override | |
public void onTabReselected(TabLayout.Tab tab) { | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
public void setTabMode(@TabLayout.Mode int mode) { | |
mTabLayoutMode = mode; | |
if (mTabLayout != null) { | |
mTabLayout.setTabMode(mTabLayoutMode); | |
} | |
} | |
public int getCurrentTab() { | |
return mViewPager.getCurrentItem(); | |
} | |
public void setCurrentTab(int position) { | |
mViewPager.setCurrentItem(position); | |
} | |
public void setCurrentTab(int position, boolean smoothScroll) { | |
mViewPager.setCurrentItem(position, smoothScroll); | |
} | |
public void notifyTabPagerDataSetChanged() { | |
mPagerAdapter.notifyDataSetChanged(); | |
} | |
public Fragment getFragment(int position) { | |
return mPagerAdapter.getFragment(position); | |
} | |
public Fragment getCurrentFragment() { | |
return mPagerAdapter.getFragment(mViewPager.getCurrentItem()); | |
} | |
public abstract Fragment prepareFragmentPage(int position); | |
public abstract int prepareFragmentPageCount(); | |
public abstract CharSequence prepareFragmentPageTitle(int position); | |
private class TabPageAdapter extends ECFragmentStatePagerAdapter { | |
TabPageAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return prepareFragmentPage(position); | |
} | |
@Override | |
public int getCount() { | |
return prepareFragmentPageCount(); | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return prepareFragmentPageTitle(position); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment