Browse Source

[공통][New] 커스텀 인디케이터 index 계산 offset 조정
- itemWidth / 2 정도 밀려 있었음

hyodong.min 6 years ago
parent
commit
4520d38c22

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/category/banner/MainBannerView.java

@@ -64,7 +64,7 @@ public class MainBannerView extends ConstraintLayout {
       }
     };
     recyclerView.setLayoutManager(layoutManager);
-    customIndicator.setIndicatorSetting(recyclerView, R.color.C4DFFFFFF, R.color.CFFFFFF, event -> {
+    customIndicator.setIndicator(recyclerView, R.color.C4DFFFFFF, R.color.CFFFFFF, event -> {
       recyclerView.smoothScrollToPosition(event.getIndex());
     });
 

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/recommend/RecommendContentsView.java

@@ -130,7 +130,7 @@ public class RecommendContentsView extends ConstraintLayout {
   public void draw(Context context, List<? extends LifeplusContentsBean> withItemList, IEventListener listener) {
     List<? extends LifeplusContentsBean> withItemList1 = withItemList;
 
-    customIndicator.setIndicatorSetting(recyclerViewRecommendContents, R.color.C19000000, R.color.C000000, event -> {
+    customIndicator.setIndicator(recyclerViewRecommendContents, R.color.C19000000, R.color.C000000, event -> {
       Log.e("APP#  RecommendContentsView | onEvent", "|" + event.getIndex());
       recyclerViewRecommendContents.smoothScrollToPosition(event.getIndex());
     });

+ 29 - 19
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/event/CustomIndicator.java

@@ -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);

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/event/EventListFragment.java

@@ -105,7 +105,7 @@ public class EventListFragment extends FragmentBase<EventListPresenter> implemen
   public void draw(List<EventBean> eventBeans) {
 //    EventListAdapter adapter = new EventListAdapter(getActivity(), eventBeans, event -> presenter.onEvent(event));
 //    recyclerViewEvent.setAdapter(adapter);
-//    customIndicator.setIndicatorSetting(recyclerViewEvent, R.color.C19000000, R.color.C000000);
+//    customIndicator.setIndicator(recyclerViewEvent, R.color.C19000000, R.color.C000000);
 //
 //    if (eventBeans.size() == 1) {
 //      setVisibleMyEventButton(true);