我正在使用fragments
和ViewPager2
显示5 FragmentStateAdapter
。每个fragment
具有ExoPlayer
实例。我正在维护ViewPager2.setOffscreenPageLimit(1)
,并且可以很好地加载fragments
。但是,当我向下滚动ViewPager2
到pos4
时,pos2
和pos1
应该调用onStop()
,onDestroyView()
,onDestroy()
。但这没有发生。
除非我按onBackPress()
,否则这些方法均未执行。 FragmentStateAdapter
的实现部分是否缺少任何内容。
我想在exoPlayer.release()
上执行onDestroyView()
以释放应用程序的内存。
这里是代码
活动
List<Post> postsList = response.getPosts();
ViewPagerAdapter adapter = new ViewPagerAdapter(this, postsList);
binding.pager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
binding.pager.setOffscreenPageLimit(1);
binding.pager.setAdapter(adapter);
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentStateAdapter {
private List<Post> postList;
public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity, List<Post> postList) {
super(fragmentActivity);
this.postList = postList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return new PostFragment(postList.get(position), position);
}
@Override
public int getItemCount() {
if (postList == null) return 0;
return postList.size();
}
}
更新
我已经介绍过此POST用于解决方案。
[VeiwPager2
建立在RecyclerView
组件上,并且FragmentStateAdapter
代替FragmentStatePagerAdapter
。
假设offscreenPageLimit = 1
,ViewPager2
将具有3 fragments
和2 fragments
预先加载/缓存的RecylerView Preloading Strategies,因此,总共有5 fragments
已加载。
要禁用预加载和缓存,我执行了:
private void disableCacheOfViewPager() {
try {
// Disable Preloading
((RecyclerView) binding.pager.getChildAt(0)).getLayoutManager().setItemPrefetchEnabled(false);
// Disable Cache
((RecyclerView) binding.pager.getChildAt(0)).setItemViewCacheSize(0);
} catch (NullPointerException e) {
Log.i(TAG, "disableCacheOfViewPager: " + e.getMessage());
}
}
之前滚动日志
//1st
2020-03-02 18:05:15.255 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 0
2020-03-02 18:05:15.256 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 0
2020-03-02 18:05:15.375 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 1
//2nd
2020-03-02 18:09:16.746 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 2
2020-03-02 18:09:16.900 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 0
2020-03-02 18:09:16.904 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 1
//3rd
2020-03-02 18:09:49.759 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 3
2020-03-02 18:09:49.940 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 1
2020-03-02 18:09:49.945 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 2
//4th
2020-03-02 18:10:06.636 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 4
2020-03-02 18:10:06.826 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 2
2020-03-02 18:10:06.830 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 3
//5th
2020-03-02 18:10:17.737 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 5
2020-03-02 18:10:18.070 12541-12541/com.example.rajat.demo I/PostFragment: onStop: 0
2020-03-02 18:10:18.071 12541-12541/com.example.rajat.demo I/PostFragment: onDestroyView: 0
2020-03-02 18:10:18.139 12541-12541/com.example.rajat.demo I/PostFragment: onDestroy: 0
2020-03-02 18:10:18.139 12541-12541/com.example.rajat.demo I/PostFragment: onDetach: 0
2020-03-02 18:10:18.144 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 3
2020-03-02 18:10:18.148 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 4
之后滚动日志
//1st
2020-03-02 18:11:20.439 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 0
2020-03-02 18:11:20.441 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 0
2020-03-02 18:11:20.642 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 1
//2nd
2020-03-02 18:11:43.833 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 2
2020-03-02 18:11:43.948 14463-14463/com.example.rajat.demo I/PostFragment: onPause: 0
2020-03-02 18:11:43.956 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 1
//3rd
2020-03-02 18:11:52.523 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 3
2020-03-02 18:11:52.686 14463-14463/com.example.rajat.demo I/PostFragment: onStop: 0
2020-03-02 18:11:52.686 14463-14463/com.example.rajat.demo I/PostFragment: onDestroyView: 0
2020-03-02 18:11:52.753 14463-14463/com.example.rajat.demo I/PostFragment: onDestroy: 0
2020-03-02 18:11:52.753 14463-14463/com.example.rajat.demo I/PostFragment: onDetach: 0
2020-03-02 18:11:52.758 14463-14463/com.example.rajat.demo I/PostFragment: onPause: 1
2020-03-02 18:11:52.761 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 2