onPageSelected doesn't work for first page

前端 未结 10 1992
[愿得一人]
[愿得一人] 2021-01-30 12:30

My pager adapter ( extends PagerAdepter ) has a textview in it. And I update this textview with MainActivity\'s onPageSelected . Its update textview for position > 0 , but start

相关标签:
10条回答
  • 2021-01-30 13:19

    My solution was to extend pager adapter and create an interface inside it. Then make the adapter call the interface only once after creating the adapter. Inside interface callback you can call onPageChanged method without having nullpointerexception. Add this class to your project and extend your adapter from it. Dont forget to set listener to adapter before setting adapter to viewpager. Adapter class below:

    public abstract class ExtendedPagerAdapter extends FragmentPagerAdapter {
    
    private boolean instantiated;
    private AdapterListener adapterListener;
    
    public interface AdapterListener {
        void onAdapterInstantiated();
    }
    
    public ExtendedPagerAdapter(FragmentManager fragmentManager) {
        this(fragmentManager, null);
    }
    
    public ExtendedPagerAdapter(FragmentManager fragmentManager, AdapterListener adapterListener) {
        super(fragmentManager);
        this.adapterListener = adapterListener;
        instantiated = false;
    }
    
    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
        if (!instantiated) {
            instantiated = true;
            if (adapterListener != null) {
                adapterListener.onAdapterInstantiated();
            }
        }
    }
    
    public void setAdapterInstantiatedListener(AdapterListener adapterListener) {
        this.adapterListener = adapterListener;
    }
    }
    
    0 讨论(0)
  • 2021-01-30 13:19

    I fixed the issue in my app by adding the code that should be executed for the first page in the onCreate()-method of my activity.

    Its not the prettiest hack and involves some redundant code, but as I only want to load some text data when the pages are changing it is the easiest way to do it in my case.

    Calling onPageSelected() manually caused NullPointerExceptions, just das described in other comments above.

    0 讨论(0)
  • 2021-01-30 13:24
    @Override
    public void addOnPageChangeListener(@NonNull OnPageChangeListener listener) {
        super.addOnPageChangeListener(listener);
        if (getAdapter() != null && getAdapter().getCount() > 0) {
            post(() -> listener.onPageSelected(getCurrentItem()));
        }
    }
    
    0 讨论(0)
  • 2021-01-30 13:30

    You probably use some AsyncTasks to load the dataset and it is hard to determine which task will be the last to finish. And only after that last task, when the whole dataset is complete you could call OnPageSelected, because it is doing a flush on the UI over the whole dataset.

    Instead of calling the OnPageSelected-function manually when the page is loaded the first time, rather build up the UI step by step the "normal"-way. Later on, when the user changes the page, the dataset should be complete and it is save to let the update been made in the OnPageSelected-function.

    0 讨论(0)
提交回复
热议问题