android-fragments 如何处理底部导航栏的碎片?

hsvhsicv  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(249)

我有四个片段连接在我的底部导航栏。现在我试图从一个片段点击一个cardview到任何其他片段。这是完美的工作,但当我将该片段到任何其他片段(这是连接在导航栏),并按下返回按钮奇怪的重叠发生。这里是我的导航栏和片段容器活动:

显示片段.java

package com.fahim69.bazaarapp.FragmentHolder;
public class ShowFragment extends AppCompatActivity {
BottomNavigationView bottomnav;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_fragment);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    bottomnav = findViewById(R.id.nav_bar);
    replacefragment(new HomeFragment());
    bottomnav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.nav_home:
                    replacefragment(new HomeFragment());
                    break;
                case R.id.nav_category:
                    replacefragment(new CategoryFragment());
                    break;
                case R.id.nav_cart:
                    replacefragment(new CartFragment());
                    break;
                case R.id.nav_profile:
                    replacefragment(new ProfileFragment());
                    break;
            }

            return true;
        }
    });
}

private void replacefragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    //fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

上)
这里是我的片段代码,从那里我去不同的片段,这是不附加到导航栏。

package com.fahim69.bazaarapp.Fragment;

public class CategoryFragment extends Fragment {

CardView fruit, fish, meat, vegetable, cooking, dairy, frozen, snacks, bread, beverage, 
personal_care, hygiene, baby_care, homekitchen, petcare;
ImageView fruit_img, fish_img, meat_img, vegetable_img, cooking_img, dairy_img, frozen_img, 
snacks_img, bread_img, beverage_img, personal_img, hygiene_img, baby_img, home_img, pet_img;
TextView fruit_tv, fish_tv, meat_tv, vegetable_tv, cooking_tv, dairy_tv, frozen_tv, snacks_tv, 
bread_tv, beverage_tv, personal_tv, hygiene_tv, baby_tv, home_tv, pet_tv;

public CategoryFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_category, container, false);
    fruit = view.findViewById(R.id.fruitname);
    fish = view.findViewById(R.id.Fish);
    meat = view.findViewById(R.id.meat);
    vegetable = view.findViewById(R.id.vegetable);
    cooking = view.findViewById(R.id.cooking);
    dairy = view.findViewById(R.id.dairy);
    frozen = view.findViewById(R.id.frozenAndCanned);
    snacks = view.findViewById(R.id.Snacks);
    bread = view.findViewById(R.id.breadandbakery);
    beverage = view.findViewById(R.id.beverages);
    personal_care = view.findViewById(R.id.Personalcare);
    hygiene = view.findViewById(R.id.Hygiene);
    baby_care = view.findViewById(R.id.BabyCare);
    homekitchen = view.findViewById(R.id.HomeandKitchen);
    petcare = view.findViewById(R.id.Petcare);

    fruit_img = view.findViewById(R.id.fruitpic);
    fish_img = view.findViewById(R.id.fish_category_img);
    meat_img = view.findViewById(R.id.meatpic);
    vegetable_img = view.findViewById(R.id.vegetablepic);
    cooking_img = view.findViewById(R.id.cookingpic);
    dairy_img = view.findViewById(R.id.dairypic);
    frozen_img = view.findViewById(R.id.frozenandcannedimg);
    snacks_img = view.findViewById(R.id.snackscatimg);
    bread_img = view.findViewById(R.id.breadandbakeryimg);
    beverage_img = view.findViewById(R.id.beveragesimg);
    personal_img = view.findViewById(R.id.personalcare_img);
    hygiene_img = view.findViewById(R.id.hygiene_img);
    baby_img = view.findViewById(R.id.babycare_img);
    home_img = view.findViewById(R.id.homeandkitchen_img);
    pet_img = view.findViewById(R.id.petcareimg);

    fruit_tv = view.findViewById(R.id.fruit_name_tv);
    fish_tv = view.findViewById(R.id.fish_category_tv);
    meat_tv = view.findViewById(R.id.meat_cat_tv);
    vegetable_tv = view.findViewById(R.id.vegetable_cat_tv);
    cooking_tv = view.findViewById(R.id.cooking_cat_tv);
    dairy_tv = view.findViewById(R.id.dairy_cat_tv);
    frozen_tv = view.findViewById(R.id.frozen_cat_tv);
    snacks_tv = view.findViewById(R.id.snacks_tv);
    bread_tv = view.findViewById(R.id.breadbackery_tv);
    beverage_tv = view.findViewById(R.id.Beverage_tv);
    personal_tv = view.findViewById(R.id.personal_care_tv);
    hygiene_tv = view.findViewById(R.id.hygiene_tv);
    baby_tv = view.findViewById(R.id.babycare_tv);
    home_tv = view.findViewById(R.id.homeandkitchen_tv);
    pet_tv = view.findViewById(R.id.petcare_tv);
    BottomNavigationView item = getActivity().findViewById(R.id.nav_bar);


    fruit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = new FruitCategory();
            FragmentManager manager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fragment_container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();

        }
    });

    return view;
}

上)

我试了很多东西,但问题正在解决。帮帮我。

xghobddn

xghobddn1#

这是因为您没有正确处理背面堆栈。

您当前的片段流:

  1. boot()-〉未连接碎片
  2. onCreate()-〉添加HomeFragment(返回堆栈中没有任何内容)。
  3. onTabClick()-〉将当前片段替换为CategoryFragment(后栈中没有任何内容)
  4. openFruitDetails()-〉用FruitCategory替换当前片段并将当前片段添加到后栈(现在CategoryFragment在后栈中)
  5. onTabClick()-〉将当前片段替换为ProfileFragmentCategoryFragment在后栈中)
  6. onBackClick()-〉将CategoryFragment从后端堆栈中弹出。
    "你能用它做什么"
    1.坏的一个-〉不要添加到回栈任何你的碎片。
    1.好的一个-〉添加导航库jetpack navigationciceronemodo等。
    1.困难的一点-〉创建您自己的片段管理器 Package 器实现来解决这种行为。

相关问题