diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index f0f7ea5..65ee143 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/custom_segment/SegmentViewHolderImpl.java b/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/custom_segment/SegmentViewHolderImpl.java index a5e9c1e..039b56d 100644 --- a/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/custom_segment/SegmentViewHolderImpl.java +++ b/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/custom_segment/SegmentViewHolderImpl.java @@ -18,7 +18,7 @@ import static segmented_control.widget.custom.android.com.segmentedcontrol.utils.Utils.createRadius; import static segmented_control.widget.custom.android.com.segmentedcontrol.utils.Utils.defineRadiusForPosition; import static segmented_control.widget.custom.android.com.segmentedcontrol.utils.Utils.getBackground; -import static segmented_control.widget.custom.android.com.segmentedcontrol.utils.Utils.isInViewBounds; +import static segmented_control.widget.custom.android.com.segmentedcontrol.utils.Utils.isInBounds; /** * Created by Robert Apikyan on 9/8/2017. @@ -29,6 +29,7 @@ public class SegmentViewHolderImpl extends SegmentViewHolder { private TextView itemTV; private float[] radius; private ValueAnimator va; + private int[] windowLocation; private final ValueAnimator.AnimatorUpdateListener bgAnimListener = new ValueAnimator.AnimatorUpdateListener() { @Override @@ -55,7 +56,9 @@ public boolean onTouch(View v, MotionEvent event) { event.getAction() == MotionEvent.ACTION_POINTER_UP || event.getAction() == MotionEvent.ACTION_OUTSIDE) { - if (!isInViewBounds(event.getX(), event.getY(), getSectionView())) { + if (!isInBounds(event.getX(), event.getY(), windowLocation[0], windowLocation[1], + getSectionView().getMeasuredWidth(), getSectionView().getMeasuredHeight())) { + setBackground(isSelected() ? getSelectedBackground() : getUnSelectedBackground()); } } @@ -72,6 +75,7 @@ public SegmentViewHolderImpl(@NonNull View sectionView) { @Override protected void onSegmentBind(CharSequence segmentData) { + initScreenLocation(); itemTV.setText(segmentData); if (isRadiusForEverySegment()) { radius = createRadius(getTopLeftRadius(), getTopRightRadius(), getBottomRightRadius(), getBottomLeftRadius()); @@ -87,6 +91,11 @@ protected void onSegmentBind(CharSequence segmentData) { ViewGroup.MarginLayoutParams.class.cast(itemTV.getLayoutParams()).setMargins(getSegmentHorizontalMargin(), getSegmentVerticalMargin(), getSegmentHorizontalMargin(), getSegmentVerticalMargin()); } + private void initScreenLocation() { + windowLocation = new int[2]; + getSectionView().getLocationOnScreen(windowLocation); + } + @Override public void onSegmentSelected(boolean isSelected, boolean isReselected) { super.onSegmentSelected(isSelected, isReselected); diff --git a/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/utils/Utils.java b/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/utils/Utils.java index 3b83d22..571f82d 100644 --- a/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/utils/Utils.java +++ b/segmentedcontrolmodule/src/main/java/segmented_control/widget/custom/android/com/segmentedcontrol/utils/Utils.java @@ -176,8 +176,8 @@ public static ValueAnimator createBackgroundAnimation(int argbStart, int argbEnd return ValueAnimator.ofObject(ArgbEvaluator.getInstance(), argbStart, argbEnd); } - public static boolean isInViewBounds(float touchX, float touchY, View view){ - return touchX >= view.getX() && touchX <= view.getX()+view.getMeasuredWidth() && // in x bounds - touchY >= view.getY() && touchY <=view.getY() + view.getMeasuredHeight(); + public static boolean isInBounds(float touchX, float touchY, float viewX, float viewY, float viewW, float viewH) { + return touchX >= viewX && touchX <= viewX + viewW && // in x bounds + touchY >= viewY && touchY <= viewY + viewH; } }