-2
我正在使用Florent37-MateriralViewPager庫它運行良好,但我的要求是標籤上的圖標。現在我只使用文本。 圖書館的MainActivity.java如下。我如何添加圖標到標籤使用「florent37/MaterialViewPager」
mViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
mViewPager.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "T", Toast.LENGTH_LONG).show();
}
});
toolbar = mViewPager.getToolbar();
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (toolbar != null) {
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
}
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, 0, 0);
mDrawer.setDrawerListener(mDrawerToggle);
mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
int oldPosition = -1;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HotOffers.newInstance();
case 1:
return TwentyOffers.newInstance();
case 2:
return Brands.newInstance();
case 3:
return Category.newInstance();
default:
return RecyclerViewFragment.newInstance();
}
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
//only if position changed
if (position == oldPosition)
return;
oldPosition = position;
int color = 0;
String imageUrl = "http://myimage.jpg";
switch (position) {
case 0:
//imageUrl = arrayList.get(position);
color = getResources().getColor(R.color.cyan);
break;
case 1:
//imageUrl = arrayList.get(position);
color = getResources().getColor(R.color.cyan);
break;
case 2:
//imageUrl = arrayList.get(position);
color = getResources().getColor(R.color.cyan);
break;
case 3:
//imageUrl = arrayList.get(position);
color = getResources().getColor(R.color.cyan);
break;
}
final int fadeDuration = 500;
mViewPager.setImageUrl(imageUrl, fadeDuration);
mViewPager.setColor(color, fadeDuration);
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Top Twenty";
case 1:
return "Just For You";
case 2:
return "Brands";
case 3:
return "Category";
}
return "";
}
});
mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
mViewPager.getViewPager().setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
請幫助我如何將圖標添加到MaterialViewPager選項卡欄。謝謝。 。想要添加這樣的圖標。
請你提供了這樣的代碼片段, –
@KiranPardeshi我加入代碼 –
我應該在Florent的MaterialViewPager的MainActivity中寫這個嗎? –