Skip to content

Commit

Permalink
修复只有一页的情况下小圆点出错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglu1209 committed Mar 3, 2017
1 parent b2a1243 commit 3c9a876
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@
/**
* Created by WangLu on 16/9/1.
*/
public class AdapterDemo extends BannerPagerAdapter {

private Context mContext;
private List<Integer> data;

public AdapterDemo(Context context, List data) {
super(context, data);
mContext = context;
this.data = data;
}

/**
* 只需要重写构造和这个方法即可
* 在这里可以设置自己的View,使用自己的图片加载库
*/
@Override
public View setView(int position) {
View v = LayoutInflater.from(mContext).inflate(R.layout.test, null);
ImageView iv = (ImageView) v.findViewById(R.id.iv);
iv.setImageResource(data.get(position));
return v;
}
public class AdapterDemo extends BannerPagerAdapter {

private Context mContext;
private List<Integer> data;

public AdapterDemo(Context context, List data) {
super(context, data);
mContext = context;
this.data = data;
}


/**
* 只需要重写构造和这个方法即可
* 在这里可以设置自己的View,使用自己的图片加载库
*/
@Override
public View setView(int position) {
View v = LayoutInflater.from(mContext).inflate(R.layout.test, null);
ImageView iv = (ImageView) v.findViewById(R.id.iv);
iv.setImageResource(data.get(position));
return v;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
/**
* 判断如果当前的position不是最后一个,那么就设置偏移量来实现被选中小圆点的滑动效果
*/
if (mSelectedDot != null && position != mAdapter.size -1) {
if (mSelectedDot != null && position != mAdapter.size -1 && mAdapter.size != 1) {
float dx = mDotGroup.getChildAt(1).getX() - mDotGroup.getChildAt(0).getX();
mSelectedDot.setTranslationX((position * dx) + positionOffset * dx);
}
Expand All @@ -113,7 +113,7 @@ public void onPageSelected(int position) {
/**
* 如果已经是最后一个,那么则直接把小圆点定位到那,不然滑动效果会出错
*/
if (mSelectedDot != null && position == mAdapter.size - 1) {
if (mSelectedDot != null && position == mAdapter.size - 1&& mAdapter.size != 1) {
float dx = mDotGroup.getChildAt(1).getX() - mDotGroup.getChildAt(0).getX();
mSelectedDot.setTranslationX(position * dx);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public Banner setAdapter(BannerPagerAdapter adapter) {


/**
* 添加到任务栈,当前所有任务完事之后添加已经选中的那个小圆点
* 添加到任务栈,当前所有任务完成之后添加已经选中的小圆点
*/
post(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public BannerPagerAdapter(Context context, List<T> data) {
size = data.size();
}



@Override
public int getCount() {
return BANNER_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 3c9a876

Please sign in to comment.