2012-08-09 82 views
1

我有一個activitybarsherlock TabsNavigation(3個選項卡)的活動,當我按下一個選項卡時,我改變相應片段的viewpager適配器。它工作正常,當我點擊除第一個創建的另一個選項卡時,第一個頁面始終是第一個選項卡中的一個。我試圖在更改適配器之前放置invalidate(),但它不起作用。任何人有任何想法?這裏是代碼:Viewpager適配器不改變視圖

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); 
    } 
} 
+0

嘗試: @覆蓋 公衆詮釋getItemPosition(Object對象) { 回報PagerAdapter.POSITION_NONE; } – sherpya 2012-08-13 01:00:58

回答

1

爲了解決你的情況,我用:

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

當選項卡上的點擊,相應的選項卡的視圖自動改變。

編輯︰我必須承認,我有一些困難,真正理解你的代碼和你想做什麼,所以我添加更多的代碼來解釋我認爲你需要什麼。

在我optinion,只有一個適配器是必要的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; 
    } 
} 

總之,當一切都成立,改變的意見並不必須通過改變適配器和調用手動完成無效()。您可以使用簡單條件從適配器返回不同類型的碎片。然後,通過調用:

this.mPager.setCurrentItem(position); 

它通過傳遞適配器的getItem(position)方法自動更改當前視圖。基本上,你只需要協調你的標籤位置和你的標籤,以獲得正確類型的片段。

隨時索取更多詳情。