且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅在片段可见时调用 onCreateView?

更新时间:2023-12-02 23:14:34

首先,我不会使用全局列表.如果您需要保存类似类型的列表,请创建一个 baseFragment.如果您的目标是在片段之间共享过滤,那么只需将应用的过滤器传递给每个片段,以便它知道如何在加载或传递列表时管理其列表,但不要使用只会自找麻烦的全局.

First, I would not use a global list. Make a baseFragment if you need to hold a similar type of list. If your goal is to share filtering among fragments, then simply pass the applied filters to each fragment so it knows how to manage its list as it loads or pass the list, but don't use a global that's just asking for trouble.

其次,onCreate 是故意在寻呼机上调用的,因此您可以在它显示在屏幕上之前预加载一些内容.这是为了在用户左右滑动时提高渲染性能.如果你在滑动发生时加载,它会很糟糕而且很糟糕.

Second, onCreate is called on the pager on purpose so you can preload some things before it shows on the screen. It's to improve rendering performance as the user swipes from side to side. If you load as the swipe happens, it will be jumpy and bad.

最后考虑将您的逻辑移至 onResume,除非您有充分的理由不这样做.如果这对您不起作用,则监视页面更改事件并调用您在每个 baseFragment 上创建的共享加载"方法,您可以在那里简单地调用和处理您的代码.

Lastly consider moving your logic to onResume, unless you have good reason not to. If that doesn't work for you, then monitor the page changing event and call a shared "load" method that you create on each baseFragment that you can simply call and handle your code there.