Skip to content

Instantly share code, notes, and snippets.

@sharifulislam52
Created March 2, 2018 09:52
Show Gist options
  • Save sharifulislam52/6360e08f6f94c9ee4290e49d519c15c4 to your computer and use it in GitHub Desktop.
Save sharifulislam52/6360e08f6f94c9ee4290e49d519c15c4 to your computer and use it in GitHub Desktop.
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
case 3:
return new FragmentFour();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 4;
}
}
// PagerAdapter, fragmentManager
ViewPager viewPager;
TabLayout tabLayout;
PagerAdapter pagerAdapter;
//-----> into onCreate method
viewPager = (ViewPager)findViewById(R.id.pager_id);
tabLayout = (TabLayout)findViewById(R.id.tablayout_id);
// call method
slider_fragment();
//<----- into onCreate method
public void slider_fragment(){
/**
* Create tabLayout and add tab names, text color.
*/
//tabLayout.addTab(tabLayout.newTab().setText("schedules")); // set text
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon2));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon3));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon4));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(ColorStateList.valueOf(Color.COLOR_NAME));
/**
* set icon color
*/
tabLayout.getTabAt(0).getIcon().setColorFilter(ContextCompat.getColor(your_activity.this, R.color.color_name), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(ContextCompat.getColor(your_activity.this, R.color.color_name), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(ContextCompat.getColor(your_activity.this, R.color.color_name), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(ContextCompat.getColor(your_activity.this, R.color.color_name), PorterDuff.Mode.SRC_IN);
// tabLayout background color
//tabLayout.setBackgroundTintList(ColorStateList.valueOf(Color.COLOR_NAME));
/**
* Create viewPager for show fragment
*/
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
/**
* the action of tabLayout
*/
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
current_position = tab.getPosition();
/**
* set icon color when fragment change
*/
int tabIconColor = ContextCompat.getColor(your_activity.this, R.color.color_name);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
/**
* set icon color when fragment change
*/
int tabIconColor = ContextCompat.getColor(your_activity.this, R.color.color_name);
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
}
//-----> helper method
// it will help to refresh tabLayout
void refreshAdapter(int position) {
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
// when notify then set manually current position.
viewPager.setCurrentItem(position);
pagerAdapter.notifyDataSetChanged();
}
//<----- helper method
// viewPager, tabLayout, pagerAdapter, pager_id, tablayout_id, icon, COLOR_NAME, color_name,
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible"
app:tabIndicatorColor="@android:color/color_name"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
<!-- tablayout_id, colorPrimary, actionBarSize, color_name, pager_id -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment