Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Created August 20, 2012 16:14
Show Gist options
  • Save chrisjenx/3405429 to your computer and use it in GitHub Desktop.
Save chrisjenx/3405429 to your computer and use it in GitHub Desktop.
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
Copy link

Would this work when orientation change?

@AlbertFFS
Copy link

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