private FrameLayout frame_layout; private JPTabBar tabbar; private InformationFragment informationFragment; private TopicFragment topicFragment; private MineFragment mineFragment; @Titles private static final String[] mTitles = {"资讯", "话题", "我的"}; @SeleIcons private static final int[] mSeleIcons = {R.mipmap.zixun, R.mipmap.huati, R.mipmap.wode}; @NorIcons private static final int[] mNormalIcons = {R.mipmap.zixun_hui, R.mipmap.huati_hui, R.mipmap.wode_hui}; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_data_load); initView(); initData(); initListener(); } @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); if (fragment instanceof InformationFragment) { informationFragment = (InformationFragment) fragment; } } private void initListener() { tabbar.setTabListener(new OnTabSelectListener() { @Override public void onTabSelect(int index) { FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.hide(informationFragment); transaction.hide(topicFragment); transaction.hide(mineFragment); switch (index) { case 0: if (informationFragment.isAdded()) { transaction.show(informationFragment); } else { transaction.add(R.id.frame_layout, informationFragment); } break; case 1: if (topicFragment.isAdded()) { transaction.show(topicFragment); } else { transaction.add(R.id.frame_layout, topicFragment); } break; case 2: if (mineFragment.isAdded()) { transaction.show(mineFragment); } else { transaction.add(R.id.frame_layout, mineFragment); } break; } transaction.commit(); } @Override public boolean onInterruptSelect(int index) { return false; } }); } private void initData() { informationFragment = new InformationFragment(); topicFragment = new TopicFragment(); mineFragment = new MineFragment(); ArrayList<Fragment> fragmentList = new ArrayList<>(); fragmentList.add(informationFragment); fragmentList.add(topicFragment); fragmentList.add(mineFragment); fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frame_layout, informationFragment).commit(); } private void initView() { frame_layout = (FrameLayout) findViewById(R.id.frame_layout); tabbar = (JPTabBar) findViewById(R.id.tabbar); }
点击切换fragment(不用viewpager)
最新推荐文章于 2025-05-17 16:12:22 发布