我在选项卡栏中将4个片段活动附加到主要活动上。frag 1
,frag 2
,frag 3
,frag 4
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
assert toolbar != null;
toolbar.setLogo(R.drawable.toplogo);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText("Videos"));
}
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText("Music"));
}
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText("MindMaps"));
}
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText("Quiz"));
}
/* if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText("Discussion"));
}*/
if (tabLayout != null) {
tabLayout.setTabTextColors(parseColor("#ffffff"), parseColor("#f1bf8f"));
}
mViewPager = (ViewPager) findViewById(R.id.container);
if (mViewPager != null) {
mViewPager.setAdapter(mSectionsPagerAdapter);
}
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
if (tabLayout != null) {
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Videos();
case 1:
return new Music();
case 2:
return new MindMaps();
case 3:
return new Quiz();
/* case 4:
return new Discussion();*/
}
return null;
}
@Override
public int getCount() {
return 4;
}
就像下面的链接一样。
图片:
每个都包含一个列表视图。这些片段从API获取数据,并显示在列表视图中。如果列表视图在片段1中为空,如何动态移动到另一个具有非空列表的片段。
让我知道是否需要任何澄清。
Answer : ((FragmentActivity)getActivity()).mViewPager.setCurrentItem(2);
当我从API获取JSON格式的数据时
因此,在“后执行”方法中,我检查了我的清单。
@Override
protected void onPostExecute(Void args) {
if (myList.isEmpty()) {
moveForward();
}
else {
//do something
}
private void moveForward() {
((FragmentActivity)getActivity()).mViewPager.setCurrentItem(1);
//changed my position from 0 to 1
//mViewPager will be package local.
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句