1

In a FrameLayout there's a child TextView which can be dragged, but the method onViewReleased and onViewPositionChanged don't work properly(scroll back to the original place) when I try to change text or margin of this TextView while it is scrolling.I'm not sure if I use it right or there's something I should know about.

part of the code:

    ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
        @Override public boolean tryCaptureView(View child, int pointerId) {
          return child == indicator;
        }

        @Override public int clampViewPositionHorizontal(View child, int left, int dx) {
          final int leftBound = getPaddingLeft();
          final int rightBound = getWidth() - indicator.getWidth() - leftBound;
          final int newLeft = Math.min(Math.max(left, leftBound), rightBound);
          return newLeft;
        }

        @Override public void onViewReleased(View releasedChild, float xvel, float yvel) {
          if (indicator == releasedChild) {
            if (releasedChild.getLeft() > ((endPointX - startPointX) / 2)) {
              textView.setText("FRUSTRATION");
              helper.settleCapturedViewAt(endPointX, getPaddingTop());
            } else {
              textView.setText("INDICATOR");
              helper.settleCapturedViewAt(startPointX, getPaddingTop());
            }
            invalidate();
          }
        }
      };
machinezhou
  • 679
  • 2
  • 6
  • 16

1 Answers1

0

It seems every move we take to setText(), setMargin() will cause child's layout reset.So after a lot of experiments, I found we should re-layout the view. Suffice it to say, If you do setText(), setMargin() or like this affecting its layout in onViewReleased() or onViewPositionChanged(), you should save its current position, then invalidate() and then use it with child.layout() when onLayout() trigged.

Hope it will help others.Question closed.

machinezhou
  • 679
  • 2
  • 6
  • 16