FragmentPagerAdapter - 设置计数和标题异步
FragmentPagerAdapter - Set Count and Titles async
我有以下 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 的 life-cycle,这些值需要预先提供。
我将如何为我的 FragmentPagerAdapter 处理这种异步选项卡的实现?
而不是 returning
public int int_items = 6 ;
在
@Override
public int getCount() {
return int_items;
}
您可能想要获取然后 return 游戏数量。
将您的异步任务执行移动到 activity 或您想设置适配器的任何片段。
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);
}
}
我有以下 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 的 life-cycle,这些值需要预先提供。
我将如何为我的 FragmentPagerAdapter 处理这种异步选项卡的实现?
而不是 returning
public int int_items = 6 ;
在
@Override
public int getCount() {
return int_items;
}
您可能想要获取然后 return 游戏数量。
将您的异步任务执行移动到 activity 或您想设置适配器的任何片段。
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);
}
}