2012-08-09 3 views
1

저는 actionbarsherlock TabsNavigation (3 개의 탭)이있는 활동이 있으며 탭을 누르면 해당 프래그먼트의 뷰 페이지 어댑터가 변경됩니다. 그것은 잘 작동하고, 것은 첫 번째 페이지 이외의 다른 탭을 클릭하면 첫 번째 페이지는 항상 그 첫 번째 탭 중 하나입니다. 어댑터를 변경하기 전에 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

시도 : @Override 공공 INT의 getItemPosition (Object 객체) { 반환 PagerAdapter.POSITION_NONE; } – sherpya

답변

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

모든 것이 초기화 될 때,이 탭과 호출기를 구축하는 방법입니다 views (두 가지가 관련되어 있습니다). 또한 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) 메서드를 전달하여 현재보기를 자동으로 변경합니다. 기본적으로 올바른 유형의 조각을 얻으려면 탭 위치와 태그를 조정해야합니다.

자세한 내용은 언제든지 문의하십시오.

관련 문제