我有一个 numberPicker 包裹在一个 RelativeLayout 中,下面有一些按钮:
<!-- 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);
. 当不调用这个时,它似乎没有冻结。即使我不想包裹选择轮。