【问题标题】:Why does content disappear after switching between fragments in navigation drawer?为什么在导航抽屉的片段之间切换后内容会消失?
【发布时间】:2019-03-09 09:25:28
【问题描述】:

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

这是

ma​​inactivity.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;
    }

ImagesFragment.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 

}
}

所以我的问题是每当我在个人资料和图像片段之间切换时,图像中的内容就会消失。

【问题讨论】:

  • 我设法解决了这个问题,我将部件 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())); 和 setadapter 放入 GetBook 方法/函数中

标签: android android-fragments android-recyclerview navigation-drawer


【解决方案1】:

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

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多