If you think the Android project vitdroid-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.googlecode.android.widgets.DateSlider;
//fromwww.java2s.comimport java.util.Calendar;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
/**
* This is a container class for ScrollLayouts. It coordinates the scrolling
* between them, so that if one is scrolled, the others are scrolled to
* keep a consistent display of the time. It also notifies an optional
* observer anytime the time is changed.
*/publicclass SliderContainer extends LinearLayout {
private Calendar mTime = null;
private OnTimeChangeListener mOnTimeChangeListener;
privateint minuteInterval;
public SliderContainer(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
}
@Override
protectedvoid onFinishInflate() {
finalint childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (v instanceof ScrollLayout) {
final ScrollLayout sl = (ScrollLayout)v;
sl.setOnScrollListener(
new ScrollLayout.OnScrollListener() {
publicvoid onScroll(long x) {
mTime.setTimeInMillis(x);
arrangeScrollers(sl);
}
});
}
}
}
/**
* Set the current time and update all of the child ScrollLayouts accordingly.
*
* @param calendar
*/publicvoid setTime(Calendar calendar) {
mTime = Calendar.getInstance(calendar.getTimeZone());
mTime.setTimeInMillis(calendar.getTimeInMillis());
arrangeScrollers(null);
}
/**
* Get the current time
*
* @return The current time
*/public Calendar getTime() {
return mTime;
}
/**
* sets the minimum date that the scroller can scroll
*
* @param c the minimum date (inclusiv)
*/publicvoid setMinTime(Calendar c) {
if (mTime==null) {
thrownew RuntimeException("You have to call setTime before setting a MinimumTime!");
}
finalint childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (v instanceof ScrollLayout) {
ScrollLayout scroller = (ScrollLayout)v;
scroller.setMinTime(c.getTimeInMillis());
}
}
}
/**
* sets the maximum date that the scroller can scroll
*
* @param c the maximum date (inclusive)
*/publicvoid setMaxTime(Calendar c) {
if (mTime==null) {
thrownew RuntimeException("You have to call setTime before setting a MinimumTime!");
}
finalint childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (v instanceof ScrollLayout) {
ScrollLayout scroller = (ScrollLayout)v;
scroller.setMaxTime(c.getTimeInMillis());
}
}
}
/**
* sets the minute interval of the scroll layouts.
* @param minInterval
*/publicvoid setMinuteInterval(int minInterval) {
this.minuteInterval = minInterval;
finalint childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (v instanceof ScrollLayout) {
ScrollLayout scroller = (ScrollLayout)v;
scroller.setMinuteInterval(minInterval);
}
}
}
/**
* Sets the OnTimeChangeListener, which will be notified anytime the time is
* set or changed.
*
* @param l
*/publicvoid setOnTimeChangeListener(OnTimeChangeListener l) {
mOnTimeChangeListener = l;
}
/**
* Pushes our current time into all child ScrollLayouts, except the source
* of the time change (if specified)
*
* @param source The ScrollLayout that generated the time change, or null if
* this isn't the result of a ScrollLayout-generated time change.
*/privatevoid arrangeScrollers(ScrollLayout source) {
finalint childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (v == source) {
continue;
}
if (v instanceof ScrollLayout) {
ScrollLayout scroller = (ScrollLayout)v;
scroller.setTime(mTime.getTimeInMillis());
}
}
if (mOnTimeChangeListener != null) {
if (minuteInterval>1) {
int minute = mTime.get(Calendar.MINUTE)/minuteInterval*minuteInterval;
mTime.set(Calendar.MINUTE, minute);
}
mOnTimeChangeListener.onTimeChange(mTime);
}
}
publicstaticinterface OnTimeChangeListener {
publicvoid onTimeChange(Calendar time);
}
}