我已按照此链接实施了tablayout Android Material Design working with Tabs
现在我做了什么,我使用片段而不是Activity for tablayout。
以下是代码: -
public class HopitalTabLayoutFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
public HopitalTabLayoutFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hopital_tab_layout, container, false);
viewPager = (ViewPager)view. findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)view. findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
并从Activity中添加此片段: -
public class HomeActivityDrawer extends AppCompatActivity {
private Toolbar toolbar;
private NavigationView navigation_view_left, navigation_view_right;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
header = (RelativeLayout) findViewById(R.id.header);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fragment = new HopitalTabLayoutFragment();
}
}
这是我在标签布局中使用的三个片段: -
public class TwoFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("DONE");
return view;
}
}
同样是一和三。
现在的问题是,第一次加载所有三个片段并运行onCreateView
方法但是当我再次打开tablayout
时,只有片段一启动而不是第二个。这段代码出了什么问题?请帮忙!!
答案 0 :(得分:14)
导入FragmentStatePagerAdapter而不是FragmentPagerAdapter。
import android.support.v4.app.FragmentPagerAdapter;
到
import android.support.v4.app.FragmentStatePagerAdapter;
然后从FragmentStatePagerAdapter扩展:
class ViewPagerAdapter extends FragmentPagerAdapter
到
class ViewPagerAdapter extends FragmentStatePagerAdapter