我将PageIndicator
与ViewPager
一起使用,但只显示了网页标题。页面图标不会出现。有人知道为什么吗?这是我的适配器。
class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override public int getIconResId(int index) {
return ICONS[index];
}
@Override
public int getCount() {
return CONTENT.length;
}
}
我的适配器
public class FavouritePagerAdapter extends PagerAdapter implements
IconPagerAdapter, ViewPager.OnPageChangeListener {
private static final int FAVOURITE_EPISODE_INDEX = 0;
private static final int FAVOURITE_SHOW_INDEX = 1;
private static final int NUMBER_OF_PAGES = 2;
private static final String[] FAVOURITE_TITLES = {
Util.getApplicationContext().getString(
R.string.favourite_title_episode),
Util.getApplicationContext().getString(
R.string.favourite_title_show) };
public static final String TAG_EMPTY_EPISODE = "emtpy_episode";
public static final String TAG_EMPTY_SHOW = "empty_show";
public static final String TAG_LOADING_EPISODE = "loading_epsiode";
public static final String TAG_LOADING_SHOW = "loading_show";
WeakReference<Activity> acitivity;
WeakReference<FavouriteFragment> favouriteFragment;
FavouriteEpisodeAdapter mFavouriteEpisodeAdapter;
FavouriteShowAdapter mFavouriteShowAdapter;
public FavouritePagerAdapter(FavouriteFragment fr, Activity a,
ArrayList<EpisodeInfo> episode, ArrayList<ShowData> show) {
super();
this.favouriteFragment = new WeakReference<FavouriteFragment>(fr);
this.acitivity = new WeakReference<Activity>(a);
mFavouriteEpisodeAdapter = new FavouriteEpisodeAdapter(a,
R.id.textView1, episode);
mFavouriteShowAdapter = new FavouriteShowAdapter(a, R.id.textView1,
show);
}
@Override
public CharSequence getPageTitle(int position) {
Log.d("getPageTitle", FAVOURITE_TITLES[position]);
return FAVOURITE_TITLES[position];
}
@Override
public int getIconResId(int index) {
return R.drawable.ic_action_search;
}
@Override
public int getCount() {
return NUMBER_OF_PAGES;
}
@Override
public Object instantiateItem(View container, int position) {
View v = this.acitivity.get().getLayoutInflater()
.inflate(R.layout.favourite_episode_fragment_layout, null);
ListView list = (ListView) v.findViewById(R.id.listView1);
TextView empty_text = (TextView) v.findViewById(R.id.empty);
View loadingView = v.findViewById(R.id.loadingView);
switch (position) {
case FAVOURITE_EPISODE_INDEX:
list.setAdapter(getmFavouriteEpisodeAdapter());
empty_text.setText(Util.getApplicationContext().getString(
R.string.favourite_empty_episode));
empty_text.setTag(TAG_EMPTY_EPISODE);
loadingView.setTag(TAG_LOADING_EPISODE);
if (favouriteFragment.get().episodeInitialized.get()
&& getmFavouriteEpisodeAdapter().getCount() == 0) {
loadingView.setVisibility(View.GONE);
empty_text.setVisibility(View.VISIBLE);
}
break;
case FAVOURITE_SHOW_INDEX:
list.setAdapter(getmFavouriteShowAdapter());
empty_text.setText(Util.getApplicationContext().getString(
R.string.favourite_empty_show));
empty_text.setTag(TAG_EMPTY_SHOW);
loadingView.setTag(TAG_LOADING_SHOW);
if (favouriteFragment.get().showInitialized.get()
&& getmFavouriteShowAdapter().getCount() == 0) {
empty_text.setVisibility(View.VISIBLE);
loadingView.setVisibility(View.GONE);
}
break;
}
((ViewPager) container).addView(v, 0);
if (((ViewPager) container).getCurrentItem() == 0) {
favouriteFragment.get().loadEpisode();
}
return v;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
public FavouriteEpisodeAdapter getmFavouriteEpisodeAdapter() {
return mFavouriteEpisodeAdapter;
}
public void setmFavouriteEpisodeAdapter(
FavouriteEpisodeAdapter mFavouriteEpisodeAdapter) {
this.mFavouriteEpisodeAdapter = mFavouriteEpisodeAdapter;
}
public FavouriteShowAdapter getmFavouriteShowAdapter() {
return mFavouriteShowAdapter;
}
public void setmFavouriteShowAdapter(
FavouriteShowAdapter mFavouriteShowAdapter) {
this.mFavouriteShowAdapter = mFavouriteShowAdapter;
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
figureWhichToLoad(arg0);
}
private void figureWhichToLoad(int arg0) {
try {
switch (arg0) {
case 0:// episode
favouriteFragment.get().loadEpisode();
break;
case 1:// show
favouriteFragment.get().loadShow();
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}