点击查看上一个版本
点击查看上一个版本
仅仅靠滑动选择时间是不够的,这次改动成可滑动,可点击,当然坏处是数据源可能很庞大!代码并不难:
package com.zhoudada.transversetimepicker;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.zhoudada.datetool.ClassVariables;
import com.zhoudada.datetool.DateChange;
@SuppressLint("SimpleDateFormat")
public class MainActivity extends Activity {
// 时间临时记录
private String mYear,mMonth,dayOfMonths , dateTime , toDay;
private TextView chooseTimeYear1 , chooseTimeMonth1 , chooseTimeDay1 , chooseTimeYear2 , chooseTimeMonth2 , chooseTimeDay2 ;
// 存放每个View
private View view;
private List viewList;
private LayoutInflater inflater;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initialize();
getNow();
setData();
ClassVariables.mViewPager.setAdapter(new PagerViewAdapter(viewList, this));
// 调到指定位置
ClassVariables.mViewPager.setCurrentItem(position);
ClassVariables.mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
dateTime = ClassVariables.timelist.get(arg0).get("year").toString()+"-"+ClassVariables.timelist.get(arg0).get("month").toString()+"-"+ClassVariables.timelist.get(arg0).get("day").toString();
String startDate = null , endDate = null;
startDate = DateChange.getDateAddOrLess(dateTime, -1);
chooseTimeYear1.setText(startDate == null ? "" : startDate.substring(0, 4));
chooseTimeMonth1.setText(startDate == null ? "" : startDate.substring(5, 7));
chooseTimeDay1.setText(startDate == null ? "" : startDate.substring(8, 10));
endDate = DateChange.getDateAddOrLess(dateTime, 1);
// 判断有没有到今天。
if(dateTime.equals(toDay)){
setTimeDateColor();
}else{
removeTimeDateColor();
}
chooseTimeYear2.setText(endDate == null ? "" : endDate.substring(0, 4));
chooseTimeMonth2.setText(endDate == null ? "" : endDate.substring(5, 7));
chooseTimeDay2.setText(endDate == null ? "" : endDate.substring(8, 10));
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/** 添加数据 */
public void setData(){
Map map;
// 添加数据
for (int i = 0; i < ClassVariables.dateTimeChoose.size(); i++){
map = new HashMap();
String year = ClassVariables.dateTimeChoose.get(i).get("year").toString();
String month = ClassVariables.dateTimeChoose.get(i).get("month").toString();
String day = ClassVariables.dateTimeChoose.get(i).get("day").toString();
String isTime = year+"-"+month+"-"+day;
map.put("year", year);
map.put("month", month);
map.put("day", day);
ClassVariables.timelist.add(map);
// toDay值可以变成你从上个页面传过来的日期
if(isTime.equals(toDay)){
position = i;
}
}
// 循环创建View
for (int i = 0; i < ClassVariables.timelist.size(); i++){
view = inflater.inflate(R.layout.textview_timechoose, null);
TextView chooseTimeYear = (TextView) view.findViewById(R.id.chooseTimeYear);
TextView chooseTimeMonth = (TextView) view.findViewById(R.id.chooseTimeMonth);
TextView chooseTimeDay = (TextView) view.findViewById(R.id.chooseTimeDay);
chooseTimeYear.setText(ClassVariables.timelist.get(i).get("year").toString());
chooseTimeMonth.setText(ClassVariables.timelist.get(i).get("month").toString());
chooseTimeDay.setText(ClassVariables.timelist.get(i).get("day").toString());
viewList.add(view);
}
}
/** 获取今天 */
public void getNow(){
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR),months = c.get(Calendar.MONTH),day = c.get(Calendar.DAY_OF_MONTH);
months = months + 1;
mYear = year+"";dayOfMonths = day+"";
// 单数前加0,强迫症!!!
if((months+"").length() == 2){
mMonth = (months)+"";
}else { // 1
mMonth = "0"+(months);
}
if((day+"").length() == 2){
dayOfMonths = ""+day;
}else{
dayOfMonths = "0"+day;
}
dateTime = mYear+"-"+mMonth+"-"+dayOfMonths;
toDay = mYear+"-"+mMonth+"-"+dayOfMonths;
parameterInitialization(toDay);
setTextValue(dateTime);
}
/** 刷新UI */
public void setTextValue(String date){
// 获取昨天
String startDate = DateChange.getDateAddOrLess(date, -1);
// 获取明天
String endDate = DateChange.getDateAddOrLess(date, 1);
chooseTimeYear1.setText(startDate == null ? "" : startDate.substring(0, 4));
chooseTimeMonth1.setText(startDate == null ? "" : startDate.substring(5, 7));
chooseTimeDay1.setText(startDate == null ? "" : startDate.substring(8, 10));
// 如果当前时间,等于今天,就把明天变成灰色
if(dateTime.equals(toDay)){
setTimeDateColor();
}else{
removeTimeDateColor();
}
chooseTimeYear2.setText(endDate == null ? "" : endDate.substring(0, 4));
chooseTimeMonth2.setText(endDate == null ? "" : endDate.substring(5, 7));
chooseTimeDay2.setText(endDate == null ? "" : endDate.substring(8, 10));
}
// 没有明天,把明天变成灰色
public void setTimeDateColor(){
chooseTimeYear2.setTextColor(Color.parseColor("#7F7F7F"));
chooseTimeMonth2.setTextColor(Color.parseColor("#7F7F7F"));
chooseTimeDay2.setTextColor(Color.parseColor("#7F7F7F"));
}
// 变回原来的颜色
public void removeTimeDateColor(){
chooseTimeYear2.setTextColor(Color.parseColor("#8B2C8C"));
chooseTimeMonth2.setTextColor(Color.parseColor("#8B2C8C"));
chooseTimeDay2.setTextColor(Color.parseColor("#8B2C8C"));
}
/** 初始化控件 */
public void initialize(){
viewList = new ArrayList();
inflater = LayoutInflater.from(this);
ClassVariables.timelist = new ArrayList
package com.zhoudada.transversetimepicker;
import java.util.List;
import com.defei.datepickerdialog.MyDatePickerDialog;
import com.zhoudada.datetool.ClassVariables;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.Toast;
@SuppressLint("UseValueOf")
public class PagerViewAdapter extends PagerAdapter {
private List mViewList;
private Context mContext;
// 时间临时记录
private String mYear,mMonth,dayOfMonthe;
public PagerViewAdapter(List titleList , Context context ) {
this.mViewList = titleList;
this.mContext = context;
}
@Override
public int getCount() {
return mViewList != null ? mViewList.size() : 0;
}
@Override
public Object instantiateItem(View view, final int index) {
((ViewPager) view).addView(mViewList.get(index), 0);
View v = mViewList.get(index);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mYear = ClassVariables.timelist.get(index).get("year").toString();
mMonth = ClassVariables.timelist.get(index).get("month").toString();
dayOfMonthe = ClassVariables.timelist.get(index).get("day").toString();
timeChoose();
}
});
return mViewList.get(index);
}
@Override
public void destroyItem(View view, int position, Object arg2) {
((ViewPager) view).removeView(mViewList.get(position));
}
@Override
public void finishUpdate(View arg0) {}
@Override
public boolean isViewFromObject(View view, Object obj) {return (view == obj);}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {return null;}
@Override
public void startUpdate(View arg0) {}
/** 时间弹窗 */
public void timeChoose(){
OnDateSetListener endDateset = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int year, int month,
int dayOfMonth) {
month = month + 1;
if(year == new Integer(mYear)){
if((month) == new Integer(mMonth.length()==1?mMonth.substring(1, mMonth.length()+1):mMonth)){
if(dayOfMonth == new Integer(dayOfMonthe.length()==1?dayOfMonthe.substring(1, dayOfMonthe.length()+1):dayOfMonthe)){
return;
}
}
}
if((month+"").length() == 2){
mMonth = ""+(month);
}else{
mMonth = "0"+(month);
}
if((dayOfMonth+"").length() == 1){
dayOfMonthe = "0"+dayOfMonth;
}else{
dayOfMonthe = ""+dayOfMonth;
}
mYear = year+"";
String thisTime = mYear+"-"+mMonth+"-"+dayOfMonthe;
int index = getIndex(thisTime);
if(index != -1){
ClassVariables.mViewPager.setCurrentItem(index);
}else{
Toast.makeText(mContext, "你得扩大数据才能选择", Toast.LENGTH_SHORT).show();
}
}
};
MyDatePickerDialog endDatePicker = new MyDatePickerDialog(mContext, endDateset, new Integer(mYear), (new Integer(mMonth)-1),
new Integer(dayOfMonthe));
endDatePicker.show();
}
public int getIndex(String thisTime){
for(int i = 0;i
// 传入时间,和-1 或者 1,-1代表减一天,1代表加1天。
public static String getDateAddOrLess(String original , int addorless){
try {
// 将结束时间天数加1
Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(original);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, addorless);
String newDate = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
if(newDate != null){
return newDate;
}
} catch (ParseException e1) {
e1.printStackTrace();
}
return null;
}