我已在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 –