问题描述
我想实现一个自定义IconPageIndicator - viewpager。我想自定义的com.viewpagerindicator.IconPageIndicator显示的图标边距和补白。所以我写了我的自定义IconPageIndicator和CustomLinearLayout。我在CustomLinearLayout改变了dividerPadding变量。这里是code。但是,这似乎并没有工作。边距和补白仍然是相同的。任何帮助?
类CustomLinearLayout扩展的LinearLayout {
私有静态最终诠释[] LL =新INT [] {
/ * 0 * / android.R.attr.divider,
/ * 1 * / android.R.attr.showDividers,
/ * 2 * / android.R.attr.dividerPadding,
};
私有静态最终诠释LL_DIVIDER = 10;
私有静态最终诠释LL_SHOW_DIVIDER = 2;
私有静态最终诠释LL_DIVIDER_PADDING = 10;
私人绘制对象mDivider;
私人诠释mDividerWidth;
私人诠释mDividerHeight;
私人诠释mShowDividers;
私人诠释mDividerPadding;
公共CustomLinearLayout(上下文的背景下,诠释themeAttr){
超(上下文);
TypedArray一个= context.obtainStyledAttributes(空,LL,themeAttr,0);
setDividerDrawable(a.getDrawable(CustomLinearLayout.LL_DIVIDER));
mDividerPadding = a.getDimensionPixelSize(LL_DIVIDER_PADDING,0);
mShowDividers = a.getInteger(LL_SHOW_DIVIDER,SHOW_DIVIDER_NONE);
a.recycle();
}
公共无效setDividerDrawable(可绘制分){
如果(分== mDivider){
返回;
}
mDivider =分;
如果(分!= NULL){
mDividerWidth = divider.getIntrinsicWidth();
mDividerHeight = divider.getIntrinsicHeight();
} 其他 {
mDividerWidth = 5;
mDividerHeight = 5;
}
setWillNotDraw(分== NULL);
requestLayout();
}
@覆盖
保护无效measureChildWithMargins(查看孩子,诠释parentWidthMeasureSpec,诠释widthUsed,诠释parentHeightMeasureSpec,诠释heightUsed){
最终诠释指数= indexOfChild(子);
最终诠释取向= getOrientation();
最后的LayoutParams PARAMS =(的LayoutParams)child.getLayoutParams();
如果(hasDividerBeforeChildAt(索引)){
如果(定向==垂直){
//账户的分频推的一切行动
params.topMargin = 10;
} 其他 {
//账户的分推一切离开
params.leftMargin = 10;
}
}
最终诠释计数= getChildCount();
如果(指数==计数 - 1){
如果(hasDividerBeforeChildAt(计数)){
如果(定向==垂直){
params.bottomMargin = 10;
} 其他 {
params.rightMargin = 10;
}
}
}
super.measureChildWithMargins(孩子,parentWidthMeasureSpec,widthUsed,parentHeightMeasureSpec,heightUsed);
}
@覆盖
保护无效的OnDraw(帆布油画){
如果(mDivider!= NULL){
如果(getOrientation()==垂直){
drawDividersVertical(画布);
} 其他 {
drawDividersHorizontal(画布);
}
}
super.onDraw(画布);
}
私人无效drawDividersVertical(帆布油画){
最终诠释计数= getChildCount();
的for(int i = 0; I<计数;我++){
最后查看孩子= getChildAt(我);
如果(孩子= NULL和放大器;!&安培;!child.getVisibility()= GONE){
如果(hasDividerBeforeChildAt(i))的{
最后android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)child.getLayoutParams();
最终诠释顶级= child.getTop() - lp.topMargin / * - mDividerHeight * /;
drawHorizontalDivider(帆布,上图);
}
}
}
如果(hasDividerBeforeChildAt(计数)){
最后查看孩子= getChildAt(计数 - 1);
INT底部= 0;
如果(孩子== NULL){
底部=的getHeight() - getPaddingBottom() - mDividerHeight;
} 其他 {
//最后的LayoutParams LP =(的LayoutParams)child.getLayoutParams();
底部= child.getBottom()/ * + lp.bottomMargin * /;
}
drawHorizontalDivider(帆布,底部);
}
}
私人无效drawDividersHorizontal(帆布油画){
最终诠释计数= getChildCount();
的for(int i = 0; I<计数;我++){
最后查看孩子= getChildAt(我);
如果(孩子= NULL和放大器;!&安培;!child.getVisibility()= GONE){
如果(hasDividerBeforeChildAt(i))的{
最后android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)child.getLayoutParams();
最终诠释左= child.getLeft() - lp.leftMargin / * - mDividerWidth * /;
drawVerticalDivider(画布,左图);
}
}
}
如果(hasDividerBeforeChildAt(计数)){
最后查看孩子= getChildAt(计数 - 1);
诠释正确= 0;
如果(孩子== NULL){
右=的getWidth() - getPaddingRight() - mDividerWidth;
} 其他 {
//最后的LayoutParams LP =(的LayoutParams)child.getLayoutParams();
右= child.getRight()/ * + lp.rightMargin * /;
}
drawVerticalDivider(帆布,右);
}
}
私人无效drawHorizontalDivider(画布油画,INT顶部){
mDivider.setBounds(getPaddingLeft()+ mDividerPadding,顶,
的getWidth() - getPaddingRight() - mDividerPadding,顶部+ mDividerHeight);
mDivider.draw(画布);
}
私人无效drawVerticalDivider(画布油画,诠释左){
mDivider.setBounds(左,getPaddingTop()+ mDividerPadding,
左+ mDividerWidth,getHeight()都会 - getPaddingBottom() - mDividerPadding);
mDivider.draw(画布);
}
私人布尔hasDividerBeforeChildAt(INT childIndex){
如果(childIndex == 0 || childIndex == getChildCount()){
返回false;
}
如果((mShowDividers&安培;!SHOW_DIVIDER_MIDDLE)= 0){
布尔hasVisibleViewBefore = FALSE;
的for(int i = childIndex - 1; I> = 0;我 - ){
如果(getChildAt(ⅰ).getVisibility()!= GONE){
hasVisibleViewBefore = TRUE;
打破;
}
}
返回hasVisibleViewBefore;
}
返回false;
}
}
公共类CustomIconPageIndicator扩展HorizontalScrollView实现PageIndicator {
私人最终CustomLinearLayout mIconsLayout;
私人ViewPager mViewPager;
私人OnPageChangeListener mListener;
私人的Runnable mIconSelector;
私人诠释mSelectedIndex;
公共CustomIconPageIndicator(上下文的背景下){
这(背景下,NULL);
}
公共CustomIconPageIndicator(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
setHorizontalScrollBarEnabled(假);
mIconsLayout =新CustomLinearLayout(背景下,com.viewpagerindicator.R.attr.vpiIconPageIndicatorStyle);
addView(mIconsLayout,新的LayoutParams(WRAP_CONTENT,FILL_PARENT,Gravity.CENTER));
}
私人无效animateToIcon(最终诠释位置){
最后查看ICONVIEW = mIconsLayout.getChildAt(位置);
如果(mIconSelector!= NULL){
removeCallbacks(mIconSelector);
}
mIconSelector =新的Runnable(){
公共无效的run(){
最终诠释scrollPos = iconView.getLeft() - (的getWidth() - iconView.getWidth())/ 2;
smoothScrollTo(scrollPos,0);
mIconSelector = NULL;
}
};
后期(mIconSelector);
}
@覆盖
公共无效onAttachedToWindow(){
super.onAttachedToWindow();
如果(mIconSelector!= NULL){
//我们节省重新发布该选择
后期(mIconSelector);
}
}
@覆盖
公共无效onDetachedFromWindow(){
super.onDetachedFromWindow();
如果(mIconSelector!= NULL){
removeCallbacks(mIconSelector);
}
}
@覆盖
公共无效onPageScrollStateChanged(INT为arg0){
如果(mListener!= NULL){
mListener.onPageScrollStateChanged(为arg0);
}
}
@覆盖
公共无效onPageScrolled(INT为arg0,浮动ARG1,ARG2 INT){
如果(mListener!= NULL){
mListener.onPageScrolled(为arg0,ARG1,ARG2);
}
}
@覆盖
公共无效onPageSelected(INT为arg0){
setCurrentItem(为arg0);
如果(mListener!= NULL){
mListener.onPageSelected(为arg0);
}
}
@覆盖
公共无效setViewPager(ViewPager视图){
如果(mViewPager ==视图){
返回;
}
如果(mViewPager!= NULL){
mViewPager.setOnPageChangeListener(空);
}
PagerAdapter适配器= view.getAdapter();
如果(适配器== NULL){
抛出新IllegalStateException异常(ViewPager没有适配器实例。);
}
mViewPager =图。
view.setOnPageChangeListener(本);
notifyDataSetChanged();
}
公共无效notifyDataSetChanged(){
mIconsLayout.removeAllViews();
IconPagerAdapter iconAdapter =(IconPagerAdapter)mViewPager.getAdapter();
诠释计数= iconAdapter.getCount();
的for(int i = 0; I<计数;我++){
ImageView的观点=新ImageView的(的getContext(),空,com.viewpagerindicator.R.attr.vpiIconPageIndicatorStyle);
view.setImageResource(iconAdapter.getIconResId(ⅰ));
view.setTag(+ I);
view.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT一个viewPosition =的Integer.parseInt(v.getTag()的toString());
mViewPager.setCurrentItem(一个viewPosition);
}
});
mIconsLayout.addView(视图);
}
如果(mSelectedIndex>计数){
mSelectedIndex =计数 - 1;
}
setCurrentItem(mSelectedIndex);
requestLayout();
}
@覆盖
公共无效setViewPager(ViewPager观点,诠释initialPosition){
setViewPager(视图);
setCurrentItem(initialPosition);
}
@覆盖
公共无效setCurrentItem(INT项){
如果(mViewPager == NULL){
抛出新IllegalStateException异常(ViewPager一直没有约束。);
}
mSelectedIndex =项目;
mViewPager.setCurrentItem(项目);
INT tabCount = mIconsLayout.getChildCount();
的for(int i = 0; I< tabCount;我++){
查看孩子= mIconsLayout.getChildAt(我);
布尔isSelected =(我==项目);
child.setSelected(isSelected);
如果(isSelected){
animateToIcon(项目);
}
}
}
@覆盖
公共无效setOnPageChangeListener(OnPageChangeListener监听器){
mListener =侦听器;
}
}
您没有正确设置页边距在正确的方法。
开始库项目 - > IconPageIndicator 并修改 notifyDataSetChanged()方法如下:
公共无效notifyDataSetChanged(){
mIconsLayout.removeAllViews();
IconPagerAdapter iconAdapter =(IconPagerAdapter)mViewPager.getAdapter();
诠释计数= iconAdapter.getCount();
的for(int i = 0; I<计数;我++){
ImageView的观点=新ImageView的(的getContext(),空,R.attr.vpiIconPageIndicatorStyle);
LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10,10,0,0); //在这里你可以设置页边距。
view.setLayoutParams(LP);
view.setImageResource(iconAdapter.getIconResId(ⅰ));
view.setTag(+ I);
view.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT一个viewPosition =的Integer.parseInt(v.getTag()的toString());
mViewPager.setCurrentItem(一个viewPosition);
}
});
mIconsLayout.addView(视图);
}
如果(mSelectedIndex>计数){
mSelectedIndex =计数 - 1;
}
setCurrentItem(mSelectedIndex);
requestLayout();
}
不要忘了清理库项目,并重建它。我希望它的工作原理。
I am trying to implement a custom IconPageIndicator - viewpager. I want to customize the margins and paddings of the icons displayed in com.viewpagerindicator.IconPageIndicator. So I wrote my custom IconPageIndicator and CustomLinearLayout. I changed the dividerPadding variable in CustomLinearLayout. Here is the code. But this doesn't seem to work. The margins and paddings are still the same. Any help?
class CustomLinearLayout extends LinearLayout {
private static final int[] LL = new int[] {
/* 0 */ android.R.attr.divider,
/* 1 */ android.R.attr.showDividers,
/* 2 */ android.R.attr.dividerPadding,
};
private static final int LL_DIVIDER = 10;
private static final int LL_SHOW_DIVIDER = 2;
private static final int LL_DIVIDER_PADDING = 10;
private Drawable mDivider;
private int mDividerWidth;
private int mDividerHeight;
private int mShowDividers;
private int mDividerPadding;
public CustomLinearLayout(Context context, int themeAttr) {
super(context);
TypedArray a = context.obtainStyledAttributes(null, LL, themeAttr, 0);
setDividerDrawable(a.getDrawable(CustomLinearLayout.LL_DIVIDER));
mDividerPadding = a.getDimensionPixelSize(LL_DIVIDER_PADDING, 0);
mShowDividers = a.getInteger(LL_SHOW_DIVIDER, SHOW_DIVIDER_NONE);
a.recycle();
}
public void setDividerDrawable(Drawable divider) {
if (divider == mDivider) {
return;
}
mDivider = divider;
if (divider != null) {
mDividerWidth = divider.getIntrinsicWidth();
mDividerHeight = divider.getIntrinsicHeight();
} else {
mDividerWidth = 5;
mDividerHeight = 5;
}
setWillNotDraw(divider == null);
requestLayout();
}
@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
final int index = indexOfChild(child);
final int orientation = getOrientation();
final LayoutParams params = (LayoutParams) child.getLayoutParams();
if (hasDividerBeforeChildAt(index)) {
if (orientation == VERTICAL) {
//Account for the divider by pushing everything up
params.topMargin = 10;
} else {
//Account for the divider by pushing everything left
params.leftMargin = 10;
}
}
final int count = getChildCount();
if (index == count - 1) {
if (hasDividerBeforeChildAt(count)) {
if (orientation == VERTICAL) {
params.bottomMargin = 10;
} else {
params.rightMargin = 10;
}
}
}
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
}
@Override
protected void onDraw(Canvas canvas) {
if (mDivider != null) {
if (getOrientation() == VERTICAL) {
drawDividersVertical(canvas);
} else {
drawDividersHorizontal(canvas);
}
}
super.onDraw(canvas);
}
private void drawDividersVertical(Canvas canvas) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child != null && child.getVisibility() != GONE) {
if (hasDividerBeforeChildAt(i)) {
final android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) child.getLayoutParams();
final int top = child.getTop() - lp.topMargin/* - mDividerHeight*/;
drawHorizontalDivider(canvas, top);
}
}
}
if (hasDividerBeforeChildAt(count)) {
final View child = getChildAt(count - 1);
int bottom = 0;
if (child == null) {
bottom = getHeight() - getPaddingBottom() - mDividerHeight;
} else {
//final LayoutParams lp = (LayoutParams) child.getLayoutParams();
bottom = child.getBottom()/* + lp.bottomMargin*/;
}
drawHorizontalDivider(canvas, bottom);
}
}
private void drawDividersHorizontal(Canvas canvas) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child != null && child.getVisibility() != GONE) {
if (hasDividerBeforeChildAt(i)) {
final android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) child.getLayoutParams();
final int left = child.getLeft() - lp.leftMargin/* - mDividerWidth*/;
drawVerticalDivider(canvas, left);
}
}
}
if (hasDividerBeforeChildAt(count)) {
final View child = getChildAt(count - 1);
int right = 0;
if (child == null) {
right = getWidth() - getPaddingRight() - mDividerWidth;
} else {
//final LayoutParams lp = (LayoutParams) child.getLayoutParams();
right = child.getRight()/* + lp.rightMargin*/;
}
drawVerticalDivider(canvas, right);
}
}
private void drawHorizontalDivider(Canvas canvas, int top) {
mDivider.setBounds(getPaddingLeft() + mDividerPadding, top,
getWidth() - getPaddingRight() - mDividerPadding, top + mDividerHeight);
mDivider.draw(canvas);
}
private void drawVerticalDivider(Canvas canvas, int left) {
mDivider.setBounds(left, getPaddingTop() + mDividerPadding,
left + mDividerWidth, getHeight() - getPaddingBottom() - mDividerPadding);
mDivider.draw(canvas);
}
private boolean hasDividerBeforeChildAt(int childIndex) {
if (childIndex == 0 || childIndex == getChildCount()) {
return false;
}
if ((mShowDividers & SHOW_DIVIDER_MIDDLE) != 0) {
boolean hasVisibleViewBefore = false;
for (int i = childIndex - 1; i >= 0; i--) {
if (getChildAt(i).getVisibility() != GONE) {
hasVisibleViewBefore = true;
break;
}
}
return hasVisibleViewBefore;
}
return false;
}
}
public class CustomIconPageIndicator extends HorizontalScrollView implements PageIndicator {
private final CustomLinearLayout mIconsLayout;
private ViewPager mViewPager;
private OnPageChangeListener mListener;
private Runnable mIconSelector;
private int mSelectedIndex;
public CustomIconPageIndicator(Context context) {
this(context, null);
}
public CustomIconPageIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
setHorizontalScrollBarEnabled(false);
mIconsLayout = new CustomLinearLayout(context, com.viewpagerindicator.R.attr.vpiIconPageIndicatorStyle);
addView(mIconsLayout, new LayoutParams(WRAP_CONTENT, FILL_PARENT, Gravity.CENTER));
}
private void animateToIcon(final int position) {
final View iconView = mIconsLayout.getChildAt(position);
if (mIconSelector != null) {
removeCallbacks(mIconSelector);
}
mIconSelector = new Runnable() {
public void run() {
final int scrollPos = iconView.getLeft() - (getWidth() - iconView.getWidth()) / 2;
smoothScrollTo(scrollPos, 0);
mIconSelector = null;
}
};
post(mIconSelector);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
if (mIconSelector != null) {
// Re-post the selector we saved
post(mIconSelector);
}
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mIconSelector != null) {
removeCallbacks(mIconSelector);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
if (mListener != null) {
mListener.onPageScrollStateChanged(arg0);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if (mListener != null) {
mListener.onPageScrolled(arg0, arg1, arg2);
}
}
@Override
public void onPageSelected(int arg0) {
setCurrentItem(arg0);
if (mListener != null) {
mListener.onPageSelected(arg0);
}
}
@Override
public void setViewPager(ViewPager view) {
if (mViewPager == view) {
return;
}
if (mViewPager != null) {
mViewPager.setOnPageChangeListener(null);
}
PagerAdapter adapter = view.getAdapter();
if (adapter == null) {
throw new IllegalStateException("ViewPager does not have adapter instance.");
}
mViewPager = view;
view.setOnPageChangeListener(this);
notifyDataSetChanged();
}
public void 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, com.viewpagerindicator.R.attr.vpiIconPageIndicatorStyle);
view.setImageResource(iconAdapter.getIconResId(i));
view.setTag(""+i);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int viewPosition = Integer.parseInt(v.getTag().toString());
mViewPager.setCurrentItem(viewPosition);
}
});
mIconsLayout.addView(view);
}
if (mSelectedIndex > count) {
mSelectedIndex = count - 1;
}
setCurrentItem(mSelectedIndex);
requestLayout();
}
@Override
public void setViewPager(ViewPager view, int initialPosition) {
setViewPager(view);
setCurrentItem(initialPosition);
}
@Override
public void setCurrentItem(int item) {
if (mViewPager == null) {
throw new 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
public void setOnPageChangeListener(OnPageChangeListener listener) {
mListener = listener;
}
}
You are not setting margins correctly in right method.
Go Library project-->IconPageIndicator and edit notifyDataSetChanged() method as below:
public void 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);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10 , 10, 0 , 0 ); // Here you can set margins.
view.setLayoutParams(lp);
view.setImageResource(iconAdapter.getIconResId(i));
view.setTag(""+i);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int viewPosition = Integer.parseInt(v.getTag().toString());
mViewPager.setCurrentItem(viewPosition);
}
});
mIconsLayout.addView(view);
}
if (mSelectedIndex > count) {
mSelectedIndex = count - 1;
}
setCurrentItem(mSelectedIndex);
requestLayout();
}
don't forget to clean the library project and rebuild it.I hope it works.
这篇关于在IconPageIndicator自定义空白和边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!