gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:15:55 26 4
gpt4 key购买 nike

我有以下 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com