Created
August 30, 2012 08:26
-
-
Save erickok/3524159 to your computer and use it in GitHub Desktop.
A simple ViewPagerIndicator that shows an 'on' or 'off' image per page appropriately
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 nl.nl2312.android.components; | |
import nl.nl2312.android.R; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import com.viewpagerindicator.PageIndicator; | |
public class OnOffPageIndicator extends LinearLayout implements PageIndicator { | |
private static final float INDICATOR_MARGIN = 3F; // In dip | |
private final Drawable offDrawable; | |
private final Drawable onDrawable; | |
private ViewPager viewPager; | |
private int currentPage; | |
private final int indicatorMargin; | |
private final float screenScale = getResources().getDisplayMetrics().density; | |
public OnOffPageIndicator(Context context) { | |
this(context, null); | |
} | |
public OnOffPageIndicator(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public OnOffPageIndicator(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
final Resources res = getResources(); | |
offDrawable = res.getDrawable(R.drawable.widget_indicator_off); | |
onDrawable = res.getDrawable(R.drawable.widget_indicator_on); | |
indicatorMargin = (int) (INDICATOR_MARGIN * screenScale + 0.5f); | |
} | |
@Override | |
public void setViewPager(ViewPager view) { | |
if (viewPager == view) { | |
return; | |
} | |
if (viewPager != null) { | |
viewPager.setOnPageChangeListener(null); | |
} | |
if (view.getAdapter() == null) { | |
throw new IllegalStateException("ViewPager does not have adapter instance."); | |
} | |
viewPager = view; | |
viewPager.setOnPageChangeListener(this); | |
buildIndicator(); | |
} | |
private void buildIndicator() { | |
removeAllViews(); | |
for (int i = 0; i < viewPager.getAdapter().getCount(); i++) { | |
ImageView circle = new ImageView(getContext()); | |
circle.setImageDrawable(offDrawable); | |
MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT); | |
//mlp.setMargins(indicatorMargin, 0, indicatorMargin, 0); | |
circle.setPadding(indicatorMargin, 0, indicatorMargin, 0); | |
addView(circle, mlp); | |
} | |
} | |
@Override | |
public void setCurrentItem(int item) { | |
if (viewPager == null) { | |
throw new IllegalStateException("ViewPager has not been bound."); | |
} | |
viewPager.setCurrentItem(item); | |
currentPage = item; | |
updateIndicator(); | |
} | |
private void updateIndicator() { | |
for (int i = 0; i < viewPager.getAdapter().getCount(); i++) { | |
ImageView circle = (ImageView) getChildAt(i); | |
circle.setImageDrawable(i == currentPage? onDrawable: offDrawable); | |
} | |
} | |
@Override | |
public void setViewPager(ViewPager view, int initialPosition) { | |
setViewPager(view); | |
setCurrentItem(initialPosition); | |
} | |
@Override | |
public void notifyDataSetChanged() { | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
currentPage = position; | |
updateIndicator(); | |
} | |
@Override | |
public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) { | |
} | |
@Override | |
public void onPageScrollStateChanged(int arg0) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment