按钮点击切换页面

public class FirstActivity extends AppCompatActivity {
private ViewPager Home_Frag;
private RadioGroup Home_Gp;
private FragmentManager mFragmentManager;
private List mFragmentList = new ArrayList<>();
private Fragment curFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    //注册
    EventBus.getDefault().register(this);
    initView();

}

private void initView() {

    Home_Frag = (ViewPager) findViewById(R.id.Home_Frag);
    Home_Gp = (RadioGroup) findViewById(R.id.Home_Gp);
    //缓存
    Home_Frag.setOffscreenPageLimit(4);
    //跳到fragment视图管理
    mFragmentManager = getSupportFragmentManager();
    //创建新的fragment
    HomeFragment homeFragment=new HomeFragment();
    CircleFragment circleFragment=new CircleFragment();
    ShoppingFragment shoppingFragment=new ShoppingFragment();
    IndentFragment indentFragment=new IndentFragment();
    MyFragment myFragment=new MyFragment();
    //放在一个集合里
    final ArrayList list=new ArrayList<>();
    list.add(homeFragment);
    list.add(circleFragment);
    list.add(shoppingFragment);
    list.add(indentFragment);
    list.add(myFragment);
    //pager的适配
    Home_Frag.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    });
    //paper的点击滑动
    Home_Frag.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Home_Gp.check(Home_Gp.getChildAt(position).getId());
            clearAll(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    //radio点击跟随
    Home_Gp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rd1:
                    Home_Frag.setCurrentItem(0);
                    break;
                case R.id.rd2:
                    Home_Frag.setCurrentItem(1);
                    break;
                case R.id.rd3:
                    Home_Frag.setCurrentItem(2);
                    break;
                case R.id.rd4:
                    Home_Frag.setCurrentItem(3);
                    break;
                case R.id.rd5:
                    Home_Frag.setCurrentItem(4);
                    break;
            }
        }
    });
}

@Subscribe
public void eventAdd(AddEvent event){
    if(Home_Frag.getVisibility() != View.GONE){
        Home_Frag.setVisibility(View.GONE);
    }
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    if(curFragment != null){
        transaction.hide(curFragment);
    }
    curFragment = event.getNeedAdd();
    mFragmentList.add(curFragment);
    transaction.add(R.id.main_frameLayout,curFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
@Subscribe
public void eventDismiss(DismissEvent event){
    Fragment needDismiss = event.getNeedDismiss();
    mFragmentList.remove(needDismiss);
    curFragment = null;
    if(mFragmentList.size() > 0){
        curFragment = mFragmentList.get(mFragmentList.size()-1);
    }else if(Home_Frag.getVisibility() != View.VISIBLE){
        Home_Frag.setVisibility(View.VISIBLE);
    }
    mFragmentManager.popBackStack();
}
private void clearAll(int position){
    for (int i = 0; i < mFragmentList.size(); i++) {
        mFragmentManager.popBackStack();
    }
    curFragment = null;
    mFragmentList.clear();
    Home_Frag.setVisibility(View.VISIBLE);
    Home_Frag.setCurrentItem(position);
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    if(mFragmentList.size() != 0 && Home_Frag.getVisibility() != View.VISIBLE){
        Home_Frag.setVisibility(View.VISIBLE);
    }
}
//销毁
@Override
protected void onDestroy() {
    super.onDestroy();
    //解除绑定
    EventBus.getDefault().unregister(this);
}

}

你可能感兴趣的:(按钮点击切换页面)