在片段之间切换时回收消失

时间:2021-06-26 08:48:22

标签: android android-fragments

我在 DashboardActvity 中有四个片段。我在 HomeFragment 中有回收视图,它是首先加载的默认页面。回收视图第一次出现,但是当我切换到另一个片段并再次回到 HomeFragment 时它消失了。 这是我的 DashbaordActivity。

public class DashboardActivity extends AppCompatActivity {
    // views
    private BottomNavigationView bottomNav;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        bottomNav = findViewById(R.id.bottom_nav);

        bottomNav.setOnNavigationItemSelectedListener(bottomNavMethod);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new HomeFragment()).commit() ;

    }

    // click event listener
    private BottomNavigationView.OnNavigationItemSelectedListener bottomNavMethod =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment fragment =null;
                    switch (item.getItemId()) {
                        case R.id.home:
                            fragment = new HomeFragment();
                            break;
                        case R.id.chat:
                            fragment = new ChatFragment();
                            break;
                        case R.id.map:
                            fragment = new MapsFragment();
                            break;
                        case R.id.users:
                            fragment = new UserFragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
                    return true;
                }
            };
}

这是我的 HomeFragment

public class HomeFragment extends Fragment {
    // views
    private View homeView;
    private RecyclerView childList;
    private Adapter adapter;

    // variables
    private IAPI api;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private SharedPreferences preferences;
    private List<Child> cList = new ArrayList<>();
    private Integer image = R.drawable.avatar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        homeView = inflater.inflate(R.layout.fragment_home, container, false);
        preferences = getActivity().getSharedPreferences(Constatnt.MY_PREFERENCE, Context.MODE_PRIVATE);
        api = RetrofitClient.getInstance().create(IAPI.class);
        childList = homeView.findViewById(R.id.child_rec_view);

        adapter = new Adapter(getContext(), cList, image);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false);
        childList.setLayoutManager(gridLayoutManager);
        childList.setAdapter(adapter);

        return homeView;
    }

    private void getChildren() {
        int parentId = preferences.getInt("Id", 0);

        compositeDisposable.add(api.getChildren(parentId)
        .subscribeOn(Schedulers.io())
        .observeOn(Schedulers.io())
        .subscribe(new Consumer<List<Child>>() {
            @Override
            public void accept(List<Child> children) throws Exception {
                for (Child c:children) {
                    cList.add(c);
                }
            }
        }));
    }

    @Override
    public void onStart() {
        super.onStart();
        getChildren();
    }
}

这是什么问题以及我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

覆盖

<块引用>

onViewCreated()

并在此方法中添加 set 适配器

    childList = view.findViewById(R.id.child_rec_view);
 adapter = new Adapter(getContext(), cList, image);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false);
    childList.setLayoutManager(gridLayoutManager);
    childList.setAdapter(adapter);