2

I have a numberPicker wrapped in a RelativeLayout like so with some buttons underneath:

    <!-- Start/End Picker-->
    <LinearLayout
        android:id="@+id/ll_date_pickers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:background="@android:color/white"
            android:descendantFocusability="blocksDescendants"/>

        <NumberPicker
            android:id="@+id/hour_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:descendantFocusability="blocksDescendants"/>

        <NumberPicker
            android:id="@+id/minute_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:background="@android:color/white"
            android:descendantFocusability="blocksDescendants"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_date_pickers"
        android:background="@color/colorPrimary"
        android:gravity="end"
        android:padding="8dp">

        <TextView
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="@android:string/cancel"
            android:textAllCaps="true"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="48dp"
            android:padding="8dp"
            android:text="@android:string/ok"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="@color/white"/>

    </LinearLayout>
</RelativeLayout>

Wrapping is set to false like so:

mDatePicker.setWrapSelectorWheel(false);

This RelativeLayout is sliding in through an animation:

//View that triggers the animation
mBtnStartDate.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mRlPicker.getVisibility() == View.GONE) {
            BasicAnimations.expand(mRlPicker, mPickerHeight, 200);
        }
        setDatePicker();
    }
});

// Actual animation
public static void expand(final View v, final int targetHeight, int duration) {
    v.getLayoutParams().height = 1;
    v.requestLayout();
    v.setVisibility(View.VISIBLE);

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height =
                    interpolatedTime == 1
                            ? ViewGroup.LayoutParams.WRAP_CONTENT
                            : ((int) (targetHeight * interpolatedTime)) == 0 ? 1 : ((int) (targetHeight * interpolatedTime));
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration(duration);
    v.startAnimation(a);
}

Once done with the pickers the user hits a button which should collapse the RelativeLayout:

mBtnOk.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        BasicAnimations.collapse(mRlPicker, 200);
    }
});

public static void collapse(final View v, int duration) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration(duration);
    v.startAnimation(a);
}

Then when hitting the ok button I want to slide the pickers back up. But doing to while doing the slightest scrolling will result in an ANR. I have no clue why this is happening. I've even tried to force stop the fling and adjust scrollers through reflection. That seemed to help a little but it was still possible to freeze the app. Also checking if the scroll state was idle and not collapsing the view until it was idle, still gave occurrences of freezing the app.

enter image description here

EDIT:

Something I just found out, is that when you animate the height, apparently influences the current value. This might be the cause of a infinite loop. I guess I should also have noted that I called mDatePicker.setWrapSelectorWheel(false);. When not calling this, it doesn't seem to freeze. Even tho I would like to not wrap the selector wheel.

Raymond
  • 2,276
  • 2
  • 20
  • 34

0 Answers0