user2678122
user2678122

Reputation: 89

How to update Fragment when scrolled in [Scrollable Tabs + Swipe] Navigation-Android?

So I'm using the Scrollable tabs + swipe navigations to play different music files on different tabs screens.

Here's my modified OnCreateView for the default fragment that's auto-generated

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
         TextView dummyTextView = (TextView) rootView
         .findViewById(R.id.sT);
         dummyTextView.setText(Integer.toString(getArguments().getInt(
         ARG_SECTION_NUMBER)));
        AssetFileDescriptor fda;
        MediaPlayer amp = new MediaPlayer();

        try {    
            fda = rootView.getContext().getAssets().openFd("songname.mp3");
            amp.reset();
            amp.setDataSource(fda.getFileDescriptor());
            amp.prepare();
            amp.start();

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }    
        return rootView;    

} Here I just have a textview that shows the index number of screen. Say first screen shows "1", 2nd one shows "2" in a textView ...and so on.

I want an imageView for each screen too, but I can do this.

The trouble is with playing audio.

Right now it just plays a song when the screen starts up. Say the song is playing.

If the user swipes left or right, how to change the song playing ? I need to stop this one, change the "setDataSource" according to the index number (ARG_SECTION_NUMBER)... and play it again .

But how to know if the user swiped and changed the open tab ? (whether by scrollable tab or swipe)

On what User event do I make these changes ? Can I set something that by default stops the playing song and switches over to the next one as the screen changes...?

I intend to add a Play/Pause button eventually. Even with that I would want to change resources with screens..Hence this question

Thnx !

Upvotes: 1

Views: 1199

Answers (1)

Sushil
Sushil

Reputation: 8488

ViewPager has a setOnPageChangeListener() method that will allow you to set an object that gets a callback when a new page becomes the one the user is looking at.

something like this will get you on the right path:

mPageChangeListener = new OnPageChangeListener() {

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int pos) {
        playSoundForPage(pos);
    }

};
yourViewPager.setOnPageChangeListener(mPageChangeListener);

however you'll have to put this in whatever owns your ViewPager (activity, or fragment) and not your Adapter.

Or you can use this:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) { }
else {  }
}

Upvotes: 2

Related Questions