java - FragmentPagerAdapter - 设置异步计数和标题

标签 java android asynchronous fragmentpageradapter

我有以下 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 的异步选项卡的实现?

最佳答案

将异步任务执行从您想要设置适配器的位置移至 Activity 或 fragment 。

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);
    }
}

关于java - FragmentPagerAdapter - 设置异步计数和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891898/

相关文章:

javascript - 异步脚本无法访问

android - 多种字体到单一 TextView 取决于语言

Android - Activity 之外的 startActivityForResult?

javascript - Node.js - 使用多个查询异步构建 JSON

Java Swing 模块化配色方案

android - 动态 ListView 项目不填充 XML

javascript - MySQL 减少 onclick 查询次数 - 异步更新

java - 如何防止使用 Hibernate + JPA 执行不需要的更新语句

java - 从一个txt文件导入数据到android数据库

java - 使用 main_activity 中的 extends Activity 在 Actionbar 中添加 Logo