1

我有一个带有 actionbarsherlock TabsNavigation(3 个标签)的活动,当我按下一个标签时,我更改了相应片段的 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);
    }
}
4

1 回答 1

1

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

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

单击选项卡时,相应选项卡的视图会自动更改。

编辑:我必须承认我很难真正理解你的代码和你想要做什么,所以我添加了更多的代码来解释我认为你需要什么。

在我的选择中,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);

一切初始化后,这就是如何构建选项卡和分页器视图的说明(两者是相关的)。另外,不要介意 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);
        }
    }
}

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

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

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

this.mPager.setCurrentItem(position);

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

随时询问更多细节。

于 2012-08-12T23:21:28.550 回答