先看下效果图,主要用于单个日期的日历选择,也可以修改为时间段选择!代码注释掉部分就是选择时间段的,用法比较简单不用导入第三方,也可以自己修改代码样式!如有问题私信

package com.mocoo.fanbei.UI.CalendarList;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.mocoo.fanbei.R;
import com.mocoo.fanbei.UI.MainActivity;
import com.mocoo.fanbei.tool.DataFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarListextends FrameLayout {
private static final StringTAG = MainActivity.class.getSimpleName() +"_LOG";
RecyclerViewrecyclerView;
CalendarAdapteradapter;
private DateBeanstartDate;//开始时间
private DateBeanendDate;//结束时间
OnDateSelectedonDateSelected;//选中监听
SimpleDateFormatsimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
public CalendarList(Context context) {
super(context);
init(context);
}
public CalendarList(Context context,AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CalendarList(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
addView(LayoutInflater.from(context).inflate(R.layout.item_calendar, this, false));
recyclerView = findViewById(R.id.recyclerView);
adapter =new CalendarAdapter();
GridLayoutManager gridLayoutManager =new GridLayoutManager(context, 7);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int i) {
if (DateBean.item_type_month ==adapter.data.get(i).getItemType()) {
return 7;
}else {
return 1;
}
}
});
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new SpacesItemDecoration(1));
adapter.data.addAll(days("", ""));
// DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
// dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.shape));
// recyclerView.addItemDecoration(dividerItemDecoration);
MyItemD myItemD =new MyItemD();
recyclerView.addItemDecoration(myItemD);
adapter.setOnRecyclerviewItemClick(new CalendarAdapter.OnRecyclerviewItemClick() {
@Override
public void onItemClick(View v, int position) {
onClick(adapter.data.get(position));
}
});
}
public class SpacesItemDecorationextends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.bottom =space;
}
}
private void onClick(DateBean dateBean) {
if (dateBean.getItemType() == DateBean.item_type_month || TextUtils.isEmpty(dateBean.getDay())) {
return;
}
//如果没有选中开始日期则此次操作选中开始日期
if (startDate ==null) {
startDate = dateBean;
dateBean.setItemState(DateBean.ITEM_STATE_BEGIN_DATE);
if(onDateSelected!=null){
onDateSelected.selected(simpleDateFormat.format(startDate.getDate()));
}
}
// else if (endDate == null) {
// //如果选中了开始日期但没有选中结束日期,本次操作选中结束日期
//
// //如果当前点击的结束日期跟开始日期一致 则不做操作
// if (startDate == dateBean) {
//
// } else if (dateBean.getDate().getTime() < startDate.getDate().getTime()) {
// //当前点选的日期小于当前选中的开始日期 则本次操作重新选中开始日期
// startDate.setItemState(DateBean.ITEM_STATE_NORMAL);
// startDate = dateBean;
// startDate.setItemState(DateBean.ITEM_STATE_BEGIN_DATE);
// } else {
// //选中结束日期
// endDate = dateBean;
// endDate.setItemState(DateBean.ITEM_STATE_END_DATE);
// setState();
//
// if(onDateSelected!=null){
// onDateSelected.selected(simpleDateFormat.format(startDate.getDate()),simpleDateFormat.format(endDate.getDate()));
// }
// }
//
// }
// else if (startDate != null && endDate != null) {
// //结束日期和开始日期都已选中
// clearState();
//
// //重新选择开始日期,结束日期清除
// startDate.setItemState(DateBean.ITEM_STATE_NORMAL);
// startDate = dateBean;
// startDate.setItemState(DateBean.ITEM_STATE_BEGIN_DATE);
//
// endDate.setItemState(DateBean.ITEM_STATE_NORMAL);
// endDate = null;
// }
adapter.notifyDataSetChanged();
}
//选中中间的日期
private void setState() {
if (endDate !=null &&startDate !=null) {
int start =adapter.data.indexOf(startDate);
start +=1;
int end =adapter.data.indexOf(endDate);
for (; start < end; start++) {
DateBean dateBean =adapter.data.get(start);
if (!TextUtils.isEmpty(dateBean.getDay())) {
dateBean.setItemState(DateBean.ITEM_STATE_SELECTED);
}
}
}
}
//取消选中状态
private void clearState() {
if (endDate !=null &&startDate !=null) {
int start =adapter.data.indexOf(startDate);
start +=1;
int end =adapter.data.indexOf(endDate);
for (; start < end; start++) {
DateBean dateBean =adapter.data.get(start);
dateBean.setItemState(DateBean.ITEM_STATE_NORMAL);
}
}
}
//日历adapter
public static class CalendarAdapterextends RecyclerView.Adapter {
ArrayListdata =new ArrayList<>();
private OnRecyclerviewItemClickonRecyclerviewItemClick;
public OnRecyclerviewItemClickgetOnRecyclerviewItemClick() {
return onRecyclerviewItemClick;
}
public void setOnRecyclerviewItemClick(OnRecyclerviewItemClick onRecyclerviewItemClick) {
this.onRecyclerviewItemClick = onRecyclerviewItemClick;
}
@Override
public int getItemCount() {
return data.size();
}
@NonNull
@Override
public RecyclerView.ViewHolderonCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == DateBean.item_type_day) {
View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_day, viewGroup, false);
final DayViewHolder dayViewHolder =new DayViewHolder(rootView);
dayViewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onRecyclerviewItemClick !=null) {
onRecyclerviewItemClick.onItemClick(v, dayViewHolder.getLayoutPosition());
}
}
});
return dayViewHolder;
}else if (i == DateBean.item_type_month) {
View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_month, viewGroup, false);
final MonthViewHolder monthViewHolder =new MonthViewHolder(rootView);
monthViewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onRecyclerviewItemClick !=null) {
onRecyclerviewItemClick.onItemClick(v, monthViewHolder.getLayoutPosition());
}
}
});
return monthViewHolder;
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolderinstanceof MonthViewHolder) {
((MonthViewHolder) viewHolder).tv_month.setText(data.get(i).getMonthStr());
}else {
DayViewHolder vh = ((DayViewHolder) viewHolder);
vh.tv_day.setText(data.get(i).getDay());
vh.tv_check_in_check_out.setVisibility(View.GONE);
DateBean dateBean =data.get(i);
//设置item状态
// if (dateBean.getItemState() == DateBean.ITEM_STATE_BEGIN_DATE || dateBean.getItemState() == DateBean.ITEM_STATE_END_DATE) {
if (dateBean.getItemState() == DateBean.ITEM_STATE_BEGIN_DATE ) {
//开始日期或结束日期
vh.itemView.setBackgroundColor(Color.parseColor("#ff6600"));
vh.tv_day.setTextColor(Color.WHITE);
// vh.tv_check_in_check_out.setVisibility(View.VISIBLE);
// if (dateBean.getItemState() == DateBean.ITEM_STATE_END_DATE) {
// vh.tv_check_in_check_out.setText("离店");
// } else {
// vh.tv_check_in_check_out.setText("入住");
// }
}
// else if (dateBean.getItemState() == DateBean.ITEM_STATE_SELECTED) {
// //选中状态
// vh.itemView.setBackgroundColor(Color.parseColor("#ffa500"));
// vh.tv_day.setTextColor(Color.WHITE);
// }
else {
//正常状态
vh.itemView.setBackgroundColor(Color.WHITE);
// vh.tv_day.setTextColor(Color.BLACK);
String sData = DataFormat.getToay();
if((sData.split("-")[0]+"-"+sData.split("-")[1]).equals(data.get(i).getMonthStr())){
if(!TextUtils.isEmpty(data.get(i).getDay())){
if(Integer.parseInt(data.get(i).getDay()) < Integer.parseInt(sData.split("-")[2])){
vh.tv_day.setTextColor(0xffc2c1c2);
vh.itemView.setEnabled(false);
}else{
vh.tv_day.setTextColor(0xff020a0f);
vh.itemView.setEnabled(true);
}
}
}else{
vh.itemView.setEnabled(true);
vh.tv_day.setTextColor(0xff020a0f);
}
}
}
}
@Override
public int getItemViewType(int position) {
return data.get(position).getItemType();
}
public class DayViewHolderextends RecyclerView.ViewHolder {
public TextViewtv_day;
public TextViewtv_check_in_check_out;
public DayViewHolder(@NonNull View itemView) {
super(itemView);
tv_day = itemView.findViewById(R.id.tv_day);
tv_check_in_check_out = itemView.findViewById(R.id.tv_check_in_check_out);
}
}
public class MonthViewHolderextends RecyclerView.ViewHolder {
public TextViewtv_month;
public MonthViewHolder(@NonNull View itemView) {
super(itemView);
tv_month = itemView.findViewById(R.id.tv_month);
}
}
public interface OnRecyclerviewItemClick {
void onItemClick(View v, int position);
}
}
/**
* 生成日历数据
*/
private Listdays(String sDate, String eDate) {
List dateBeans =new ArrayList<>();
try {
Calendar calendar = Calendar.getInstance();
//日期格式化
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatYYYYMM =new SimpleDateFormat("yyyy-MM");
//起始日期
Date startDate =new Date();
calendar.setTime(startDate);
//结束日期
calendar.add(Calendar.MONTH, 5);
Date endDate =new Date(calendar.getTimeInMillis());
Log.d(TAG, "startDate:" + format.format(startDate) +"----------endDate:" + format.format(endDate));
//格式化开始日期和结束日期为 yyyy-mm-dd格式
String endDateStr = format.format(endDate);
endDate = format.parse(endDateStr);
String startDateStr = format.format(startDate);
startDate = format.parse(startDateStr);
calendar.setTime(startDate);
Log.d(TAG, "startDateStr:" + startDateStr +"---------endDate:" + format.format(endDate));
Log.d(TAG, "endDateStr:" + endDateStr +"---------endDate:" + format.format(endDate));
calendar.set(Calendar.DAY_OF_MONTH, 1);
Calendar monthCalendar = Calendar.getInstance();
//按月生成日历 每行7个 最多6行 42个
//每一行有七个日期 日 一 二 三 四 五 六 的顺序
for (; calendar.getTimeInMillis() <= endDate.getTime(); ) {
//月份item
DateBean monthDateBean =new DateBean();
monthDateBean.setDate(calendar.getTime());
monthDateBean.setMonthStr(formatYYYYMM.format(monthDateBean.getDate()));
monthDateBean.setItemType(DateBean.item_type_month);
dateBeans.add(monthDateBean);
//获取一个月结束的日期和开始日期
monthCalendar.setTime(calendar.getTime());
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
Date startMonthDay = calendar.getTime();
monthCalendar.add(Calendar.MONTH, 1);
monthCalendar.add(Calendar.DAY_OF_MONTH, -1);
Date endMonthDay = monthCalendar.getTime();
//重置为本月开始
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
Log.d(TAG, "月份的开始日期:" + format.format(startMonthDay) +"---------结束日期:" + format.format(endMonthDay));
for (; monthCalendar.getTimeInMillis() <= endMonthDay.getTime(); ) {
//生成单个月的日历
//处理一个月开始的第一天
if (monthCalendar.get(Calendar.DAY_OF_MONTH) ==1) {
//看某个月第一天是周几
int weekDay = monthCalendar.get(Calendar.DAY_OF_WEEK);
switch (weekDay) {
case 1:
//周日
break;
case 2:
//周一
addDatePlaceholder(dateBeans, 1, monthDateBean.getMonthStr());
break;
case 3:
//周二
addDatePlaceholder(dateBeans, 2, monthDateBean.getMonthStr());
break;
case 4:
//周三
addDatePlaceholder(dateBeans, 3, monthDateBean.getMonthStr());
break;
case 5:
//周四
addDatePlaceholder(dateBeans, 4, monthDateBean.getMonthStr());
break;
case 6:
addDatePlaceholder(dateBeans, 5, monthDateBean.getMonthStr());
//周五
break;
case 7:
addDatePlaceholder(dateBeans, 6, monthDateBean.getMonthStr());
//周六
break;
}
}
//生成某一天日期实体 日item
DateBean dateBean =new DateBean();
dateBean.setDate(monthCalendar.getTime());
dateBean.setDay(monthCalendar.get(Calendar.DAY_OF_MONTH) +"");
dateBean.setMonthStr(monthDateBean.getMonthStr());
dateBeans.add(dateBean);
//处理一个月的最后一天
if (monthCalendar.getTimeInMillis() == endMonthDay.getTime()) {
//看某个月第一天是周几
int weekDay = monthCalendar.get(Calendar.DAY_OF_WEEK);
switch (weekDay) {
case 1:
//周日
addDatePlaceholder(dateBeans, 6, monthDateBean.getMonthStr());
break;
case 2:
//周一
addDatePlaceholder(dateBeans, 5, monthDateBean.getMonthStr());
break;
case 3:
//周二
addDatePlaceholder(dateBeans, 4, monthDateBean.getMonthStr());
break;
case 4:
//周三
addDatePlaceholder(dateBeans, 3, monthDateBean.getMonthStr());
break;
case 5:
//周四
addDatePlaceholder(dateBeans, 2, monthDateBean.getMonthStr());
break;
case 6:
addDatePlaceholder(dateBeans, 1, monthDateBean.getMonthStr());
//周5
break;
}
}
//天数加1
monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
Log.d(TAG, "日期" + format.format(calendar.getTime()) +"----周几" + getWeekStr(calendar.get(Calendar.DAY_OF_WEEK) +""));
//月份加1
calendar.add(Calendar.MONTH, 1);
}
}catch (Exception ex) {
}
return dateBeans;
}
//添加空的日期占位
private void addDatePlaceholder(List dateBeans, int count, String monthStr) {
for (int i =0; i < count; i++) {
DateBean dateBean =new DateBean();
dateBean.setMonthStr(monthStr);
dateBeans.add(dateBean);
}
}
private StringgetWeekStr(String mWay) {
if ("1".equals(mWay)) {
mWay ="天";
}else if ("2".equals(mWay)) {
mWay ="一";
}else if ("3".equals(mWay)) {
mWay ="二";
}else if ("4".equals(mWay)) {
mWay ="三";
}else if ("5".equals(mWay)) {
mWay ="四";
}else if ("6".equals(mWay)) {
mWay ="五";
}else if ("7".equals(mWay)) {
mWay ="六";
}
return mWay;
}
public interface OnDateSelected{
// void selected(String startDate, String endDate);
void selected(String startDate);
}
public void setOnDateSelected(OnDateSelected onDateSelected) {
this.onDateSelected = onDateSelected;
}
}
package com.mocoo.fanbei.UI.CalendarList;
import java.util.Date;
public class DateBean {
//item类型
public static int item_type_day =1;//日期item
public static int item_type_month =2;//月份item
int itemType =1;//默认是日期item
//item状态
public static int ITEM_STATE_BEGIN_DATE =1;//开始日期
public static int ITEM_STATE_END_DATE =2;//结束日期
public static int ITEM_STATE_SELECTED =3;//选中状态
public static int ITEM_STATE_NORMAL =4;//正常状态
public int itemState =ITEM_STATE_NORMAL;
Datedate;//具体日期
Stringday;//一个月的某天
StringmonthStr;//月份
public int getItemState() {
return itemState;
}
public void setItemState(int itemState) {
this.itemState = itemState;
}
public int getItemType() {
return itemType;
}
public void setItemType(int itemType) {
this.itemType = itemType;
}
public StringgetMonthStr() {
return monthStr;
}
public void setMonthStr(String monthStr) {
this.monthStr = monthStr;
}
public static int getItem_type_month() {
return item_type_month;
}
public static void setItem_type_month(int item_type_month) {
DateBean.item_type_month = item_type_month;
}
public static int getItem_type_day() {
return item_type_day;
}
public static void setItem_type_day(int item_type_day) {
DateBean.item_type_day = item_type_day;
}
public StringgetDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public DategetDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package com.mocoo.fanbei.UI.CalendarList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class MyItemDextends RecyclerView.ItemDecoration {
Paintpaint=new Paint();
PaintcolorPaint=new Paint();
PaintlinePaint=new Paint();
public MyItemD(){
paint.setColor(Color.parseColor("#ffffff"));
paint.setStyle(Paint.Style.FILL);
colorPaint.setColor(Color.parseColor("#ff6600"));
colorPaint.setAntiAlias(true);
linePaint.setAntiAlias(true);
linePaint.setColor(Color.parseColor("#dddddd"));
}
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if(parent.getChildCount()<=0){
return;
}
//头部的高度
int height=50;
final float scale = parent.getContext().getResources().getDisplayMetrics().density;
height= (int) (height*scale+0.5f);
//获取第一个可见的view,通过此view获取其对应的月份
CalendarList.CalendarAdapter a=(CalendarList.CalendarAdapter) parent.getAdapter();
View fistView=parent.getChildAt(0);
String text=a.data.get(parent.getChildAdapterPosition(fistView)).getMonthStr();
String fistMonthStr="";
int fistViewTop=0;
//查找当前可见的itemView中第一个月份类型的item
for(int i=0;i
View v=parent.getChildAt(i);
if(2==parent.getChildViewHolder(v).getItemViewType()){
fistMonthStr=a.data.get(parent.getChildAdapterPosition(v)).getMonthStr();
fistViewTop=v.getTop();
break;
}
}
//计算偏移量
int topOffset=0;
if(!fistMonthStr.equals(text)&&fistViewTop
//前提是第一个可见的月份item不是当前显示的月份和距离的顶部的距离小于头部的高度
topOffset=height-fistViewTop;
}
int t=0-topOffset;
//绘制头部区域
c.drawRect(parent.getLeft(),t,parent.getRight(),t+height,paint);
colorPaint.setTextAlign(Paint.Align.CENTER);
colorPaint.setTextSize(15*scale+0.5f);
//绘制头部月份文字
c.drawText(text,parent.getRight()/2,(t+height)/2,colorPaint);
//绘制分割线
// if(fistViewTop!=height) {
// linePaint.setStrokeWidth(scale * 0.5f + 0.5f);
// c.drawLine(parent.getLeft(), t + height, parent.getRight(), t + height, linePaint);
// }
}
}
创建Layout name= item_calendar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:background="#F2F2F2"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="日"
android:textColor="#ff6600"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="一"
android:textColor="#666"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="二"
android:textColor="#666"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="三"
android:textColor="#666"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="四"
android:textColor="#666"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="五"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="六"
android:textColor="#ff6600"
android:gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
创建Layout name=item_day
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="1"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_check_in_check_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="入住"
android:textColor="#fff"
android:textSize="12dp"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
创建Layout name=item_month
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="50dp">
<TextView
android:id="@+id/tv_month"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:text="8"
android:textSize="15dp"
android:textColor="#ff6600" />
</android.support.constraint.ConstraintLayout>
用法也比较简单,如果要用弹出模式直接嵌套再Dialog里面即可:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="#00000000"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingTop="15dp"
android:paddingBottom="20dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期"
android:layout_centerHorizontal="true"
android:textColor="#ff020a0f"
android:textSize="17sp" />
android:id="@+id/tv_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:text="取消"
android:textColor="#fff94d1e"
android:textSize="12sp" />
android:id="@+id/calendarList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Java代码
/**
* 从底部弹出popupwindow
*/
private void showBottomPop(View parent) {
final View popView = View.inflate(getActivity(), R.layout.activity_calendar_list, null);
showAnimation(popView);//开启动画
final PopupWindow mPopWindow =new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// clickPopItem(popView, mPopWindow);//条目的点击
mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopWindow.showAtLocation(parent,
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
mPopWindow.setOutsideTouchable(true);
mPopWindow.setFocusable(true);
mPopWindow.update();
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
DisplayMetrics d = getActivity().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.alpha =0.7f;
getActivity().getWindow().setAttributes(lp);
popView.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopWindow.dismiss();
}
});
mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha =1f;
getActivity().getWindow().setAttributes(lp);
}
});
CalendarList calendarList=popView.findViewById(R.id.calendarList);
calendarList.setOnDateSelected(new CalendarList.OnDateSelected() {
@Override
public void selected(String startDate) {
mPopWindow.dismiss();
isSelectDate =true;
baseAdapter.notifyDataSetChanged();
tvSelectDate.setText(startDate.split("-")[1]+"-"+startDate.split("-")[2]);
date = startDate;
loadListStore();
}
});
}
/**
* 给popupwindow添加动画
*
* @param popView
*/
private void showAnimation(View popView) {
AnimationSet animationSet =new AnimationSet(false);
AlphaAnimation alphaAnimation =new AlphaAnimation(0f, 1.0f);
alphaAnimation.setDuration(300);
TranslateAnimation translateAnimation =new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f
);
translateAnimation.setDuration(300);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(translateAnimation);
popView.startAnimation(animationSet);
}
网友评论