Android : How to solve DatePicker,TimePicker,.. scroll problem when using ScrollView

Par kneves, le 19 juillet 20126 commentaires

When running application on Android 2.1 to 3.1, the DatePicker and TimePicker are using the old-style display where you can either enter the value or press the +/- as shown below :

DatePicker Android < 3.2

Running applications on Android 3.2 or higher versions , the DatePicker are the new ones where you can also scroll on the day, the month and the year.

DatePicker Android < 3.2


The problem that occurs in these versions of Android is when you use a DatePicker in a ScrollView and try so scroll down for example the day from 30 to 10.
At first, it will be fine and the scrolling will be smoothly. But then, it simply blocks, and when you try to scroll/swipe again, only a few pixels will move.
The reason of this problem is simple : the ScrollView is intercepting and stealing the touch events once your finger moves outside of the Date/Time Picker controls. A simple work-around is to create a subclass of DatePicker where you prevent the parent from stealing events after the initial touch down on your Picker. Here is the code :
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.DatePicker;

public class CustomDatePicker extends DatePicker
{
    public CustomDatePicker(Context context, AttributeSet attrs, int
defStyle)
    {
        super(context, attrs, defStyle);
    }

    public CustomDatePicker(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public CustomDatePicker(Context context)
    {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        /* Prevent parent controls from stealing our events once we've
gotten a touch down */
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
        {
            ViewParent p = getParent();
            if (p != null)
                p.requestDisallowInterceptTouchEvent(true);
        }

        return false;
    }
}

Now, you can use your CustomDatePicker in your Java classes. If you want to use it directly in your XML layout, you can do it using custom view :
 
If you have any questions, I would be happy to answer :)


Source
Mots clés : Android, Tutoriel
Classé dans : Android, Tutos

Commentaires

#1 • Le 10 septembre 2012 à 22:56, hotel paris a dit :

Merci pour cette belle serie.

#2 • Le 23 octobre 2012 à 10:42, cheap 10 tablet pc a dit :

4026 Products – cheap 10 tablet pc, Source Cheap 10 Mid Tablet Pc Products at Tablet PC, Laptops from Manufacturers and Suppliers around the World ...

#3 • Le 28 novembre 2012 à 00:39, Pont a dit :

if you don't want to subclass the date/time picker. You can do following:
public dp = new DatePicker(this){
@Override
public boolean onInterceptTouchEvent(MotionEvent evt){
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}

}
};

#4 • Le 28 février 2013 à 04:58, pipo u1 a dit :

This is a good article, the content is very rich, and I really like, I will continue to support you!

#5 • Le 28 février 2013 à 06:23, haier w718 a dit :

This is a good article, the content is very rich, and I really like, I will continue to support you!

#6 • Le 07 mars 2013 à 04:08, ds708 a dit :

Genuine Autel MaxiDas DS708 Professional . 12 months manufacturers warranty and 12 months FREE software updates from the Autel manufacturers website

Ecrire un commentaire




Quelle est la quatrième lettre du mot kofotl