我已按照此链接实施使用Tabs的 tablayout Android Material Design
现在我做了什么,我使用片段代替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 ListmFragmentList = new ArrayList<>(); private final List 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); } } }
}
并从活动中添加此片段: -
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(); } }
@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_home_drawer);
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; } }
fragment = new HopitalTabLayoutFragment(); }}
这些是我在标签布局中使用的三个片段: -
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 ListmFragmentList = new ArrayList<>(); private final List 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); } } }
同样一和三.
现在的问题是,第一次加载所有三个片段并运行onCreateView方法,但是当我再次打开tablayout时,只有片段One启动而不是第二个片段.这段代码出了什么问题?请帮忙!!
导入FragmentStatePagerAdapter而不是FragmentPagerAdapter.
import android.support.v4.app.FragmentPagerAdapter;
至
import android.support.v4.app.FragmentStatePagerAdapter;
然后从FragmentStatePagerAdapter:扩展
class ViewPagerAdapter extends FragmentPagerAdapter
到
class ViewPagerAdapter extends FragmentStatePagerAdapter