自己实现了一个更加方便的ViewPagerIndicator,希望大家批评指正,代码如下:
package com.ningfengview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NFViewPager extends LinearLayout {
private HorizontalScrollView mScrollViewHeader = null;
private LinearLayout mHeaderCOntainer= null;
private LinearLayout mTitleCOntainer= null;
private LinearLayout mIndecatorCOntainer= null;
private LinearLayout mIndecatorContainer2 = null;
private LinearLayout mIndecator = null;
private LinearLayout mIndecator2 = null;
private ListmTitleList = null;
ListmFragmentList = null;
private int mHorizOntialMargin= 0;
private ViewPager mViewPager = null;
private int titleSelectedColor = 0;
private int titleUnselectedColor = 0;
private Context cOntext= null;
private NFViewPagerInterface nfinterface=null;
public NFViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.cOntext= context;
this.mTitleList = new ArrayList();
this.mScrollViewHeader = new HorizontalScrollView(context);
this.mScrollViewHeader.setHorizontalScrollBarEnabled(false);
this.mHeaderCOntainer= new LinearLayout(context);
this.mHeaderContainer.setOrientation(LinearLayout.VERTICAL);
this.mViewPager = new ViewPager(context);
this.mTitleCOntainer= new LinearLayout(context);
this.mTitleContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorCOntainer= new LinearLayout(context);
this.mIndecatorContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorContainer2 = new LinearLayout(context);
this.mIndecatorContainer2.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecator = new LinearLayout(context);
this.mIndecator.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecator2 = new LinearLayout(context);
this.mIndecator2.setOrientation(LinearLayout.HORIZONTAL);
}
public interface NFViewPagerInterface{
public void onPageChanged(int currentPage);
}
public class NFOnClickListener implements OnClickListener {
int position = 0;
public NFOnClickListener(int position) {
this.position = position;
}
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
}
public void init(NFViewPagerInterface inter,int id,ListtextList, List frgList,
FragmentManager frm, int indecatorHeight, int titleMarginLeft,
int titleMarginRight, int titleMarginTop, int titleMarginBottom,
int indecatorBackgroundColor, int indecatorColor,
int titleUnselectedColor, int titleSelectedColor,
float titleTextSize) {
this.mViewPager.setId(id);
this.nfinterface=inter;
this.titleSelectedColor = titleSelectedColor;
this.titleUnselectedColor = titleUnselectedColor;
this.mFragmentList = frgList;
LayoutParams titleParam = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// titleParam.setMargins(titleMarginHorizontal, titleMarginVertical,
// titleMarginHorizontal, titleMarginVertical);
for (int i = 0; i
TextView texttitle = new TextView(context);
texttitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleTextSize);
texttitle.setText(textList.get(i));
texttitle.setOnClickListener(new NFOnClickListener(i));
texttitle.setPadding(titleMarginLeft, titleMarginTop,
titleMarginRight, titleMarginBottom);
if (i ==0) {
texttitle.setTextColor(titleSelectedColor);
} else {
texttitle.setTextColor(titleUnselectedColor);
}
this.mTitleList.add(texttitle);
this.mTitleContainer.addView(texttitle, titleParam);
}
LayoutParams titleCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer
.addView(this.mTitleContainer, titleContainerParam);
this.mIndecator.setBackgroundColor(indecatorColor);
this.mIndecator2.setBackgroundColor(indecatorColor);
measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight);
this.mIndecatorContainer.addView(this.mIndecator, indecatorParam);
LayoutParams indecatorParam2 = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight*4);
this.mIndecatorContainer2.addView(this.mIndecator2, indecatorParam2);
this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorCOntainerParam= new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams indecatorContainerParam2 = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer.addView(this.mIndecatorContainer2,
indecatorContainerParam2);
this.mHeaderContainer.addView(this.mIndecatorContainer,
indecatorContainerParam);
LayoutParams headerCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mScrollViewHeader.addView(this.mHeaderContainer,
headerContainerParam);
LayoutParams scrollViewHeaderParam = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(mScrollViewHeader, scrollViewHeaderParam);
LayoutParams viewPagerParam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
this.mViewPager.setAdapter(new MyFragmentPagerAdapter(frm,
this.mFragmentList));
this.mViewPager.setCurrentItem(0);
this.mViewPager
.setOnPageChangeListener(new NFViewPagerOnPageChangeListener());
this.addView(this.mViewPager, viewPagerParam);
}
public ViewPager getViewPager()
{
return this.mViewPager;
}
public void setNFCurrentItem(int index)
{
mViewPager.setCurrentItem(index);
int len = mTitleList.size();
for (int i = 0; iif (index == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}
}
public class NFViewPagerOnPageChangeListener implements
OnPageChangeListener {
private int position = 0;
@Override
public void onPageScrollStateChanged(int arg0) {
if (arg0 == 0) {
int len = mTitleList.size();
for (int i = 0; iif (this.position == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
mIndecatorContainer.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
mIndecatorContainer2.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
if (arg0
int tempwidth = mTitleList.get(arg0).getWidth()
+ (int) ((mTitleList.get(arg0 + 1).getWidth() - mTitleList
.get(arg0).getWidth()) * arg1);
LayoutParams indecatorParam = (LayoutParams) mIndecator
.getLayoutParams();
indecatorParam.width = tempwidth;
mIndecator.setLayoutParams(indecatorParam);
LayoutParams indecatorParam2 = (LayoutParams) mIndecator2
.getLayoutParams();
indecatorParam2.width = tempwidth;
mIndecator2.setLayoutParams(indecatorParam2);
} else {
}
if (arg0 > 0) {
mScrollViewHeader
.scrollTo(
mTitleList.get(arg0 - 1).getLeft()
+ (int) (mTitleList.get(arg0 - 1)
.getWidth() * arg1), 0);
}
}
@Override
public void onPageSelected(int arg0) {
this.position = arg0;
nfinterface.onPageChanged(arg0);
}
}
public static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public Listlist;
public MyFragmentPagerAdapter(FragmentManager fm, Listlist) {
super(fm);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
// 初始化每个页卡选项
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
// TODO Auto-generated method stub
return super.instantiateItem(arg0, arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
System.out.println( "**********************************position Destory" + position);
super.destroyItem(container, position, object);
}
}
private void measureView(View child, int flag) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
if (flag == 1) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
} else if (2 == flag) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
}
package com.ningfengview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NFStableWidthViewPager extends LinearLayout {
private HorizontalScrollView mScrollViewHeader = null;
private LinearLayout mHeaderCOntainer= null;
private LinearLayout mTitleCOntainer= null;
private LinearLayout mIndecatorCOntainer= null;
private LinearLayout mIndecatorContainer2=null;
private LinearLayout mIndecator = null;
private LinearLayout mIndecator2 = null;
private ListmTitleList = null;
ListmFragmentList = null;
private int mHorizOntialMargin= 0;
private ViewPager mViewPager = null;
private int titleSelectedColor = 0;
private int titleUnselectedColor = 0;
private Context cOntext= null;
public NFStableWidthViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.cOntext= context;
this.mTitleList = new ArrayList();
this.mScrollViewHeader = new HorizontalScrollView(context);
this.mScrollViewHeader.setHorizontalScrollBarEnabled(false);
this.mHeaderCOntainer= new LinearLayout(context);
this.mHeaderContainer.setOrientation(LinearLayout.VERTICAL);
this.mViewPager = new ViewPager(context);
this.mTitleCOntainer= new LinearLayout(context);
this.mTitleContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorCOntainer= new LinearLayout(context);
this.mIndecatorContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorContainer2 = new LinearLayout(context);
this.mIndecatorContainer2.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecator = new LinearLayout(context);
this.mIndecator.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecator2 = new LinearLayout(context);
this.mIndecator2.setOrientation(LinearLayout.HORIZONTAL);
}
public class NFOnClickListener implements OnClickListener {
int position = 0;
public NFOnClickListener(int position) {
this.position = position;
}
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
}
public void init(int id,ListtextList, List frgList,
FragmentManager frm, int indecatorHeight, int singleTitleWidth,int marginVertical,
int indecatorBackgroundColor, int indecatorColor,
int titleUnselectedColor, int titleSelectedColor,
float titleTextSize) {
this.mViewPager.setId(id);
this.titleSelectedColor = titleSelectedColor;
this.titleUnselectedColor = titleUnselectedColor;
this.mFragmentList = frgList;
LayoutParams titleParam = new LayoutParams(singleTitleWidth,
LayoutParams.WRAP_CONTENT);
titleParam.setMargins(0, marginVertical,
0, marginVertical);
for (int i = 0; i
TextView texttitle = new TextView(context);
texttitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleTextSize);
texttitle.setText(textList.get(i));
texttitle.setOnClickListener(new NFOnClickListener(i));
texttitle.setGravity(Gravity.CENTER_HORIZONTAL);
//texttitle.setPadding(titleMarginLeft, titleMarginTop,
//titleMarginRight, titleMarginBottom);
if (i ==0) {
texttitle.setTextColor(titleSelectedColor);
} else {
texttitle.setTextColor(titleUnselectedColor);
}
this.mTitleList.add(texttitle);
this.mTitleContainer.addView(texttitle, titleParam);
}
LayoutParams titleCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer
.addView(this.mTitleContainer, titleContainerParam);
this.mIndecator.setBackgroundColor(indecatorColor);
measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight);
this.mIndecatorContainer.addView(this.mIndecator, indecatorParam);
this.mIndecator2.setBackgroundColor(indecatorColor);
measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam2 = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight*4);
this.mIndecatorContainer2.addView(this.mIndecator2, indecatorParam2);
this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorCOntainerParam= new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorContainerParam2 = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer.addView(this.mIndecatorContainer2,
indecatorContainerParam2);
this.mHeaderContainer.addView(this.mIndecatorContainer,
indecatorContainerParam);
LayoutParams headerCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mScrollViewHeader.addView(this.mHeaderContainer,
headerContainerParam);
LayoutParams scrollViewHeaderParam = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(mScrollViewHeader, scrollViewHeaderParam);
LayoutParams viewPagerParam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
this.mViewPager.setAdapter(new MyFragmentPagerAdapter(frm,
this.mFragmentList));
this.mViewPager.setCurrentItem(0);
this.mViewPager
.setOnPageChangeListener(new NFViewPagerOnPageChangeListener());
this.addView(this.mViewPager, viewPagerParam);
}
public ViewPager getViewPager()
{
return this.mViewPager;
}
public class NFViewPagerOnPageChangeListener implements
OnPageChangeListener {
private int position = 0;
@Override
public void onPageScrollStateChanged(int arg0) {
if (arg0 == 0) {
int len = mTitleList.size();
for (int i = 0; iif (this.position == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
mIndecatorContainer.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
mIndecatorContainer2.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
if (arg0
int tempwidth = mTitleList.get(arg0).getWidth()
+ (int) ((mTitleList.get(arg0 + 1).getWidth() - mTitleList
.get(arg0).getWidth()) * arg1);
LayoutParams indecatorParam = (LayoutParams) mIndecator
.getLayoutParams();
indecatorParam.width = tempwidth;
mIndecator.setLayoutParams(indecatorParam);
LayoutParams indecatorParam2 = (LayoutParams) mIndecator2
.getLayoutParams();
indecatorParam2.width = tempwidth;
mIndecator2.setLayoutParams(indecatorParam2);
} else {
}
if (arg0 > 0) {
mScrollViewHeader
.scrollTo(
mTitleList.get(arg0 - 1).getLeft()
+ (int) (mTitleList.get(arg0 - 1)
.getWidth() * arg1), 0);
}
}
@Override
public void onPageSelected(int arg0) {
this.position = arg0;
}
}
public static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public Listlist;
public MyFragmentPagerAdapter(FragmentManager fm, Listlist) {
super(fm);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
// 初始化每个页卡选项
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
// TODO Auto-generated method stub
return super.instantiateItem(arg0, arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
}
private void measureView(View child, int flag) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
if (flag == 1) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
} else if (2 == flag) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
}
android:id="@+id/nfviewpager"
android:layout_
android:layout_
android:orientation="vertical" >
List如果是固定宽度的只需要把NFViewPager换成NFStableWidthViewPager并把初始化代码换成下面的就行mTempData = new ArrayList ();
ListmTempFragments = new ArrayList ();
NFViewPager nfViewPager = null;
nfViewPager = (NFViewPager) view
.findViewById(R.id.nfviewpager);
mTempData.add("title1");
mTempData.add("title2");
mTempData.add("title3");
mTempData.add("title4");
mTempData.add("title5");
mTempData.add("title6");
mTempData.add("title7");
mTempData.add("title8");
mTempData.add("title9");
mTempData.add("title10");
for (int i = 0; i <10; i++) {
mTempFragments.add(new TestFrg());
}
nfViewPager.init(new NFViewPagerInterface() {
@Override
public void onPageChanged(int currentPage) {
// TODO Auto-generated method stub
}
},5000, mTempData, mTempFragments,
getChildFragmentManager(), 1, 6* Helper.screenwidth / 100,
6 * Helper.screenwidth / 100,
(int) (1.5 * Helper.screenheight / 200),
(int) (1.5 * Helper.screenheight / 200),
Color.rgb(222, 241, 253), Color.rgb(51, 158, 226),
Color.rgb(102, 102, 102), Color.rgb(51, 158, 226), 16);
nfViewPager.setNFCurrentItem(0);
nfViewPager.init(123,this.mData, this.mFragment,getChildFragmentManager(), 1,Helper.screenwidth/4/*此参数即是每个title的固定宽度*/,8,Color.rgb(211, 237, 253),Color.rgb(51, 158, 226), Color.rgb(102, 102, 102), Color.rgb(51, 158, 226), 16);
是不是比ViewPagerIndicator还好用。欢迎拍砖吐槽