Android架構之Activity和Fragment復雜嵌套技巧

Android架構之Activity和Fragment復雜嵌套技巧。

像這種界面,需要在Activity裡嵌套Fragment,Fragment裡嵌套Fragment,此時,往往需要對Fragment做好棧的管理。

1.Activity中放置多個Fragment,而且能左右滑動切換

     list_fragment.add(ActiveAreaFragment.create(list.get(0).getId()));
                        list_fragment.add(WanShuiFragment.create(list.get(1).getId()));
                        list_fragment.add(BaoShuiFragment.create(list.get(2).getId()));
                        list_fragment.add(HaiWaiFragment.create(list.get(3).getId()));
                        list_fragment.add(QuanqiuFragment.create(list.get(4).getId()));
  tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getContext(), getSurportFragmentManager(),
                list_fragment, list_title, viewPager, (TabLayout) view.findViewById(R.id.tablayout)) {
        };
        viewPager.setAdapter(tabFragmentPagerAdapter);

2.Activity中放置多個Fragment,不能能左右滑動切換:

 switch (v.getId()) {
            case R.id.layout_home:
                replaceFragment(R.id.framelayout, createFragment(FRAGMENT_HOME));
                resetTab();
                tvs[0].setTextColor(getResources().getColor(R.color.red));
                ivs[0].setImageResource(R.drawable.home_selected);

                break;
            case R.id.layout_dianpu:
                replaceFragment(R.id.framelayout, createFragment(FRAGMENT_DINAPU));
                resetTab();
                tvs[1].setTextColor(getResources().getColor(R.color.red));
                ivs[1].setImageResource(R.drawable.dianpu_selected);
                break;
            case R.id.layout_fenlei:

                replaceFragment(R.id.framelayout, createFragment(FRAGMENT_FENLEI));
                resetTab();
                tvs[2].setTextColor(getResources().getColor(R.color.red));
                ivs[2].setImageResource(R.drawable.fenlei_selected);
                break;
            case R.id.layout_gouwuche:

                replaceFragment(R.id.framelayout, createFragment(FRAGMENT_GOUWUCHE));
                resetTab();
                tvs[3].setTextColor(getResources().getColor(R.color.red));
                ivs[3].setImageResource(R.drawable.gouwuche_selected);
                break;
            case R.id.layout_my:

                replaceFragment(R.id.framelayout, createFragment(FRAGMENT_MY));
                resetTab();
                tvs[4].setTextColor(getResources().getColor(R.color.red));
                ivs[4].setImageResource(R.drawable.my_selected);
                break;
        }
@Override
    public Fragment createFragment(int position) {
        Fragment fragment = map_fragment.get(position);  //在集合中取出來Fragment
        //如果在集合中沒有取出來,需要重新創建
        if (fragment == null) {

            switch (position) {
                case FRAGMENT_MY_UNLOGIN:
                    fragment = new UnLoginMyFragment();

                    break;
                case FRAGMENT_MY_LOGINED:
                    fragment = new LoginedMyFragment();

                    break;
                case FRAGMENT_HOME:
                    fragment = new HomeFragment();

                    break;
                case FRAGMENT_DINAPU:
                    fragment = new DianpuFragment();

                    break;
                case FRAGMENT_FENLEI:
                    fragment = new FenleiFragment();

                    break;
                case FRAGMENT_GOUWUCHE:
                    fragment = new CartFragment();

                    break;
                case FRAGMENT_MY:
                    fragment = new MyFragment();

                    break;
            }
            map_fragment.put(position, fragment);

        }
        return fragment;
    }

3.Fragment中 放置多個Fragment,並且能左右滑動:

 list_fragment.add(ActiveAreaFragment.create(list.get(0).getId()));
                        list_fragment.add(WanShuiFragment.create(list.get(1).getId()));
                        list_fragment.add(BaoShuiFragment.create(list.get(2).getId()));
                        list_fragment.add(HaiWaiFragment.create(list.get(3).getId()));
                        list_fragment.add(QuanqiuFragment.create(list.get(4).getId()));
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getContext(), getChildFragmentManager(),
                list_fragment, list_title, viewPager, (TabLayout) view.findViewById(R.id.tablayout)) {
        };
        viewPager.setAdapter(tabFragmentPagerAdapter);

4.Fragment中放置多個Fragment,由棧管理跳轉和返回:

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        replaceFragment(R.id.framelayout,new  LoginRegisterFragment());


    }


}
public void replaceFragment(int framelayout_id, Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(framelayout_id, fragment).commitAllowingStateLoss();
    }

Fragment跳轉Fragment:

startFragment(R.id.framelayout, new FindPwdFragment());
   public void startFragment(int framelayout_id, Fragment fragment) {
        FragmentTransaction fragmentTransaction = myActivity.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.left_out, R.anim.left_in, R.anim.right_out);
        fragmentTransaction.replace(framelayout_id, fragment).addToBackStack(null).commit();

    }

啟動瞭多個Fragment後,需要根據棧返回:

 public void popupFragment() {

        if (myActivity.getSupportFragmentManager().getBackStackEntryCount() == 0) {
            finishActivity();
            return;
        }
        myActivity.getSupportFragmentManager().popBackStack();

    }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *