Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BasePagerFragment - Simple way to place a FragmentViewPager in a Fragment!
/**
* @project
* @author chris.jenkins
* @created Dec 28, 2011
*/
package com.application.android.ui.fragments;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author chris.jenkins
*/
public abstract class BasePagerFragment extends BaseFragment
{
// Data
private FragmentStatePagerAdapter mAdapter = null;
// Views
private int layoutRes;
private View mRootView;
private ViewPager mPager;
// Handlers
private final Handler handler = new Handler();
private Runnable runPager;
private boolean mCreated = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
mRootView = inflater.inflate(layoutRes, container, false);
mPager = (ViewPager) mRootView.findViewById(R.id.pager);
mPager.setOffscreenPageLimit(2);
return mRootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (runPager != null) handler.post(runPager);
mCreated = true;
}
/**
* @see android.support.v4.app.Fragment#onPause()
*/
@Override
public void onPause()
{
super.onPause();
handler.removeCallbacks(runPager);
}
/**
* Set the ViewPager adapter from this or from a subclass.
*
* @author chris.jenkins
* @param adapter This is a FragmentStatePagerAdapter due to the way it creates the TAG for the
* Fragment.
*/
protected void setAdapter(FragmentStatePagerAdapter adapter)
{
mAdapter = adapter;
runPager = new Runnable() {
@Override
public void run()
{
mPager.setAdapter(mAdapter);
}
};
if (mCreated)
{
handler.post(runPager);
}
}
/**
* Has to be set before onCreateView
*
* @author chris.jenkins
* @param layoutRes
*/
protected void setLayoutRes(int layoutRes)
{
this.layoutRes = layoutRes;
}
}
@necronet

This comment has been minimized.

Copy link

necronet commented Dec 16, 2013

Would this work when orientation change?

@AlbertFFS

This comment has been minimized.

Copy link

AlbertFFS commented Jan 15, 2014

instead of :
protected void setLayoutRes(int layoutRes) {this.layoutRes = layoutRes}

you can enforce the layoutResrcId pre-requisite by turning it into an abstract method that the child classes must implement:

protected abstract int getLayoutResId();

This also removes the need for private instance variable layoutRes as follows:
...
mRootView = inflater.inflate(getLayoutResId(), container, false);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.