Skip to content

添加单选模式。可以隐藏右上角的隐藏按钮。 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void setOnDateSelected(OnDateSelected onDateSelected) {
titleView.setOnDateSelected(onDateSelected, monthView);
}

/**
* 监听选择的日期发生变化
* @param onDateSelected
*/
public void setOnDateChanged(OnDateSelected onDateSelected) {
monthView.setOnDateChanged(onDateSelected);
}

public void hideEnsureButton() {
titleView.hideEnsureButton();
}

@Override
public void setColor(int color) {
titleView.setColor(color);
Expand All @@ -52,4 +64,8 @@ public void setColor(int color) {
public void isLunarDisplay(boolean display) {
monthView.setLunarShow(display);
}

public void setSingleMonth(boolean t) {
monthView.setSingleSelected(t);
}
}
173 changes: 121 additions & 52 deletions DatePicker/src/main/java/cn/aigestudio/datepicker/views/MonthView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import cn.aigestudio.datepicker.bizs.CalendarBiz;
import cn.aigestudio.datepicker.entities.BGCircle;
import cn.aigestudio.datepicker.interfaces.OnDateSelected;

/**
* 月视图
Expand Down Expand Up @@ -69,6 +70,7 @@ public class MonthView extends View implements ValueAnimator.AnimatorUpdateListe
private Map<String, BGCircle> circlesAppear = new HashMap<>();
private Map<String, BGCircle> circlesDisappear = new HashMap<>();
private List<String> dateSelected = new ArrayList<>();
private OnDateSelected mOnDateSelected;

private enum EventType {
SINGLE, MULTIPLE
Expand Down Expand Up @@ -135,6 +137,18 @@ public MonthView(Context context, AttributeSet attrs, int defStyleAttr) {
buildCalendarRegion();
}

/**
* 设置日期是多选还是单选
* @param t
*/
public void setSingleSelected(boolean t) {
if (t) {
mEventType = EventType.SINGLE;
} else {
mEventType = EventType.MULTIPLE;
}
}

/**
* 设置页面改变时的监听器
*
Expand Down Expand Up @@ -249,6 +263,104 @@ private BGCircle createCircle(float x, float y) {
return circle1;
}

/**
* 点击选择日期后,添加或者删除
* @param region
* @param date
*/
private void dateSelect(Region region, final String date) {
if (mEventType == EventType.SINGLE) {
int size = dateSelected.size();
if (!dateSelected.contains(date)) {
for (int i = 0; i < size; i++) {
removeDateSelected(region, dateSelected.get(i));
}
addDateSelected(region, date);
} else {
for (int i = 0; i < size; i++) {
String nDate = dateSelected.get(i);
if (nDate.equals(date)) {
continue;
}
removeDateSelected(region, nDate);
}
}
} else {
if (dateSelected.contains(date)) {
removeDateSelected(region, date);
} else {
addDateSelected(region, date);
}
}
}

/**
* 删除一个已经选择的日期
* @param region
* @param date
*/
private void removeDateSelected(Region region, final String date) {
if (dateSelected.contains(date)) {
dateSelected.remove(date);

BGCircle circle = circlesAppear.get(date);

ValueAnimator animScale = ObjectAnimator.ofInt(circle, "radius", circleRadius, 0);
animScale.setDuration(250);
animScale.setInterpolator(new AccelerateInterpolator());
animScale.addUpdateListener(this);
animScale.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
circlesDisappear.remove(date);
}
});
animScale.start();

circlesDisappear.put(date, circle);

circlesAppear.remove(date);
}
}

/**
* 添加日期到选择的列表里面
* @param region
* @param date
*/
private void addDateSelected(Region region, final String date) {
dateSelected.add(date);

BGCircle circle = createCircle(region.getBounds().centerX() + index * sizeBase, region.getBounds().centerY());

ValueAnimator animScale1 = ObjectAnimator.ofInt(circle, "radius", 0, animZoomOut1);
animScale1.setDuration(250);
animScale1.setInterpolator(new DecelerateInterpolator());
animScale1.addUpdateListener(this);

ValueAnimator animScale2 = ObjectAnimator.ofInt(circle, "radius", animZoomOut1, animZoomIn1);
animScale2.setDuration(100);
animScale2.setInterpolator(new AccelerateInterpolator());
animScale2.addUpdateListener(this);

ValueAnimator animScale3 = ObjectAnimator.ofInt(circle, "radius", animZoomIn1, animZoomOut2);
animScale3.setDuration(150);
animScale3.setInterpolator(new DecelerateInterpolator());
animScale3.addUpdateListener(this);

ValueAnimator animScale4 = ObjectAnimator.ofInt(circle, "radius", animZoomOut2, circleRadius);
animScale4.setDuration(50);
animScale4.setInterpolator(new AccelerateInterpolator());
animScale4.addUpdateListener(this);

AnimatorSet animSet = new AnimatorSet();
animSet.playSequentially(animScale1, animScale2, animScale3, animScale4);

animSet.start();

circlesAppear.put(date, circle);
}

private void defineContainRegion(int x, int y) {
for (int i = 0; i < mRegion.length; i++) {
for (int j = 0; j < mRegion[i].length; j++) {
Expand All @@ -267,58 +379,7 @@ private void defineContainRegion(int x, int y) {
}
final String date = currentYear + "-" + currentMonth + "-" + mCalendarBiz.getGregorianCreated().get(index)[i][j];

if (dateSelected.contains(date)) {
dateSelected.remove(date);

BGCircle circle = circlesAppear.get(date);

ValueAnimator animScale = ObjectAnimator.ofInt(circle, "radius", circleRadius, 0);
animScale.setDuration(250);
animScale.setInterpolator(new AccelerateInterpolator());
animScale.addUpdateListener(this);
animScale.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
circlesDisappear.remove(date);
}
});
animScale.start();

circlesDisappear.put(date, circle);

circlesAppear.remove(date);
} else {
dateSelected.add(date);

BGCircle circle = createCircle(region.getBounds().centerX() + index * sizeBase, region.getBounds().centerY());

ValueAnimator animScale1 = ObjectAnimator.ofInt(circle, "radius", 0, animZoomOut1);
animScale1.setDuration(250);
animScale1.setInterpolator(new DecelerateInterpolator());
animScale1.addUpdateListener(this);

ValueAnimator animScale2 = ObjectAnimator.ofInt(circle, "radius", animZoomOut1, animZoomIn1);
animScale2.setDuration(100);
animScale2.setInterpolator(new AccelerateInterpolator());
animScale2.addUpdateListener(this);

ValueAnimator animScale3 = ObjectAnimator.ofInt(circle, "radius", animZoomIn1, animZoomOut2);
animScale3.setDuration(150);
animScale3.setInterpolator(new DecelerateInterpolator());
animScale3.addUpdateListener(this);

ValueAnimator animScale4 = ObjectAnimator.ofInt(circle, "radius", animZoomOut2, circleRadius);
animScale4.setDuration(50);
animScale4.setInterpolator(new AccelerateInterpolator());
animScale4.addUpdateListener(this);

AnimatorSet animSet = new AnimatorSet();
animSet.playSequentially(animScale1, animScale2, animScale3, animScale4);

animSet.start();

circlesAppear.put(date, circle);
}
dateSelect(region, date);
}
}
}
Expand Down Expand Up @@ -388,6 +449,10 @@ public boolean onTouchEvent(MotionEvent event) {
}
} else {
defineContainRegion((int) event.getX(), (int) event.getY());
// 调用外部设置的监听器
if (mOnDateSelected != null) {
mOnDateSelected.selected(dateSelected);
}
}
break;
}
Expand Down Expand Up @@ -542,4 +607,8 @@ private void drawMonth(Canvas canvas, float offsetX, int year, int month) {
mCalendarBiz.getGregorianCreated().put(current, gregorianCurrent);
canvas.restore();
}

public void setOnDateChanged(OnDateSelected onDateSelected) {
mOnDateSelected = onDateSelected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public void onSizeChanged(int size) {
tvConfirm.setPadding(0, padding, padding, padding);
tvConfirm.getPaint().setTextSize(textSizeSmall);
}

/**
* 隐藏确定按钮
*/
public void hideEnsureButton() {
tvConfirm.setVisibility(View.INVISIBLE);
}
}
28 changes: 24 additions & 4 deletions Demo/src/main/java/cn/aigestudio/datepicker/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @author AigeStudio 2015-03-26
*/
public class MainActivity extends Activity {
private DatePicker mDatePicker;
private DatePicker mDatePicker, mDPNoTitle;
private Button btnPick;

@Override
Expand All @@ -51,14 +51,34 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

mDatePicker = (DatePicker) findViewById(R.id.main_dp);
mDatePicker.setSingleMonth(true);
mDatePicker.setOnDateSelected(new OnDateSelected() {
@Override
public void selected(List<String> date) {
for (String s : date) {
LogUtil.v(s);
}
Toast.makeText(MainActivity.this, date.toString(),
Toast.LENGTH_SHORT).show();
}
});
mDatePicker.setOnDateChanged(new OnDateSelected() {
@Override
public void selected(List<String> date) {
Toast.makeText(MainActivity.this, date.toString(),
Toast.LENGTH_SHORT).show();
}
});

/* 隐藏确定按钮
mDPNoTitle = (DatePicker) findViewById(R.id.picker_no_title);
mDPNoTitle.hideEnsureButton();
mDPNoTitle.setSingleMonth(true);
mDPNoTitle.setOnDateChanged(new OnDateSelected() {
@Override
public void selected(List<String> date) {
Toast.makeText(MainActivity.this, date.toString(),
Toast.LENGTH_SHORT).show();
}
});
*/

btnPick = (Button) findViewById(R.id.main_btn);
btnPick.setOnClickListener(new View.OnClickListener() {
Expand Down
12 changes: 10 additions & 2 deletions Demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Single date picker:"
android:textSize="16sp"
android:layout_margin="5dp"
android:textStyle="bold" />

<cn.aigestudio.datepicker.views.DatePicker
android:id="@+id/main_dp"
android:layout_width="match_parent"
Expand All @@ -14,6 +22,6 @@
android:id="@+id/main_btn"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Pick date"
android:text="Multiple date picker"
android:layout_height="wrap_content"/>
</LinearLayout>