FragmentPagerAdapter - 设置计数和标题异步

时间:2016-03-09 12:40:57

标签: java android asynchronous fragmentpageradapter

我有folling FragmentPagerAdapter

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的async选项卡的这种实现?

2 个答案:

答案 0 :(得分:1)

将异步任务执行从您想要设置适配器的位置移动到活动或片段。

GetGameListTask getGamesTask = new GetGameListTask(this);
getGamesTask.execute();

在asyncTask的onPostExecute()中,执行

@Override
public void onPostExecute(){
    GameList games;
    GameListPagerAdapter adapter = new GameListPagerAdapter(getFragmentManager(), games);    
    view_pager.setAdapter(adapter);
}

像这样创建你的适配器

public class GameListPagerAdapter extends FragmentPagerAdapter {

    GameList _games;
    public int int_items = 6 ;

    public GameListPagerAdapter(FragmentManager fm, GameList _games) {
        super(fm);
        this._games = _games;
    }

    @Override
    public Fragment getItem(int position)
    {
        Bundle bundle = new Bundle();
        bundle.putInt("GameId", position);
        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) {
        return _games.get(position);
    }
}

答案 1 :(得分:0)

而不是返回

public int int_items = 6 ;

@Override public int getCount() { return int_items; }

你可能想要获取然后返回游戏数量。