Inform when view is displaying in FragmentStatePagerAdapter Android


Inform when view is displaying in FragmentStatePagerAdapter Android



I'm trying to create slideshow something like Instagram stories.
I write some code and everything is working fine. I'm using Handler.postDelay
to change the slide. I want to prevent changing slide when slide is video type it stop thread until video is loaded after loading video it start again handler thread. Check code for better understanding.


Handler.postDelay



Here is my code.


private class StorySlidePagerAdapter extends FragmentStatePagerAdapter {
public StorySlidePagerAdapter(FragmentManager fm) {
super(fm);
Log.i("StoryActivityDebug", "StorySlidePagerAdapter constructor");
}

@Override
public Fragment getItem(int position) {
Log.i("StorySlideDebug", "getItem position " + position);
StorySlide storySlide = StorySlide.create(position);
storySlide.setOnStoryLoad(StoryActivity.this);
return storySlide;
}


@Override
public int getCount() {
return NUM_STORIES;
}
}



Here is how I change slide.


updateStory = new Runnable() {
@Override
public void run() {

if (killThread)
return;

mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
};
handler.postDelayed(updateStory, slideChangingTime);



Here is StorySlide


public class StorySlide extends Fragment {
// other code

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// other code
// storySlides is list of object contain info about story
if( storySlides.getType == VIDEO )
slideInterface.onKillThread(true); // it will stop the thread

}

@Override
public void onVideoPrepare(){ slideInterface.startThread() }

}



When storyslide type is video it stop the updateStory thread to change next slide and wait until video is prepared after video prepration it start again changing slides. It's working fine but problemm is in FragmentStatePagerAdapter


updateStory


FragmentStatePagerAdapter



FragmentStatePagerAdapter create at least one or two fragments already maybe for better performance. If you are on the first slide it already created the next slide for you but show only when you set mPager.setCurrentItem(itemNumber)
You can check it by seeing Logs.
This behaviour is creating problem for me. For example if first slide is image and second one is video. It create both simultaneously because when slide is video it prevent thread to change slide until video load. Due to this first slide is not changed until until video is not load in second slide.
I want first slide change and second slide wait until video is load.


FragmentStatePagerAdapter


fragments


mPager.setCurrentItem(itemNumber)



Please let me know where I'm making problem.



Update



All the problem is with the behaviuor of FragmentStatePagerAdapter. It creates view before displaying. And there is no override method that inform view is displaying or not. How can I make a such method myself. Or stop FragmentStatePagerAdapter to create view before displaying ? Is there any way ?


FragmentStatePagerAdapter


FragmentStatePagerAdapter





Here's the answer you are looking for.
– azizbekian
Jun 29 at 10:28









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined