为什么在导航抽屉中的片段之间切换后内容会消失?

问题描述 投票:0回答:1

我正在使用导航抽屉,在我的一个片段中,我创建了一个循环视图。但每当我在片段之间切换时,循环视图的内容就会消失。

这是

main activity.Java

 public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.nav_image:

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ImagesFragment()).commit(); //this contains recycleview

                break;
            case R.id.nav_profile:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ProfileFragment()).commit();
                break;

        drawer.closeDrawer(GravityCompat.START);


        return true;
    }

images fragment.Java

       public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                View rootView =  inflater.inflate(R.layout.fragment_image,container,false);
     rv= (RecyclerView) rootView.findViewById(R.id.book_RV);

            //LAYOUT MANAGER
            rv.setLayoutManager(new LinearLayoutManager(getActivity()));
     GetImage();

    return rootView 

}
}

所以我的问题是每当我在配置文件和图像片段之间切换时,图像内的内容就会消失。

android android-fragments android-recyclerview android-navigation-drawer
1个回答
0
投票

每次创建新片段而不是重用旧片段时。

您可以在类中创建片段作为字段:

ImagesFragment imagesFragment = new ImagesFragment();
ProfileFragment profileFragment = new ProfileFragment();

在你的switch内你可以重复使用它们:

switch (item.getItemId()) {
    case R.id.nav_image:
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container,
                        imagesFragment
                ).commit(); //this contains recycleview

        break;

    case R.id.nav_profile:
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container,
                        profileFragment)
                .commit();
        break;
}

您可以将方法拆分为两个:

  • 首先要做的就是选择片段
  • 第二种方法是做交易。

显示选定的片段(并隐藏其余部分):

public class MainActivity extends AppCompatActivity {

    // Create instance of the fragments
    ImagesFragment imagesFragment = new ImagesFragment();
    ProfileFragment profileFragment = new ProfileFragment();

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        // Select fragment to show
        switch (item.getItemId()) {
            case R.id.nav_image:
                showFragment(imagesFragment);
                break;
            case R.id.nav_profile:
                showFragment(profileFragment);
                break;
        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    void showFragment(Fragment fragmentToShow) {
        // Create transactionns
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

        // Hide all of the fragments
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            transaction.hide(fragment);
        }

        if (fragmentToShow.isAdded()) {
            // When fragment was previously added - show it
            transaction.show(fragmentToShow);
        } else {
            // When fragment is adding first time - add it
            transaction.add(R.id.fragment_container, fragmentToShow);
        }

        transaction.commit();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.