Android 如何在滚动视图中滚动日期选择器
基本上,我不能在日期选择器中滚动月份和年份,因为滚动视图是滚动的视图Android 如何在滚动视图中滚动日期选择器,android,datepicker,android-scrollview,Android,Datepicker,Android Scrollview,基本上,我不能在日期选择器中滚动月份和年份,因为滚动视图是滚动的视图 <DatePicker android:layout_width="match_parent" android:id="@+id/datePicker5" android:layout_weight="3.61" android:layout_height="match_parent" /> 我不知道为什么它仍然不起作
<DatePicker
android:layout_width="match_parent"
android:id="@+id/datePicker5"
android:layout_weight="3.61"
android:layout_height="match_parent" />
我不知道为什么它仍然不起作用,请帮助。在另一个可滚动视图中有一个可滚动视图不是一个好的做法。然而,要解决这个问题,您必须在用户与日期选择器交互时禁用父scrollview,如下所示-
date_picker.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Disallow the touch request for parent scroll on touch of datepicker view
requestDisallowParentInterceptTouchEvent(v, true);
}
else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
// Re-allows parent events
requestDisallowParentInterceptTouchEvent(v, false);
}
return false;
}
});
private void requestDisallowParentInterceptTouchEvent(View v, Boolean disallowIntercept) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (v.getParent() instanceof ScrollView) {
v.getParent().requestDisallowInterceptTouchEvent(disallowIntercept);
}
v = (View) v.getParent();
}
}
@DOOM博士你能分享你的完整xml文件吗?
date_picker.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Disallow the touch request for parent scroll on touch of datepicker view
requestDisallowParentInterceptTouchEvent(v, true);
}
else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
// Re-allows parent events
requestDisallowParentInterceptTouchEvent(v, false);
}
return false;
}
});
private void requestDisallowParentInterceptTouchEvent(View v, Boolean disallowIntercept) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (v.getParent() instanceof ScrollView) {
v.getParent().requestDisallowInterceptTouchEvent(disallowIntercept);
}
v = (View) v.getParent();
}
}