1

actionbarsherlock TabsNavigation (3 つのタブ) を使用したアクティビティがあり、タブを押すと、対応するフラグメントのビューページ アダプターが変更されます。最初に作成されたタブ以外の別のタブをクリックすると、最初のページは常にその最初のタブのページになります。アダプターを変更する前に、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 に必要なアダプターは 1 つだけです。

// 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);

すべてが初期化されたら、これがタブとページャー ビューの作成方法です (この 2 つは関連しています)。また、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 に答える