我正在使用Design Library支持TabLayout,我正在尝试在Tab被按下或ViewPager滑动时更改工具栏的标题。但到目前为止无法取得任何成功。这是以下代码: -
public class ActivityHomePage extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
Toolbar toolbar;
DesignDemoPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// viewPager.getAdapter().getPageTitle();
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private int[] tabIcons = {
R.drawable.ic_action_star,
R.drawable.ic_action_heart,
R.drawable.ic_action_square
};
private void setupViewPager(ViewPager viewPager) {
adapter = new DesignDemoPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentStar(), "star");
adapter.addFrag(new FragmentHeart(), "heart");
adapter.addFrag(new FragmentSquare(), "square");
viewPager.setAdapter(adapter);
}
@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;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
@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();
switch (id) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
class DesignDemoPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public DesignDemoPagerAdapter(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 null;
}
}
}
答案 0 :(得分:7)
最后我能够做到,可能对某人有所帮助,所以这里是上面的答案: -
/** Tab & PageViewer Controller**/
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_star));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_heart));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_square));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// setupTabIcons();
viewPager = (ViewPager) findViewById(R.id.viewpager);
pAdapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pAdapter);
toolbar.setTitle("Same Page");
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch(tab.getPosition()) {
case 0:
viewPager.setCurrentItem(0);
toolbar.setTitle("Fragment Star");
break;
case 1:
viewPager.setCurrentItem(1);
toolbar.setTitle("Fragment Heart");
break;
case 2:
viewPager.setCurrentItem(2);
toolbar.setTitle("Fragment Squae");
break;
default:
viewPager.setCurrentItem(tab.getPosition());
toolbar.setTitle("Fragment Star");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
/** **/
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentStar tab0 = new FragmentStar();
return tab0;
case 1:
FragmentHeart tab1 = new FragmentHeart();
return tab1;
case 2:
FragmentSquare tab2 = new FragmentSquare();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
答案 1 :(得分:0)
如果有人来这里寻求帮助。 这是@Khanna代码的更简单和更好的版本
根据标签名称在类中初始化一个数组。
CharSequence title [] = {&#34; Star&#34;,&#34; Heart&#34;,&#34; Square&#34;};
现在只需使用。
toolbar.setTitle(标题[tab.getPosition()]);
这是编辑过的代码。
CharSequence titles[] = {"Star","Heart", "Square"};
/** Tab & PageViewer Controller**/
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_star));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_heart));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_square));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// setupTabIcons();
viewPager = (ViewPager) findViewById(R.id.viewpager);
pAdapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pAdapter);
toolbar.setTitle("Same Page");
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(0);
//Now just set title using your array
toolbar.setTitle(titles[tab.getPosition()]);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public class PagerAdapter extends FragmentStatePagerAdapter{
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentStar tab0 = new FragmentStar();
return tab0;
case 1:
FragmentHeart tab1 = new FragmentHeart();
return tab1;
case 2:
FragmentSquare tab2 = new FragmentSquare();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
答案 2 :(得分:0)
不建议使用setOnTabSelectedListener,您可以像以下示例一样使用
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
viewPager.setCurrentItem(0);
getSupportActionBar().setTitle("Recipes");
break;
case 1:
viewPager.setCurrentItem(1);
getSupportActionBar().setTitle("Categories");
break;
case 2:
viewPager.setCurrentItem(2);
getSupportActionBar().setTitle("Favorites");
break;
default:
viewPager.setCurrentItem(tab.getPosition());
getSupportActionBar().setTitle("Cook Book");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});