|
|
@@ -45,11 +45,8 @@ public class CustomIndicator extends View {
|
|
|
private int activeColor;
|
|
|
private int inActiveColor;
|
|
|
private int activePosition;
|
|
|
- private float totalLength;
|
|
|
- private float indicatorTotalWidth;
|
|
|
private float indicatorStartX;
|
|
|
private int itemCount;
|
|
|
- private boolean isDispose;
|
|
|
|
|
|
private static final float DP = Resources.getSystem().getDisplayMetrics().density;
|
|
|
private final int mIndicatorHeight = (int) (DP * 16);
|
|
|
@@ -59,19 +56,33 @@ public class CustomIndicator extends View {
|
|
|
private final Paint mPaint = new Paint();
|
|
|
private IEventListener listener;
|
|
|
|
|
|
- public void setIndicatorSetting(RecyclerView recyclerview, int inActiveColor, int activeColor, IEventListener listener) {
|
|
|
+ /**
|
|
|
+ * 설정
|
|
|
+ *
|
|
|
+ * @param recyclerview
|
|
|
+ * @param inActiveColor
|
|
|
+ * @param activeColor
|
|
|
+ * @param listener
|
|
|
+ */
|
|
|
+ public void setIndicator(RecyclerView recyclerview, int inActiveColor, int activeColor, IEventListener listener) {
|
|
|
this.recyclerView = recyclerview;
|
|
|
this.inActiveColor = inActiveColor;
|
|
|
this.activeColor = activeColor;
|
|
|
this.listener = listener;
|
|
|
+ mPaint.setAntiAlias(true);
|
|
|
mPaint.setStrokeCap(Paint.Cap.ROUND);
|
|
|
mPaint.setStrokeWidth(mIndicatorStrokeWidth);
|
|
|
mPaint.setStyle(Paint.Style.FILL);
|
|
|
this.invalidate();
|
|
|
}
|
|
|
|
|
|
- public void onChangedIndex(int firstVisibleItemPosition) {
|
|
|
- activePosition = firstVisibleItemPosition;
|
|
|
+ /**
|
|
|
+ * 인덱스 변경
|
|
|
+ *
|
|
|
+ * @param index
|
|
|
+ */
|
|
|
+ public void onChangedIndex(int index) {
|
|
|
+ activePosition = index;
|
|
|
invalidate();
|
|
|
}
|
|
|
|
|
|
@@ -82,10 +93,10 @@ public class CustomIndicator extends View {
|
|
|
}
|
|
|
|
|
|
itemCount = recyclerView.getAdapter().getItemCount();
|
|
|
- totalLength = mIndicatorItemLength * itemCount;
|
|
|
+ float totalLength = mIndicatorItemLength * itemCount;
|
|
|
float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
|
|
|
- indicatorTotalWidth = totalLength + paddingBetweenItems; //인디케이터 width
|
|
|
- indicatorStartX = (recyclerView.getWidth() - indicatorTotalWidth) / 2F; //중앙
|
|
|
+ float indicatorTotalWidth = totalLength + paddingBetweenItems;
|
|
|
+ indicatorStartX = (recyclerView.getWidth() - indicatorTotalWidth) / 2F; // 시작점
|
|
|
float indicatorPosY = getHeight() - mIndicatorHeight / 2F;
|
|
|
|
|
|
drawInactiveIndicators(canvas, indicatorStartX, indicatorPosY, itemCount);
|
|
|
@@ -100,7 +111,7 @@ public class CustomIndicator extends View {
|
|
|
float itemWidth = mIndicatorItemLength + mIndicatorItemPadding; //한칸
|
|
|
|
|
|
float start = indicatorStartX;
|
|
|
- for (int i = 0; i < itemCount; i++) {
|
|
|
+ for (int i = 0; i < itemCount; ++i) {
|
|
|
// itemCount 1은 안띄움
|
|
|
if (itemCount != 1) {
|
|
|
c.drawCircle(start, indicatorPosY, DP * 3, mPaint);
|
|
|
@@ -109,14 +120,13 @@ public class CustomIndicator extends View {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void drawHighlights(Canvas canvas, float indicatorStartX, float indicatorPosY,
|
|
|
- int highlightPosition, int itemCount) {
|
|
|
+ private void drawHighlights(Canvas canvas, float indicatorStartX, float indicatorPosY, int highlightPosition, int itemCount) {
|
|
|
mPaint.setColor(context.getResources().getColor(activeColor));
|
|
|
|
|
|
final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
|
|
|
float start = indicatorStartX;
|
|
|
- for (int i = 0; i < itemCount; i++) {
|
|
|
+ for (int i = 0; i < itemCount; ++i) {
|
|
|
if (itemCount != 1 && i == highlightPosition) {
|
|
|
canvas.drawCircle(start, indicatorPosY, DP * 3, mPaint);
|
|
|
}
|
|
|
@@ -126,15 +136,15 @@ public class CustomIndicator extends View {
|
|
|
|
|
|
@Override
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
- if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
|
|
+ int action = event.getAction() & MotionEvent.ACTION_MASK;
|
|
|
+ if (action == MotionEvent.ACTION_DOWN) {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
- else if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
|
|
- float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;
|
|
|
- int index = (int) Math.floor((event.getX() - indicatorStartX) / itemWidth); //인디케이터 안에서 찍힌 x 좌표
|
|
|
+ else if (action == MotionEvent.ACTION_UP) {
|
|
|
+ int itemWidth = (int) (mIndicatorItemLength + mIndicatorItemPadding);
|
|
|
+ int index = (int) Math.floor((event.getX() - indicatorStartX + (itemWidth >> 1)) / itemWidth); //인디케이터 안에서 찍힌 x 좌표
|
|
|
if (index >= 0 && index < itemCount) {
|
|
|
- listener.onEvent(new Event.Builder(Event.CLICK).index((int) index).build());
|
|
|
+ listener.onEvent(new Event.Builder(Event.CLICK).index(index).build());
|
|
|
}
|
|
|
}
|
|
|
return super.onTouchEvent(event);
|