我已在folling FragmentPagerAdapterFragmentPagerAdapter - 設定計數和標題異步
public class GameListPagerAdapter extends FragmentPagerAdapter implements IGetGameListTaskListener {
GameList _games;
public int int_items = 6 ;
public GameListPagerAdapter(FragmentManager fm) {
super(fm);
//async task to get a list of games
GetGameListTask getGamesTask = new GetGameListTask(this);
getGamesTask.execute();
}
@Override
public Fragment getItem(int position)
{
Bundle bundle = new Bundle();
bundle.putInt("GameId", position);
//get the generic matchlistfragment and tell it which game it is
MatchListFragment matchListFragment = new MatchListFragment();
matchListFragment.setArguments(bundle);
return matchListFragment; // always return the same fragment, just give it a different game id
}
@Override
public int getCount() {
return int_items;
}
@Override
public CharSequence getPageTitle(int position) {
//NEED HELP - Need to get the tiltle here
//return _games.getGames().get(position-1).getName();
switch (position) {
case 0:
return "One";
case 1:
return "Two";
case 2:
return "Three";
case 3:
return "Four";
case 4:
return "Five";
case 5:
return "Six";
}
return null;
}
@Override
public void onComplete(GameList data) {
//get the data on onPostExecute of my "getGamesTask"
_games = data;
//NEED HELP - need to set the item count here, but it fails**
//int_items = _games.getGames().size();
}
}
這裏是我的IGetGameListTaskListener
接口:
public interface IGetGameListTaskListener {
public void onComplete(GameList data);
}
我這裏的問題是,我從api獲取遊戲列表,這是設置爲異步任務GetGameListTask
。然後,我想爲該列表中返回的每個遊戲創建一個選項卡。
一旦有了遊戲onComplete(GameList data)
我需要設置int_items
,以及int_items
然而,由於(我的理解)的FragmentPagerAdapter的生命週期清單,這些值需要提供前期。
我將如何處理我的FragmentPagerAdapter的異步選項卡的這種實現?
不要事先創建適配器對象。在onPostExecute()中或在獲得結果後創建它。獲得結果後,可以將它們作爲數組或列表發送給適配器的構造函數並使用它們。 –
嘿,爲什麼不執行這個GetGameListTask getGamesTask = new GetGameListTask(this);在調用GameListPagerAdapter之前?並且在DoInBackGround完成之後,你可以通過GamelIst並設置大小,這種方法是否有任何疑問? –
@ankitaggarwal這只是將問題移到我的viewpager。我現在必須在viewpager中執行後臺任務,然後設置viewPager.setAdapter –