I have ViewPager with 200+ tabs. Every tab includes recyclerview with dynamically loading data via retrofit2. Data are loading after every onTabSelected()
action.
But because of viewpager loading position-1 (if any), position, position+1 (if any)
I am getting 2-3 tabs with identical data.
The question is - how can I load only one fragment that is currently selected without neighbor positions?
I've thought that BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
in my FragmentPagerAdapter
will solve my issue by simply passing it to the constructor:
public Adapter_programmes(FragmentManager fm, Context context) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
mContext = context;
fragment_some = new Fragment_Some();
}
but nothing has changed.
UPD: I think I've found solution here: ViewPager offscreen page limit
But I can not understand it: load your grid with placeholder images, and do not load the real images until the page is changed.
**UPD2: ** setOffscreenPageLimit(1) did not solving my issue.
My code:
Fragment:
public static Fragment_some newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
Fragment_some fragment = new Fragment_some();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPage = getArguments().getInt(ARG_PAGE);
}
arrayNames.clear();
arrayTimes.clear();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mContext=context;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "");
//creating fragment
rootView = inflater.inflate(R.layout.content_list, container, false);
loadProgrammes(CID); //doing retrofit query for data, using argument which I've got from onTabSelected by SharedPreferences
//data loaded succecfully
Handler handler = new Handler();
handler.postDelayed(() -> {
adapterItem = new Adapter_programme_item(mContext, arrayTimes, arrayNames);
RecyclerView RV = rootView.findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapterItem);
}, 3000); //3 seconds waiting
return rootView;
}
FragmentPagerAdapter:
public Adapter_programmes(FragmentManager fm, Context context) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
mContext = context;
fragment_some = new Fragment_some();
}
@Override public int getCount() {
return PAGE_COUNT;
}
@Override public Fragment getItem(int position) {
return fragment_some.newInstance(position + 1);
}
@Override public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
public void updateTitleData(String[] titles) {
...
notifyDataSetChanged();
}
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new Adapter_programmes(getSupportFragmentManager(), MainActivity.this);
ViewPager viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
loaderFromURL(); //getting tab titles via retrofit
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d(TAG, "onTabSelected");
String mCID = listm3u.get(tab.getPosition()).getItemCID();
AppPreferences.setCurrentCID(MainActivity.this, mCID);
String iconURL = listm3u.get(tab.getPosition()).getItemIcon();
ImageView IVChanelLogo = findViewById(R.id.IVChanelLogo);
Picasso.with(MainActivity.this).load(iconURL).into(IVChanelLogo);
}
});
}