gpt4 book ai didi

android - Viewpager 适配器不改变 View

转载 作者:行者123 更新时间:2023-11-30 04:04:48 27 4
gpt4 key购买 nike

我有一个带有 actionbarsherlock TabsNavigation(3 个选项卡)的 Activity ,当我按下一个选项卡时,我更改了相应 fragment 的 viewpager 适配器。它工作正常,问题是当我单击第一个创建的选项卡之外的另一个选项卡时,第一页始终是第一个选项卡中的一个。我试图在更改适配器之前放置 invalidate(),但它不起作用。有人知道吗?这是代码:

public class Tabsteste2 extends SherlockFragmentActivity implements TabListener {


static AdapterOpiniao mOdapter;
static AdapterDados mDdapter;
static AdapterFoto mFdapter;
Bundle extras;
JSONParser jsonParser = new JSONParser();
SharedPreferences mPrefs;

static ViewPager mPager;
static int countopiniao;
static int countdados;
static int countfoto;
JSONArray perguntas = null;
PageIndicator mIndicator;
static ArrayList<HashMap<String, String>> opiniaolist;
static ArrayList<HashMap<String, String>> dadoslist;
static ArrayList<HashMap<String, String>> fotolist;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabsteste2);
opiniaolist = new ArrayList<HashMap<String, String>>();
dadoslist = new ArrayList<HashMap<String, String>>();
fotolist = new ArrayList<HashMap<String, String>>();
mPager = (ViewPager)findViewById(R.id.pager);
extras = getIntent().getExtras();

Boolean opiniaoflag = extras.getBoolean("opiniaoflag");
Boolean dadosflag = extras.getBoolean("dadosflag");
Boolean fotoflag = extras.getBoolean("fotoflag");
countdados= extras.getInt("countdados");
countopiniao=extras.getInt("countopiniao");
countfoto=extras.getInt("countfoto");
mPrefs = getSharedPreferences("mPrefs1",MODE_PRIVATE);
Log.d("countdados",""+countdados);
Log.d("countfoto",""+countfoto);
Log.d("countopiniao",""+countopiniao);

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if(opiniaoflag==true){
ActionBar.Tab opiniaotab = getSupportActionBar().newTab();
opiniaotab.setText("Opinião");
opiniaotab.setTag("op");
opiniaotab.setTabListener(this);
mOdapter = new AdapterOpiniao(getSupportFragmentManager());
Log.d("Opiniao",""+opiniaotab.getTag());
getSupportActionBar().addTab(opiniaotab);
}if(dadosflag == true){
ActionBar.Tab dadostab = getSupportActionBar().newTab();
dadostab.setText("Dados");
dadostab.setTag("dd");

mDdapter = new AdapterDados(getSupportFragmentManager());
dadostab.setTabListener(this);
Log.d("Dados",""+dadostab.getTag());
getSupportActionBar().addTab(dadostab);
}
// mDdapter = new AdapterDados(getSupportFragmentManager());
if(fotoflag==true){
ActionBar.Tab fotostab = getSupportActionBar().newTab();
fotostab.setText("Fotos");
fotostab.setTag("ft");
mFdapter = new AdapterFoto(getSupportFragmentManager());
fotostab.setTabListener(this);
Log.d("Foto",""+fotostab.getTag());
getSupportActionBar().addTab(fotostab);
}


new getpergunta().execute();


}
@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
if(tab.getTag().equals("op")){
mPager.invalidate();
mPager.setAdapter(mOdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);

}else if (tab.getTag().equals("dd")){
mPager.invalidate();
mPager.setAdapter(mDdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}else if(tab.getTag().equals("ft")){
mPager.invalidate();
mPager.setAdapter(mFdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}


@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
public static class AdapterOpiniao extends FragmentPagerAdapter {
public AdapterOpiniao(FragmentManager fm) {
super(fm);
}

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

@Override
public Fragment getItem(int position) {
return FragmentOpinioes.newInstance(position);
}
}
public static class AdapterDados extends FragmentPagerAdapter {
public AdapterDados(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return countdados;
}
@Override
public Fragment getItem(int position) {
return FragmentDados.newInstance(position);
}
}
public static class AdapterFoto extends FragmentPagerAdapter {
public AdapterFoto(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return countfoto;
}
@Override
public Fragment getItem(int position) {
return FragmentFotos.newInstance(position);
}
}

最佳答案

为了解决您的问题,我使用了:

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
this.mPager.setCurrentItem(tab.getPosition());
}

当点击一个选项卡时,相应选项卡的 View 会自动更改。

编辑:我必须承认,我在真正理解您的代码和您尝试做的事情时遇到了一些困难,因此我添加了更多代码来解释我认为您需要的内容。

在我看来,ViewPager 只需要一个适配器,然后如果我是对的,您会这样做:

// I took some personal code for my example
private ViewPager mPager;
private PageIndicator mIndicator;
private TabsExampleSectionsAdapter mAdapter;

// Inside the onCreate method
this.mPager = (ViewPager) findViewById(R.id.pager);
this.mIndicator = new TabPageIndicator(this);
this.mAdapter = new TabsExampleSectionsAdapter(this.getSupportFragmentManager());

this.mPager.setAdapter(this.mAdapter);
this.mIndicator.setViewPager(this.mPager);

当一切都初始化完成后,这就是如何构建标签页和分页 View 的说明(两者是相关的)。另外,不要介意 Section 类,它是一个自定义数据模型对象,其中包含您需要的标记数据,但它与 actionbarsherlock 无关。

private void buildTabs(Section[] sections) {

if (sections != null) {

for (Section section : sections) {

ActionBar.Tab sectionTab = getSupportActionBar().newTab();
sectionTab.setText(section.name);
sectionTab.setTabListener(this);
getSupportActionBar().addTab(sectionTab);

// The tag ("op" or "dd" in your case for example) is contained somewhere in the section object
this.mAdapter.getSections().add(section);
}
}
}

最后,这是 View 寻呼机适配器。它将根据您为每个标签位置定义的标签选择要返回的 fragment 类型:

public class TabsExampleSectionsAdapter extends FragmentPagerAdapter {

private ArrayList<Section> mSectionsList = new ArrayList<Section>();

public TabsExampleSectionsAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

@Override
public Fragment getItem(int position) {

// Retrieving the cooresponding tag of position
Section section = this.mSectionsList.get(position % getCount());

// Here, you check the tag to know what type of fragment you must return
if (section.getTag().equals("dd")) {
return FragmentDados.newInstance(position);
} else if (section.getTag.equals("op")) {
return FragmentOp.newInstance(position);
}
}

@Override
public int getCount() {
return this.mSectionsList.size();
}

@Override
public CharSequence getPageTitle(int position) {
return this.mSectionsList.get(position % getCount()).name.toUpperCase();
}

public ArrayList<Section> getSections() {
return this.mSectionsList;
}
}

总而言之,当一切都设置好后,更改 View 不必通过更改适配器和调用 invalidate() 来手动完成。您可以使用简单的条件从适配器返回不同类型的 fragment 。然后,通过调用:

this.mPager.setCurrentItem(position);

它通过传入适配器的 getItem(position) 方法自动更改当前 View 。基本上,您只需协调制表符位置和标签即可获得正确类型的 fragment 。

欢迎询问更多详情。

关于android - Viewpager 适配器不改变 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888886/

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