Created
November 30, 2015 09:03
-
-
Save rogerhu/24ef60eaa4218b3ceb47 to your computer and use it in GitHub Desktop.
SampleFragmentIconPagerAdapter
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
package codepath.com.tablayoutfun; | |
import android.os.Bundle; | |
import android.support.design.widget.TabLayout; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.ActionBarActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends ActionBarActivity { | |
TabLayout tabLayout; | |
ViewPager viewPager; | |
public static String POSITION = "POSITION"; | |
int curSelected; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Get the ViewPager and set it's PagerAdapter so that it can display items | |
viewPager = (ViewPager) findViewById(R.id.viewpager); | |
SampleFragmentIconPagerAdapter pagerAdapter = | |
new SampleFragmentIconPagerAdapter(getSupportFragmentManager(), MainActivity.this); | |
viewPager.setAdapter(pagerAdapter); | |
// Give the TabLayout the ViewPager | |
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); | |
tabLayout.setupWithViewPager(viewPager); | |
for (int i = 0; i < tabLayout.getTabCount(); i++) { | |
TabLayout.Tab tab = tabLayout.getTabAt(i); | |
tab.setIcon(pagerAdapter.getIcon(i)); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt(POSITION, tabLayout.getSelectedTabPosition()); | |
} | |
@Override | |
protected void onRestoreInstanceState(Bundle savedInstanceState) { | |
super.onRestoreInstanceState(savedInstanceState); | |
curSelected = savedInstanceState.getInt(POSITION); | |
viewPager.setCurrentItem(curSelected); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
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
package codepath.com.tablayoutfun; | |
import android.content.Context; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
public class SampleFragmentIconPagerAdapter extends FragmentPagerAdapter { | |
final int PAGE_COUNT = 3; | |
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; | |
private Integer tabIcons[] = new Integer[] { R.drawable.abc_btn_check_material, R.drawable.abc_ab_share_pack_mtrl_alpha, R.drawable.abc_btn_radio_material}; | |
private Context context; | |
public SampleFragmentIconPagerAdapter(FragmentManager fm, Context context) { | |
super(fm); | |
this.context = context; | |
} | |
@Override | |
public int getCount() { | |
return PAGE_COUNT; | |
} | |
@Override | |
public android.support.v4.app.Fragment getItem(int position) { | |
return PageFragment.newInstance(position + 1); | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
// Generate title based on item position | |
return tabTitles[position]; | |
} | |
public int getIcon(int position) { | |
return tabIcons[position]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment