我想知道是否有人可以帮助我。我需要在运行时更改单个选项卡中的片段。我知道它应该使用FragmentPagerAdapter来完成,但是我已经设法找到了它。我正在使用android studio。我的项目在API 22上运行。感谢所有答案! 编辑:这是代码。
这里我设置了选项卡式布局类
Stack<Fragment> Tab1;
Stack<Fragment> Tab2;
Stack<Fragment> Tab3;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_root);
/*Tab1 = new Stack<Fragment>();
Tab1.addElement(new Tab1Informations());
Tab2 = new Stack<Fragment>();
Tab2.addElement(new Tab2Events());
Tab3 = new Stack<Fragment>();
Tab3.addElement(new tab3tests());*/
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
//mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), Tab1.peek(), Tab2.peek(), Tab3.peek());
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),new Tab1Informations(), new Tab2Events(), new tab3tests());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
CreateButtonListener();
}
这里我尝试更改SectionChangedAdapter
Stack<Fragment> Tab1;
Stack<Fragment> Tab2;
Stack<Fragment> Tab3;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_root);
/*Tab1 = new Stack<Fragment>();
Tab1.addElement(new Tab1Informations());
Tab2 = new Stack<Fragment>();
Tab2.addElement(new Tab2Events());
Tab3 = new Stack<Fragment>();
Tab3.addElement(new tab3tests());*/
//mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager(), Tab1.peek(),
Tab2.peek(), Tab3.peek());
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager(),new Tab1Informations(),
new Tab2Events(), new tab3tests());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
CreateButtonListener();
}
在这里我定义了适配器本身。 公共类SectionsPagerAdapter扩展 FragmentPagerAdapter { 片段tab1; 片段tab2; 片段tab3;
public SectionsPagerAdapter(FragmentManager fm, Fragment tab1, Fragment
tab2, Fragment tab3){
super(fm);
this.tab1 = tab1;
this.tab2 = tab2;
this.tab3 = tab3;
}
@Override
public Fragment getItem(int position){
switch(position){
case 0:
return tab1;
case 1:
return tab2;
case 2:
return tab3;
default:
return null;
}
}
@Override
public int getCount(){
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position){
switch(position){
case 0:
return getString(R.string.InformationsTabTitle);
case 1:
return getString(R.string.EventsTabTitle);
case 2:
return getString(R.string.TestsTabTitle);
default:
return null;
}
}
}`
答案 0 :(得分:0)
如果不使用视图寻呼机
//..............
tabLayoutMain = (TabLayout) findViewById(R.id.tabLayoutMain);
tabLayoutMain.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
selectTab(tab);
}
private void selectTab(TabLayout.Tab tab) {
// do something
tabText = "" + tab.getText();
handelTabsSelection(tabText);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
selectTab(tab);
tabLayoutMain.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
selectTab(tab);
}
@Override
public void onTabReselected(TabLayout.Tab arg0) {
}
@Override
public void onTabUnselected(TabLayout.Tab arg0) {
}
});
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
});
和
private void handelTabsSelection(String tabText) {
switch (tabText) {
case "news":
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.placeholder, new NewsFragment());
fragmentTransaction.commit();
break;
default:
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.placeholder, new HomeFragment());
fragmentTransaction.commit();
}
}