一个简单的自定义Android日历
程序员文章站
2022-06-08 15:41:28
...
自定义的一个Android日历
1.先上图片
2.思路:
布局是使用了两个Button,中间的一个TextView,最下方一个RecycelrVeiw。这不用多讲。最主要的还是如何获得日期并正确显示。
2.1如何获得日期
通过Calendar类可以获得日期。
2.2如何显示
首先获得当月1号是星期几,比如说2018/6/1是星期五,那么就应该让它显示在星期五上。对于之前的五月后几天,可以通过Calendar的add方法获得
3.布局代码
activity_main.xml:
<com.calendardemo.cjl.calendardemo.utils.calendarView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:onClick="lastMonth"
android:text="上一月"/>
<TextView
android:id="@+id/calendar_month_head"
android:layout_width="80dp"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20dp"
android:layout_centerInParent="true" />
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:onClick="nextMonth"
android:text="下一月"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:textColor="#000000"
android:text="日"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="一"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="二"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="三"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="四"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="五"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="六"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/calendar_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</com.calendardemo.cjl.calendardemo.utils.calendarView>
4.recyclerview的item布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="@+id/recycler_view_item_tv"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginLeft="15dp"
android:gravity="center"
android:text="25"
android:textSize="20dp"/>
</LinearLayout>
6.MainAcivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "cjl";
private RecyclerView recyclerView;
private Calendar calendar = Calendar.getInstance();
private TextView textView;
private List<DateUtils> dates = new ArrayList<>();
private RecyclerViewAdapter adapter;
private ChangerDateOfAdapter changerDateOfAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.calendar_month_head);
recyclerView = (RecyclerView) findViewById(R.id.calendar_recycler_view);
//更新数据
refreshCaledar();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
textView.setText(String.valueOf(year) + "/" + String.valueOf(month));
GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), 7);
adapter = new RecyclerViewAdapter(dates,this);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
}
public void setChangerDatrOfAdapter(ChangerDateOfAdapter changerDatrOfAdapter){
this.changerDateOfAdapter=changerDatrOfAdapter;
}
/**
* 上一个月
*/
public void lastMonth(View v) {
calendar.add(Calendar.MONTH, -1);
int years = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
textView.setText(String.valueOf(years) + "/" + String.valueOf(month));
refreshCaledar();
adapter.notifyDataSetChanged();
changerDateOfAdapter.changerMonth(-1);
}
/**
* 下一月
*/
public void nextMonth(View v) {
calendar.add(Calendar.MONTH, +1);
int years = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
textView.setText(String.valueOf(years) + "/" + String.valueOf(month));
refreshCaledar();
adapter.notifyDataSetChanged();
changerDateOfAdapter.changerMonth(1);
}
// TODO: 2018/6/20 刷新控件
private void refreshCaledar() {
//清除list数据
if (dates != null) {
dates.clear();
}
//将原有的calendar复制
Calendar calendars = (Calendar) calendar.clone();
//将日改为1号,以用来确定1号为星期几
calendars.set(Calendar.DAY_OF_MONTH, 1);
//获得1号的星期
int weekday = calendars.get(Calendar.DAY_OF_WEEK) - 1;
//偏移calendas
calendars.add(Calendar.DAY_OF_MONTH, -weekday);
int maxNumber = 6 * 7;
while (dates.size() < maxNumber) {
int yaer = calendars.get(Calendar.YEAR);
int month=calendars.get(Calendar.MONTH);
int day=calendars.get(Calendar.DAY_OF_MONTH);
DateUtils utils=new DateUtils();
utils.setYear(yaer);
utils.setMonth(month);
utils.setDay(day);
dates.add(utils);
calendars.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
7.recyclerview的Adapter代码
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>
implements ChangerDateOfAdapter {
private static final String TAG = "cjl";
private List<DateUtils> dateList;
private Calendar calendar;
private int nowYear;
private int nowMonth;
private int nowDay;
private MainActivity mContext;
private Calendar calendars;
static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.recycler_view_item_tv);
}
}
public RecyclerViewAdapter(List<DateUtils> dateList, MainActivity context) {
this.dateList = dateList;
this.mContext=context;
calendar = Calendar.getInstance();
calendars = (Calendar) calendar.clone();
mContext.setChangerDatrOfAdapter(this);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent,
false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
DateUtils dateUtils = dateList.get(position);
int year = dateUtils.getYear();
int month = dateUtils.getMonth();
int day = dateUtils.getDay();
nowYear = calendar.get(Calendar.YEAR);
nowMonth = calendar.get(Calendar.MONTH);
nowDay = calendar.get(Calendar.DAY_OF_MONTH);
int months=calendars.get(Calendar.MONTH);
if (year == nowYear && month == nowMonth && day == nowDay) {
holder.textView.setTextColor(Color.parseColor("#FF00FF"));
}
if (month != months) {
holder.textView.setTextColor(Color.parseColor("#D1CB07"));
Log.i(TAG, "yanse: ");
}
holder.textView.setText(String.valueOf(day));
}
@Override
public int getItemCount() {
return dateList.size();
}
@Override
public void changerMonth(int i) {
calendars.add(Calendar.MONTH,i);
}
}
下一篇: 表格单元格点击事件