效果
size是3的倍数时
![](https://i-blog.csdnimg.cn/blog_migrate/c7f81b515b069d080b3ccb42c39c700f.jpeg)
其他情况下
![](https://i-blog.csdnimg.cn/blog_migrate/4f81fd8cd77dcaa767da18a18b23e38e.jpeg)
初始化
private List<HonerUserBean> anchorList;
private ViewPager vp_anchor;
private ViewPagerAdapter mAdapter;
private CirclePageIndicator cvpi;
mAdapter
=
new
ViewPagerAdapter();
vp_anchor.setAdapter(mAdapter);
cvpi.setCentered(true);
cvpi.setRadius(7);
cvpi.setPageColor(context.getResources().getColor(R.color.tips_custom));
cvpi.setFillColor(context.getResources().getColor(R.color.main_color));
cvpi.setStrokeWidth(0);
cvpi.setViewPager(vp_anchor);
<android.support.v4.view.ViewPager
android:id="@+id/vp_anchor"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="14dp"
android:paddingLeft="20dp"
android:paddingRight="20dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/cvpi"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_gravity="center_horizontal" />
数据处理
//异步请求服务器信息
JSONArray anchorArray = obj.optJSONArray("reputation");
if (anchorArray != null && anchorArray.length() > 0) {
anchorList.clear();
for (int i = 0; i < anchorArray.length(); i++) {
anchorList.add(new HonerUserBean(anchorArray.optJSONObject(i)));
}
}
//更新UI
vp_anchor.setAdapter(mAdapter);//每次都要重新设置一下adapter,否则会显示异常!
mAdapter.notifyDataSetChanged();
if (anchorList.size() > 3) {
cvpi.setVisibility(View.VISIBLE);
} else {
cvpi.setVisibility(View.GONE);
}
适配器
/**
* 冠名主播的适配器
*/
class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
if (anchorList.size() <= 3) {
return 1;
} else {
if (anchorList.size() % 3 == 0) {
return anchorList.size() / 3;
} else {
return anchorList.size() / 3 + 1;
}
}
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
//
int
android.support.v4.view.PagerAdapter.POSITION_NONE = -2
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (object instanceof View) {
container.removeView((View) object);
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
1);
//最后一个参数是:权重
NormalRankViewItem first = new NormalRankViewItem(context);
NormalRankViewItem second = new NormalRankViewItem(context);
NormalRankViewItem third = new NormalRankViewItem(context);
HonerUserBean firstBean = null;
HonerUserBean secondBean = null;
HonerUserBean thirdBean = null;
try {
firstBean = anchorList.get(3 * position + 0);
secondBean = anchorList.get(3 * position + 1);
thirdBean = anchorList.get(3 * position + 2);
} catch (Exception e) {
}
first.setAnchorInfo(firstBean);
second.setAnchorInfo(secondBean);
third.setAnchorInfo(thirdBean);
first.setLayoutParams(layoutParams);
second.setLayoutParams(layoutParams);
third.setLayoutParams(layoutParams);
linearLayout.addView(first);
linearLayout.addView(second);
linearLayout.addView(third);
container.addView(linearLayout);
return linearLayout;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}