先上一下实现后的效果图吧,日期颜色,以及下面小圆圈里面的数字都可以自己来决定,添加了月份的切换,日期的点击事件。
话不多说,直接上完整代码,可以根据业务适当的修改代码来达到自己想要的效果
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hasee.water.R;
import com.example.hasee.water.model.RiLiModle;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class RiliActivity extends AppCompatActivity {
private static final String TAG = "RiliActivity";
private RecyclerView recyclerView;
private TextView leftTextView;
private TextView rightTextView;
private TextView currentMonthTextView;
private RiLiAdapter riLiAdapter;
private int currentYear;//当前年份
private int currentMonth;//当前月份
private int selectYear;//当前年份
private int selectMonth;//当前月份
private List<RiLiModle> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rili);
initView();
getCurrentDay();
}
private void getCurrentDay() {
Calendar calendar = Calendar.getInstance();
//获取系统的日期
//年
currentYear = calendar.get(Calendar.YEAR);
//月
currentMonth = calendar.get(Calendar.MONTH) + 1;
//日
selectYear = currentYear;
selectMonth = currentMonth;
int day = calendar.get(Calendar.DAY_OF_MONTH);
//根据年月查询当月天数;
getDaysByYearMonth(currentYear, currentMonth);
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
GridLayoutManager layoutManager = new GridLayoutManager(this, 7, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
riLiAdapter = new RiLiAdapter();
recyclerView.setAdapter(riLiAdapter);
leftTextView = (TextView) findViewById(R.id.leftTextView);
rightTextView = (TextView) findViewById(R.id.rightTextView);
currentMonthTextView = (TextView) findViewById(R.id.currentMonthTextView);
leftTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectMonth == 1) {
selectMonth = 12;
selectYear--;
} else {
selectMonth--;
}
currentMonthTextView.setText(selectYear + "-" + selectMonth);
getDaysByYearMonth(selectYear, selectMonth);
}
});
rightTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectMonth == 12) {
selectMonth = 1;
selectYear++;
} else {
selectMonth++;
}
currentMonthTextView.setText(selectYear + "-" + selectMonth);
getDaysByYearMonth(selectYear, selectMonth);
}
});
}
/**
* * 根据 年、月 获取对应的月份 的 天数
*/
public void getDaysByYearMonth(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
String date = year + "-" + month + "-01";
getDayOfWeekByDate(date, maxDate);
}
/**
* 根据日期 找到对应日期的 星期几
*
* @param date 比如传参:2018-07-13 将返回“周五”
*/
public void getDayOfWeekByDate(String date, int selectMonthTotalDay) {
String dayOfweek = "-1";
try {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = myFormatter.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("E");
String str = formatter.format(myDate);
dayOfweek = str;
} catch (Exception e) {
System.out.println("错误!");
}
Log.d(TAG, "getDayOfWeekByDate: 当前日期" + date + "对应" + dayOfweek);
setRiLiDate(dayOfweek, selectMonthTotalDay);
}
private void setRiLiDate(String dayOfweek, int selectMonthTotalDay) {
//先判断前面的空白日期
int spaceDate = 0;
if ("周一".equals(dayOfweek) ) {
spaceDate = 0;
} else if ("周二".equals(dayOfweek)){
spaceDate = 1;
} else if ("周三".equals(dayOfweek)){
spaceDate = 2;
} else if ("周四" .equals(dayOfweek)) {
spaceDate = 3;
} else if ("周五".equals(dayOfweek)) {
spaceDate = 4;
} else if ("周六" .equals(dayOfweek)) {
spaceDate = 5;
} else if ("周日".equals(dayOfweek)) {
spaceDate = 6;
} else {
spaceDate = 0;
}
Log.d(TAG, "getDayOfWeekByDate: "+spaceDate);
list.clear();
//添加空白日期
for (int m = 0; m < spaceDate; m++) {
RiLiModle riLiModle = new RiLiModle();
riLiModle.date = "";
riLiModle.time = "";
list.add(riLiModle);
}
//添加日历数据
for (int i = 1; i <= selectMonthTotalDay; i++) {
RiLiModle riLiModle = new RiLiModle();
riLiModle.date = i + "";
riLiModle.time=i+"";
list.add(riLiModle);
}
riLiAdapter.notifyDataSetChanged();
}
public class RiLiAdapter extends RecyclerView.Adapter<RiLiAdapter.ViewHolder> {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(RiliActivity.this).inflate(R.layout.item_rili, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
if (TextUtils.isEmpty(list.get(position).time)) {
holder.timeTextView.setVisibility(View.INVISIBLE);
} else {
holder.timeTextView.setVisibility(View.VISIBLE);
}
if (TextUtils.isEmpty(list.get(position).date)) {
holder.dateTextView.setVisibility(View.INVISIBLE);
} else {
holder.dateTextView.setVisibility(View.VISIBLE);
}
holder.dateTextView.setText(list.get(position).date);
holder.timeTextView.setText(list.get(position).time);
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (position < 10) {
Toast.makeText(RiliActivity.this, "选择的日期" + selectYear + "-" + selectMonth + "-0" + list.get(position).date, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RiliActivity.this, "选择的日期" + selectYear + "-" + selectMonth + "-" + list.get(position).date, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
private TextView dateTextView;
private TextView timeTextView;
public ViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
dateTextView = (TextView) itemView.findViewById(R.id.dateTextView);
timeTextView = (TextView) itemView.findViewById(R.id.timeTextView);
}
}
}
}
activity_rili布局文件:
<?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:paddingRight="16dp"
android:paddingLeft="16dp"
android:orientation="vertical"
tools:context=".activity.RiliActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/leftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#123456"
android:textSize="14dp"
android:text="上个月"/>
<TextView
android:id="@+id/currentMonthTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="17dp"
android:text="2020-03"/>
<TextView
android:id="@+id/rightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#123456"
android:textSize="14dp"
android:text="下个月"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:weightSum="7"
android:background="#f4f4f4"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="一"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="二"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="三"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="四"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="五"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="六"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="日"
android:textSize="14dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
//item_rili布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@drawable/square"
android:gravity="center">
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16dp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/timeTextView"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center"
android:textColor="#ffffff"
android:layout_marginTop="4dp"
android:background="@drawable/circle"
android:text="3"/>
</LinearLayout>
circle.xml布局
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dp"/>
<solid
android:color="#FF9800"/>
</shape>
到这里就完整的结束了,是不是很简单