If you think the Android project Tacere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (c) 2014 Jonathan Nelson/*www.java2s.com*/
* Released under the BSD license. For details see the COPYING file.
*/package com.viewpagerindicator;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
importstatic android.view.ViewGroup.LayoutParams.FILL_PARENT;
importstatic android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
/**
* This widget implements the dynamic action bar tab behavior that can change
* across different configurations or circumstances.
*/publicclass IconPageIndicator extends HorizontalScrollView implements PageIndicator {
privatefinal IcsLinearLayout mIconsLayout;
private ViewPager mViewPager;
private OnPageChangeListener mListener;
private Runnable mIconSelector;
privateint mSelectedIndex;
public IconPageIndicator(Context context) {
this(context, null);
}
public IconPageIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
setHorizontalScrollBarEnabled(false);
mIconsLayout = new IcsLinearLayout(context, R.attr.vpiIconPageIndicatorStyle);
addView(mIconsLayout, new LayoutParams(WRAP_CONTENT, FILL_PARENT, Gravity.CENTER));
}
privatevoid animateToIcon(finalint position) {
final View iconView = mIconsLayout.getChildAt(position);
if (mIconSelector != null) {
removeCallbacks(mIconSelector);
}
mIconSelector = new Runnable() {
publicvoid run() {
finalint scrollPos = iconView.getLeft() - (getWidth() - iconView.getWidth()) / 2;
smoothScrollTo(scrollPos, 0);
mIconSelector = null;
}
};
post(mIconSelector);
}
@Override
publicvoid onAttachedToWindow() {
super.onAttachedToWindow();
if (mIconSelector != null) {
// Re-post the selector we saved
post(mIconSelector);
}
}
@Override
publicvoid onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mIconSelector != null) {
removeCallbacks(mIconSelector);
}
}
@Override
publicvoid onPageScrollStateChanged(int arg0) {
if (mListener != null) {
mListener.onPageScrollStateChanged(arg0);
}
}
@Override
publicvoid onPageScrolled(int arg0, float arg1, int arg2) {
if (mListener != null) {
mListener.onPageScrolled(arg0, arg1, arg2);
}
}
@Override
publicvoid onPageSelected(int arg0) {
setCurrentItem(arg0);
if (mListener != null) {
mListener.onPageSelected(arg0);
}
}
@Override
publicvoid setViewPager(ViewPager view) {
if (mViewPager == view) {
return;
}
if (mViewPager != null) {
mViewPager.setOnPageChangeListener(null);
}
PagerAdapter adapter = view.getAdapter();
if (adapter == null) {
thrownew IllegalStateException("ViewPager does not have adapter instance.");
}
mViewPager = view;
view.setOnPageChangeListener(this);
notifyDataSetChanged();
}
publicvoid notifyDataSetChanged() {
mIconsLayout.removeAllViews();
IconPagerAdapter iconAdapter = (IconPagerAdapter) mViewPager.getAdapter();
int count = iconAdapter.getCount();
for (int i = 0; i < count; i++) {
ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);
view.setImageResource(iconAdapter.getIconResId(i));
mIconsLayout.addView(view);
}
if (mSelectedIndex > count) {
mSelectedIndex = count - 1;
}
setCurrentItem(mSelectedIndex);
requestLayout();
}
@Override
publicvoid setViewPager(ViewPager view, int initialPosition) {
setViewPager(view);
setCurrentItem(initialPosition);
}
@Override
publicvoid setCurrentItem(int item) {
if (mViewPager == null) {
thrownew IllegalStateException("ViewPager has not been bound.");
}
mSelectedIndex = item;
mViewPager.setCurrentItem(item);
int tabCount = mIconsLayout.getChildCount();
for (int i = 0; i < tabCount; i++) {
View child = mIconsLayout.getChildAt(i);
boolean isSelected = (i == item);
child.setSelected(isSelected);
if (isSelected) {
animateToIcon(item);
}
}
}
@Override
publicvoid setOnPageChangeListener(OnPageChangeListener listener) {
mListener = listener;
}
}