我有一个水平的ViewPager2
。在视图寻呼机的第二张幻灯片上,我实现了RecyclerView
无限滚动。我使用EndlessRecyclerViewScrollListener来实现无限滚动。
但是,向下滚动 this 时RecyclerView
,它不会触发无限滚动。永远不会调用无限滚动的第 2 页,只有第 1 页。
这是我的课程ViewPager2
:
public class BaseBottomNavigationViewActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
// The number of pages in the bottom navigation view
private static final int NUM_PAGES = 5;
// The view pager
private ViewPager2 viewPager;
// The pager adapter
private FragmentStateAdapter pagerAdapter;
// The bottom navigation view
private BottomNavigationView bottomNavigationView;
private NewPostBottomSheetDialogFragment newPostModalDialog;
private RelativeLayout newPostButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = findViewById(R.id.view_pager);
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
// The number of screens to keep in memory
viewPager.setOffscreenPageLimit(5);
// Disable swiping in the view pager
viewPager.setUserInputEnabled(false);
// Set the pager adapter
pagerAdapter = new BottomNavigationViewPagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
// Register the item selected listener
bottomNavigationView.setOnNavigationItemSelectedListener(this);
// Set default selection (home)
bottomNavigationView.setSelectedItemId(R.id.action_first);
}
@Override
protected int getLayoutResource() {
return R.layout.activity_base_bottom_navigation_view;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_first:
viewPager.setCurrentItem(0, false);
break;
case R.id.action_second:
viewPager.setCurrentItem(1, false);
break;
case R.id.action_third:
return false;
case R.id.action_fourth:
viewPager.setCurrentItem(3, false);
break;
case R.id.action_fifth:
viewPager.setCurrentItem(4, false);
break;
default:
break;
}
return true;
}
private static class BottomNavigationViewPagerAdapter extends FragmentStateAdapter {
public BottomNavigationViewPagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new FirstPageFragment();
case 1:
return new SecondPageFragment();
case 2:
return new ThirdPageFragment();
case 3:
return new FourthPageFragment();
case 4:
return new FifthPageFragment();
}
return null;
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
}
这是第二页的类ViewPager2
:
public class SecondPageFragment extends Fragment {
private MyAdapter myAdapter;
private LinearLayoutManager layoutManager;
private RecyclerView recyclerView;
private EndlessRecyclerViewScrollListener scrollListener;
private List<Object> objects = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_page_two, parent, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter(getContext(), layoutManager, objects);
recyclerView.setAdapter(myAdapter);
scrollListener = new EndlessRecyclerViewScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
}
private void getData(final int page) {
// Here I call the API to fetch the data and add them to the objects list
}
}
为什么RecyclerView
在这种情况下滚动时不调用第 2 页,我该怎么做才能修复它?
谢谢!