我有一个包含在RelativeLayout中的numberPicker,下面有一些按钮:
<!-- 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设置为false,如下所示:
mDatePicker.setWrapSelectorWheel(false);
这个RelativeLayout正在通过动画滑入:
//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);
}
一旦完成选择器,用户就会按下一个按钮,该按钮应该折叠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);
}
然后,当按下确定按钮时,我想将拾取器向上滑动。但是在做最轻微的滚动时做一下就会产生ANR。我不知道为什么会这样。我甚至试图强制停止投掷并通过反射调整滚动条。这似乎有点帮助,但它仍然可以冻结应用程序。同时检查滚动状态是否处于空闲状态,并且在视图空闲之前不会折叠视图,仍会出现冻结应用程序的情况。
修改
我刚发现的东西是,当你为高度设置动画时,显然会影响当前的值。这可能是无限循环的原因。我想我也应该注意到我打电话给mDatePicker.setWrapSelectorWheel(false);
。如果不打电话给它,它似乎不会冻结。即使我不想包裹选择轮。