gpt4 book ai didi

java - 无法交换 ViewPager 中显示的 fragment

转载 作者:行者123 更新时间:2023-11-30 02:51:37 25 4
gpt4 key购买 nike

我正在更改一个应用程序的导航,使其工作方式如下:该应用程序将包含一个 Activity ,该 Activity 最初显示一个具有三个 View ( fragment )的 ViewPager,并且在其中一个 View 中有一个按钮可以用另外两个 fragment 替换这些 fragment 。

我在 this answer 中发现我不能使用 FragmentPagerAdapter 因为它在第一次显示后永远不会销毁 fragment ;因为我希望交换 fragment ,所以我将不得不使用 FragmentStatePagerAdapter,同时确保始终在方法 getItem(int position) 中返回一个新的 fragment 实例。但是,为了让 ViewPager 在更改绑定(bind)到它的适配器后有效地更新 View (实际上我不更改适配器,只更改与其关联的数据,我用它来实例化 fragment ),我必须覆盖方法 getItemPosition(Object object) 以使其返回 POSITION_NONE 并在适配器中调用 notifyDataSetChanged()。但是,在该过程之后 View 没有更新(这正是我的问题)。

我的代码如下:

ConfiguracoesDeItemDoViewPager.java(包含适配器实例化新 fragment 所需的数据):

public static class ConfiguracoesDeItemDoViewPager {

private String nomeDaAba;
private Class<? extends FragmentoBase> classeDoFragmento;
private Bundle argumentosDoFragmento;

public ConfiguracoesDeItemDoViewPager(String nomeDaAba, Class<? extends FragmentoBase> classeDoFragmento, Bundle argumentosDoFragmento) {
this.nomeDaAba = nomeDaAba;
this.classeDoFragmento = classeDoFragmento;
this.argumentosDoFragmento = argumentosDoFragmento;
}

public String getNomeDaAba() {
return nomeDaAba;
}

public void setNomeDaAba(String nomeDaAba) {
this.nomeDaAba = nomeDaAba;
}

public Class<? extends FragmentoBase> getClasseDoFragmento() {
return classeDoFragmento;
}

public void setClasseDoFragmento(
Class<? extends FragmentoBase> classeDoFragmento) {
this.classeDoFragmento = classeDoFragmento;
}

public Bundle getArgumentosDoFragmento() {
return argumentosDoFragmento;
}

public void setArgumentosDoFragmento(Bundle argumentosDoFragmento) {
this.argumentosDoFragmento = argumentosDoFragmento;
}
}

Constantes.java(提供数据以使用一组 fragment 配置 ViewPager):

public class Constantes {

public static final ConfiguracoesDeItemDoViewPager [] CONFIGURACOES_DO_VIEWPAGER_1;
public static final ConfiguracoesDeItemDoViewPager [] CONFIGURACOES_DO_VIEWPAGER_2;

// Bloco de inicialização de valores estáticos
static {
CONFIGURACOES_DO_VIEWPAGER_1 = new DadosDeItemDoViewPager[3];
CONFIGURACOES_DO_VIEWPAGER_1[0] = new DadosDeItemDoViewPager("Aba 1", FragmentoA.class, null);
CONFIGURACOES_DO_VIEWPAGER_1[1] = new DadosDeItemDoViewPager("Aba 2", FragmentoB.class, null);
CONFIGURACOES_DO_VIEWPAGER_1[2] = new DadosDeItemDoViewPager("Aba 3", FragmentoC.class, null);

CONFIGURACOES_DO_VIEWPAGER_2 = new DadosDeItemDoViewPager[2];
CONFIGURACOES_DO_VIEWPAGER_2[0] = new DadosDeItemDoViewPager("Aba 1", FragmentoD.class, null);
CONFIGURACOES_DO_VIEWPAGER_2[1] = new DadosDeItemDoViewPager("Aba 2", FragmentoE.class, null);
}

...

}

AtividadePrincipal.java(应用程序中唯一的 Activity ,具有 FragmentStatePagerAdapter 的内部类):

public class AtividadePrincipal extends ActionBarActivity {

private ViewPager mViewPager;
private TabsPagerAdapter mTabsPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager(), Constantes.CONFIGURACOES_DO_VIEWPAGER_1);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mTabsPagerAdapter);
}

public void exibirViewPager2() {
((TabsPagerAdapter)mViewPager.getAdapter()).setConfiguracoesParaInicializarViewPager(Constantes.CONFIGURACOES_DO_VIEWPAGER_2);
}

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

private FragmentManager mFragmentManager;
private ConfiguracoesDeItemDoViewPager [] mConfiguracoesDeItensDoViewPager;

public TabsPagerAdapter(FragmentManager fm, ConfiguracoesDeItensDoViewPager [] configuracoesDeItensDoViewPager) {
super(fm);
this.mFragmentManager = fm;
this.mConfiguracoesDeItensDoViewPager = configuracoesDeItensDoViewPager;
}

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

@Override
public Fragment getItem(int indice) {

Fragment fragmento = null;

try {
fragmento = mConfiguracoesDeItensDoViewPager[indice].getClasseDoFragmento().newInstance();
if (mConfiguracoesDeItensDoViewPager[indice].getArgumentosDoFragmento() != null) {
fragmento.setArguments(mConfiguracoesDeItensDoViewPager[indice].getArgumentosDoFragmento());
}
} catch (IllegalAccessException e) {
Log.e(Logs.gerarTagParaFiltragemNoLogCat(AtividadePrincipal.this, this), "Exceção inesperada!", e);
} catch (InstantiationException e) {
Log.e(Logs.gerarTagParaFiltragemNoLogCat(AtividadePrincipal.this, this), "Exceção inesperada!", e);
}
return fragmento;
}

@Override
public int getCount() {
return mConfiguracoesDeItensDoViewPager.length;
}

@Override
public CharSequence getPageTitle(int posicao) {
return mConfiguracoesDeItensDoViewPager[posicao].getNomeDaAba();
}

public void setConfiguracoesParaInicializarViewPager(ConfiguracoesDeItemDoViewPager [] configuracoes) {
// Remove um fragmento extra contido dentro do FragmentoB, que se for
// deixado no FragmentManager causa erro de id duplicado
FragmentTransaction ft = mFragmentManager.beginTransaction();
Fragment fragmento = mFragmentManager.findFragmentById(R.id.mapa);
if (fragmento != null && fragmento instanceof SupportMapFragment) {
ft.remove(fragmento);
}
ft.commit();

this.mConfiguracoesDeItensDoViewPager = configuracoes;

notifyDataSetChanged();
}
}
}

只是为了完成解释:当用户单击其中一个 fragment 中的按钮时,方法 AtividadePrincipal.exibirViewPager2() 被调用。它应该更新 ViewPager View /fragment ,但这并没有发生。

最佳答案

我已经试过了,它会重新创建/重新加载 fragment (调用 onCreate)。在您的 AtividadePrincipal.exibirViewPager2() 方法中添加这一行。

    this.mViewPager.setAdapter(mTabsPagerAdapter);

关于java - 无法交换 ViewPager 中显示的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059852/

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