Last active
June 21, 2017 17:27
-
-
Save xuhaibahmad/2394175bd6ddf0f7138a9dd21547a9f4 to your computer and use it in GitHub Desktop.
A fragment less View Pager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zuhaibahmad.upworkevaluationproject.utils; | |
import android.content.Context; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* Created by Zuhaib Ahmad on 1/10/2017. | |
* <p> | |
* View pager used for a finite, low number of pages, where there is no need for optimization. | |
*/ | |
public class StaticViewPager extends ViewPager { | |
/** | |
* Initialize the view. | |
* | |
* @param context The application context. | |
*/ | |
public StaticViewPager(final Context context) { | |
super(context); | |
} | |
/** | |
* Initialize the view. | |
* | |
* @param context The application context. | |
* @param attrs The requested attributes. | |
*/ | |
public StaticViewPager(final Context context, final AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
// Make sure all are loaded at once | |
final int childrenCount = getChildCount(); | |
setOffscreenPageLimit(childrenCount - 1); | |
// Attach the adapter | |
setAdapter(new PagerAdapter() { | |
@Override | |
public Object instantiateItem(final ViewGroup container, final int position) { | |
return container.getChildAt(position); | |
} | |
@Override | |
public boolean isViewFromObject(final View arg0, final Object arg1) { | |
return arg0 == arg1; | |
} | |
@Override | |
public int getCount() { | |
return childrenCount; | |
} | |
@Override | |
public void destroyItem(final View container, final int position, final Object object) { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment