Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

Add margin option in icons of IconPagerIndicator #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library/res/values/vpi__attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@
<!-- View background -->
<attr name="android:background"/>
</declare-styleable>

<declare-styleable name="IconPageIndicator">
<!-- Margin for icons. -->
<attr name="leftMargin" format="dimension" />
<attr name="rightMargin" format="dimension" />
<attr name="topMargin" format="dimension" />
<attr name="bottomMargin" format="dimension" />
</declare-styleable>

</resources>
23 changes: 22 additions & 1 deletion library/src/com/viewpagerindicator/IconPageIndicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.viewpagerindicator;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
Expand All @@ -25,6 +26,7 @@
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Expand All @@ -40,6 +42,11 @@ public class IconPageIndicator extends HorizontalScrollView implements PageIndic
private OnPageChangeListener mListener;
private Runnable mIconSelector;
private int mSelectedIndex;
private int mLeftMargin;
private int mTopMargin;
private int mRightMargin;
private int mBottomMargin;


public IconPageIndicator(Context context) {
this(context, null);
Expand All @@ -51,6 +58,17 @@ public IconPageIndicator(Context context, AttributeSet attrs) {

mIconsLayout = new IcsLinearLayout(context, R.attr.vpiIconPageIndicatorStyle);
addView(mIconsLayout, new LayoutParams(WRAP_CONTENT, FILL_PARENT, Gravity.CENTER));
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.IconPageIndicator, 0, 0);
try {
mLeftMargin = typedArray.getDimensionPixelSize(R.styleable.IconPageIndicator_leftMargin, 0);
mTopMargin = typedArray.getDimensionPixelSize(R.styleable.IconPageIndicator_topMargin, 0);
mRightMargin = typedArray.getDimensionPixelSize(R.styleable.IconPageIndicator_rightMargin, 0);
mBottomMargin = typedArray.getDimensionPixelSize(R.styleable.IconPageIndicator_bottomMargin, 0);
} finally {
typedArray.recycle();
}

}

private void animateToIcon(final int position) {
Expand Down Expand Up @@ -131,7 +149,10 @@ public void notifyDataSetChanged() {
for (int i = 0; i < count; i++) {
ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);
view.setImageResource(iconAdapter.getIconResId(i));
mIconsLayout.addView(view);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
layoutParams.setMargins(mLeftMargin, mTopMargin, mRightMargin, mBottomMargin);
mIconsLayout.addView(view, layoutParams);

}
if (mSelectedIndex > count) {
mSelectedIndex = count - 1;
Expand Down