Skip to content

Commit

Permalink
默认禁用循环滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed Jan 9, 2017
1 parent 23405b7 commit c7296c6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
20 changes: 12 additions & 8 deletions app/src/main/java/cn/qqtheme/androidpicker/NestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void onSelected(boolean isUserScroll, int index, String item) {
});

picker = new CarNumberPicker(this);
picker.setLineConfig(new WheelView.LineConfig(0));
picker.setOffset(3);
picker.setOnWheelListener(new CarNumberPicker.OnWheelListener() {
@Override
Expand All @@ -58,14 +57,19 @@ public void onSecondWheeled(int index, String item) {
});
ViewGroup viewGroup = findView(R.id.wheelview_container);
viewGroup.addView(picker.getContentView());
}

public void onBack(View view) {
finish();
}

public void onCarNumber(View view) {
picker.show();
findViewById(R.id.nest_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
findViewById(R.id.nest_carnumber).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
picker.show();
}
});
}

}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ com.android.build.gradle.overridePathCheck=true
#android.useDeprecatedNdk=true

#下面定义一些常量供gradle脚本使用
VERSION_NAME=1.4.1
VERSION_CODE=141
VERSION_NAME=1.4.2
VERSION_CODE=142
COMPILE_SDK_VERSION=23
MIN_SDK_VERSION=11
#安卓6.0开始(API23+),需要动态申请权限
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class WheelPicker extends ConfirmPopup<View> {
protected int textColorNormal = WheelView.TEXT_COLOR_NORMAL;
protected int textColorFocus = WheelView.TEXT_COLOR_FOCUS;
protected int offset = WheelView.ITEM_OFF_SET;
protected boolean cycleDisable = false;
protected boolean cycleDisable = true;
protected WheelView.LineConfig lineConfig;
private View contentView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.Size;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand Down Expand Up @@ -57,14 +56,14 @@ public class WheelView extends ListView implements ListView.OnScrollListener, Vi
public static final int SMOOTH_SCROLL_DURATION = 50;//ms

public static final int TEXT_SIZE = 16;//sp
public static final float TEXT_ALPHA = 0.7f;
public static final float TEXT_ALPHA = 0.8f;
public static final int TEXT_COLOR_FOCUS = 0XFF0288CE;
public static final int TEXT_COLOR_NORMAL = 0XFFBBBBBB;

public static final int ITEM_OFF_SET = 2;
public static final int ITEM_HEIGHT = 45;//dp
public static final int ITEM_PADDING_TOP_BOTTOM = 8;//dp
public static final int ITEM_PADDING_LEFT_RIGHT = 15;//dp
public static final int ITEM_HEIGHT = 40;//dp
public static final int ITEM_PADDING_TOP_BOTTOM = 5;//dp
public static final int ITEM_PADDING_LEFT_RIGHT = 10;//dp
public static final int ITEM_MARGIN = 5;//dp
public static final int ITEM_TAG_IMAGE = 100;
public static final int ITEM_TAG_TEXT = 101;
Expand All @@ -76,7 +75,7 @@ public class WheelView extends ListView implements ListView.OnScrollListener, Vi
private static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

private int itemHeight = 0; // 每一项高度
private int itemHeightPixels = 0; // 每一项高度
private int currentPosition = -1; // 记录滚轮当前刻度
private WheelAdapter adapter = new WheelAdapter();
private OnWheelListener onWheelListener;
Expand Down Expand Up @@ -130,13 +129,13 @@ public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
int childCount = getChildCount();
if (childCount > 0 && itemHeight == 0) {
itemHeight = getChildAt(0).getHeight();
LogUtils.verbose(this, "itemHeight=" + itemHeight);
if (itemHeight != 0) {
if (childCount > 0 && itemHeightPixels == 0) {
itemHeightPixels = getChildAt(0).getHeight();
LogUtils.verbose(this, "itemHeightPixels=" + itemHeightPixels);
if (itemHeightPixels != 0) {
int wheelSize = adapter.getWheelSize();
ViewGroup.LayoutParams params = getLayoutParams();
params.height = itemHeight * wheelSize;
params.height = itemHeightPixels * wheelSize;
refreshVisibleItems(getFirstVisiblePosition(),
getCurrentPosition() + wheelSize / 2, wheelSize / 2);
changeBackground();
Expand All @@ -155,9 +154,9 @@ private void changeBackground() {
lineConfig = new LineConfig();
}
lineConfig.setWidth(getWidth());
lineConfig.setHeight(itemHeight * wheelSize);
lineConfig.setHeight(itemHeightPixels * wheelSize);
lineConfig.setWheelSize(wheelSize);
lineConfig.setItemHeight(itemHeight);
lineConfig.setItemHeight(itemHeightPixels);
Drawable drawable;
WheelDrawable holoWheelDrawable = new HoloWheelDrawable(lineConfig);
if (lineConfig.isShadowVisible()) {
Expand All @@ -178,16 +177,6 @@ private void changeBackground() {
}
}

/**
* 设置滚轮是否循环滚动
*/
public void setLoop(boolean loop) {
if (loop != adapter.isLoop()) {
setSelection(0);
adapter.setLoop(loop);
}
}

private void _setItems(List<String> list) {
if (null == list || list.size() == 0) {
throw new IllegalArgumentException("data are empty");
Expand Down Expand Up @@ -259,6 +248,9 @@ public void setOffset(@IntRange(from = 1, to = 3) int offset) {
adapter.setWheelSize(wheelSize);
}

/**
* 设置滚轮是否禁用循环滚动
*/
public void setCycleDisable(boolean cycleDisable) {
adapter.setLoop(!cycleDisable);
}
Expand Down Expand Up @@ -353,15 +345,15 @@ private int getSmoothDistance(float scrollDistance) {
}

private void refreshCurrentPosition() {
if (getChildAt(0) == null || itemHeight == 0) {
if (getChildAt(0) == null || itemHeightPixels == 0) {
return;
}
int firstPosition = getFirstVisiblePosition();
if (adapter.isLoop() && firstPosition == 0) {
return;
}
int position;
if (Math.abs(getChildAt(0).getY()) <= itemHeight / 2) {
if (Math.abs(getChildAt(0).getY()) <= itemHeightPixels / 2) {
position = firstPosition;
} else {
position = firstPosition + 1;
Expand Down Expand Up @@ -445,14 +437,14 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
}
float deltaY = itemView.getY();
// fixed: 17-1-7 Equality tests should not be made with floating point values.
if ((int) deltaY == 0 || itemHeight == 0) {
if ((int) deltaY == 0 || itemHeightPixels == 0) {
return;
}
if (Math.abs(deltaY) < itemHeight / 2) {
if (Math.abs(deltaY) < itemHeightPixels / 2) {
int d = getSmoothDistance(deltaY);
smoothScrollBy(d, SMOOTH_SCROLL_DURATION);
} else {
int d = getSmoothDistance(itemHeight + deltaY);
int d = getSmoothDistance(itemHeightPixels + deltaY);
smoothScrollBy(d, SMOOTH_SCROLL_DURATION);
}
}
Expand Down Expand Up @@ -772,7 +764,7 @@ public View bindView(int position, View convertView, ViewGroup parent) {
} else {
itemView = (WheelView.ItemView) convertView;
}
itemView.setText(getItem(position));
itemView.setText(data.get(position));
//itemView.setImage(...);
return itemView;
}
Expand Down

0 comments on commit c7296c6

Please sign in to comment.