如何在放置在scrollview中时滚动DatePicker

时间:2017-02-17 09:18:15

标签: android datepicker android-scrollview

基本上我无法在datepicker中滚动月份和年份,因为scrollview是滚动的。

<DatePicker
            android:layout_width="match_parent"
            android:id="@+id/datePicker5"
            android:layout_weight="3.61"
            android:layout_height="match_parent" />

我需要添加什么?感谢

我也有这个代码btw。

private void addPet(){
    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c);
    View mView = layoutInflaterAndroid.inflate(R.layout.add_pet_dialog, null);
    AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(c);
    alertDialogBuilderUserInput.setView(mView);


    final EditText userInputDialogEditText = (EditText) mView.findViewById(R.id.pet_name);
    final DatePicker date_picker = (DatePicker) mView.findViewById(R.id.datepick);
    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;
        }
    });



        alertDialogBuilderUserInput
            .setCancelable(false)
            .setPositiveButton("Send", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogBox, int id) {
                    // ToDo get user input here
                }
            })

            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogBox, int id) {
                            dialogBox.cancel();
                        }
                    });

    AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
    alertDialogAndroid.show();


}



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();
    }
}

我不知道为什么它仍然不起作用帮助。

1 个答案:

答案 0 :(得分:1)

在另一个可滚动视图中包含可滚动视图不是一个好习惯。但是,要解决此问题,您必须在用户与日期选择器交互时禁用父滚动视图,如下所示 -

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();
    }
}