我想實現帶滑動標籤的Google Play商店樣式菜單。我密切實施tutorial,但是在我的實現中,我通過CursorLoader啓動異步數據加載,我在PagerAdapter內部類的instantiateItem方法中初始化它。使用ViewPager滑動標籤
問題是,PagerAdapter調用instantiateItem兩次(因此實例化對應於兩個選項卡的視圖),它啓動兩次異步過程,並且每次用戶單擊不同的選項卡時,都會啓動異步加載另外兩次,造成巨大的難以控制的混亂。我怎樣才能讓PagerAdapter只調用一次instantiateItem?
謝謝。
編輯:這是我爲我的PagerAdapter sublcass代碼,實現教程:
class SamplePagerAdapter extends PagerAdapter {
/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 5;
}
/**
* @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
* same object as the {@link View} added to the {@link ViewPager}.
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
// BEGIN_INCLUDE (pageradapter_getpagetitle)
/**
* Return the title of the item at {@code position}. This is important as what this method
* returns is what is displayed in the {@link SlidingTabLayout}.
* <p>
* Here we construct one using the position value, but for real application the title should
* refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
CharSequence title = "default";
switch (position) {
case 0:
title = "Happiness";
break;
case 1:
title = "Intelligence";
break;
case 2:
title = "Memory";
break;
case 3:
title = "Personality";
break;
case 4:
title = "Success & Failure";
break;
}
return title;
}
// END_INCLUDE (pageradapter_getpagetitle)
/**
* Instantiate the {@link android.view.View} which should be displayed at {@code position}. Here we
* inflate a layout from the apps resources and then change the text view to signify the position.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
ListView view = (ListView) getActivity().getLayoutInflater().inflate(R.layout.article_list,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);
switch (position) {
case 0:
Intent intent1 = new Intent(getActivity(), ArticleService.class);
intent1.putExtra(ArticleService.CATEGORY_EXTRA, 1);
getActivity().startService(intent1);
getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
view.setAdapter(mArticleAdapter);
Log.v("PagerAdapter", "Adapter Set");
Log.v("PagerAdapter", "View added");
break;
case 1:
Intent intent2 = new Intent(getActivity(), ArticleService.class);
intent2.putExtra(ArticleService.CATEGORY_EXTRA, 2);
getActivity().startService(intent2);
getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
view.setAdapter(mArticleAdapter);
break;
case 2:
Intent intent3 = new Intent(getActivity(), ArticleService.class);
intent3.putExtra(ArticleService.CATEGORY_EXTRA, 3);
getActivity().startService(intent3);
getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
view.setAdapter(mArticleAdapter);
break;
case 3:
Intent intent4 = new Intent(getActivity(), ArticleService.class);
intent4.putExtra(ArticleService.CATEGORY_EXTRA, 4);
getActivity().startService(intent4);
getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
view.setAdapter(mArticleAdapter);
break;
case 4:
Intent intent5 = new Intent(getActivity(), ArticleService.class);
intent5.putExtra(ArticleService.CATEGORY_EXTRA, 5);
getActivity().startService(intent5);
getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
view.setAdapter(mArticleAdapter);
break;
}
// Return the View
return view;
}
/**
* Destroy the item from the {@link ViewPager}. In our case this is simply removing the
* {@link View}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
}
}
我知道這是PagerAdapter的默認行爲,以便優化內存使用和平滑滾動兩次調用該方法。本質上,我問是否有辦法改變它。
添加代碼,您應該從您採取的示例中更改代碼。 –
@HarshaVardhan請參閱編輯。 – AutomEng
添加您的自定義適配器類代碼.. –