本文實例為大家分享了Android自定義日歷滑動控件的使用方法,供大家參考,具體內容如下
創(chuàng)新互聯(lián)一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務為基礎,以質量求生存,以技術求發(fā)展,成交一個客戶多一個朋友!為您提供網(wǎng)站制作、成都網(wǎng)站設計、成都網(wǎng)頁設計、小程序設計、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、成都app軟件開發(fā)是成都本地專業(yè)的網(wǎng)站建設和網(wǎng)站設計公司,等你一起來見證!
最近公司項目需要做這個需求,自己才疏學淺,總算能寫出個大概來,遂在這里記錄下來。
分析
先來分析一下:
首先,我們的需求是可以左右點擊查看跳轉到下一個月,中間的日歷控件可以水平滾動選擇日期,所以我們中間的日歷控件用一個RecycleView來做,左右兩位的為ImageVeiw。
LRCalendarView 總體流程:
1. R.layout.calendar_view
<?xml version="1.0" encoding="utf-8"?>
2. 新建類LRCalendarView繼承LinearLayout并添加布局
public class LRCalendarView extends LinearLayout { private Context context; private ImageView ivLeft, ivRight; private RecyclerView mRecyclerView; private GalleryAdapter mAdapter; private Listdata = new ArrayList<>(); private int mCurrYear, mCurrMonth, mCurrDay; private int mSelYear, mSelMonth, mSelDay; //今天的日期的position private int todayPos; public LRCalendarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; setupView(context); } public LRCalendarView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LRCalendarView(Context context) { this(context, null); } /** * 初始化控件 */ private void setupView(final Context context) { View view = LayoutInflater.from(context).inflate(R.layout.calendar_view, null); this.addView(view); data = init(); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); ivLeft = (ImageView) findViewById(R.id.iv_left); ivRight = (ImageView) findViewById(R.id.iv_right); //設置manger LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); //設置adapter mAdapter = new GalleryAdapter(context, init()); mRecyclerView.setAdapter(mAdapter); } } |
public class Cell { private String day; private String month; private int mCurrDay; private boolean isSelect; public int getmCurrDay() { return mCurrDay; } public void setmCurrDay(int mCurrDay) { this.mCurrDay = mCurrDay; } public String getDay() { return day; } public void setDay(String day) { this.day = day; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public boolean isSelect() { return isSelect; } public void setSelect(boolean select) { isSelect = select; } }
接下來都是獲取日期數(shù)據(jù),可以寫在一個工具類里,我直接寫在LRCalendarView
/** * 初始化每個月的數(shù)據(jù) */ private Listinit() { Calendar calendar = Calendar.getInstance(); mCurrYear = calendar.get(Calendar.YEAR); mCurrMonth = calendar.get(Calendar.MONTH); mCurrDay = calendar.get(Calendar.DATE); return init(mCurrYear, mCurrMonth, mCurrDay, 0); } |
/** * @param flag 0:正常初始化日期 1:為左右點擊的初始化日期 */ private Listinit(int year, int month, int day, int flag) { if (flag != 0) { day = 1; } setSelectYearMonth(year, month, day); List | listData = new ArrayList<>(); int mMonthDays = getMonthDays(year, month); for (int i = 0; i < mMonthDays; i++) { Cell cell = new Cell(); cell.setDay((i + 1) + ""); cell.setMonth((month + 1) + "月"); if (i + 1 == day) { cell.setmCurrDay(day); todayPos = day; } listData.add(cell); } return listData; } private void setSelectYearMonth(int year, int month, int day) { mSelYear = year; mSelMonth = month; mSelDay = day; } |
/** * 通過年份和月份 得到當月的日子 * * @param year * @param month * @return */ public int getMonthDays(int year, int month) { month++; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return 29; } else { return 28; } default: return -1; } }
GalleryAdapter
class GalleryAdapter extends RecyclerView.Adapter{ private LayoutInflater mInflater; private List mDatas; /** * 用來記錄點擊的item */ private int selectedPos; private boolean isClick; public GalleryAdapter(Context context, List | data) { mInflater = LayoutInflater.from(context); this.mDatas = data; } public void setList(List | data) { this.mDatas = data; isClick = false; } public static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View view) { super(view); tv1 = (TextView) view.findViewById(R.id.textView1); tv2 = (TextView) view.findViewById(R.id.textView2); linearLayout = (LinearLayout) view.findViewById(R.id.ll); } TextView tv1; TextView tv2; LinearLayout linearLayout; } @Override public int getItemCount() { return mDatas.size(); } /** * 創(chuàng)建ViewHolder */ @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = mInflater.inflate(R.layout.cell, null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } /** * 設置值 */ @Override public void onBindViewHolder(final ViewHolder viewHolder, final int i) { if (!isClick) { selectedPos = Integer.valueOf(mDatas.get(i).getmCurrDay()); selectedPos -= 1; } viewHolder.tv1.setText(mDatas.get(i).getDay()); viewHolder.tv2.setText(mDatas.get(i).getMonth()); if (selectedPos == i) { viewHolder.linearLayout.setSelected(true); } else { viewHolder.linearLayout.setSelected(false); } //如果設置了回調,則設置點擊事件 if (mOnItemClickLitener != null) { viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isClick = true; clearSelection(i); notifyDataSetChanged(); String date = mDatas.get(i).getMonth() + mDatas.get(i).getDay(); mOnItemClickLitener.onItemClick(viewHolder.itemView, i, date); } }); } } public void clearSelection(int pos) { selectedPos = pos; } private OnItemClickLitener mOnItemClickLitener; public interface OnItemClickLitener { void onItemClick(View view, int position, String date); } public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } |
給左右的ImageView設置點擊事件
ivLeft.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { data.clear(); data.addAll(onLeftClick(mSelYear, mSelMonth, mSelDay)); mAdapter.setList(data); mAdapter.notifyDataSetChanged(); mRecyclerView.scrollToPosition(0); } }); ivRight.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { data.clear(); data.addAll(onRightClick(mSelYear, mSelMonth, mSelDay)); mAdapter.setList(data); mAdapter.notifyDataSetChanged(); mRecyclerView.scrollToPosition(0); } });
/** * 左點擊,日歷向后翻頁 */ public ListonLeftClick(int mSelYear, int mSelMonth, int mSelDay) { int year = mSelYear; int month = mSelMonth; int day = mSelDay; if (month == 0) {//若果是1月份,則變成12月份 year = mSelYear - 1; month = 11; } else if (getMonthDays(year, month) == day) { //如果當前日期為該月最后一點,當向前推的時候,就需要改變選中的日期 month = month - 1; day = getMonthDays(year, month); } else { month = month - 1; } return init(year, month, day, 1); } /** * 右點擊,日歷向后翻頁 */ public List | onRightClick(int mSelYear, int mSelMonth, int mSelDay) { int year = mSelYear; int month = mSelMonth; int day = mSelDay; if (month == 11) {//若果是12月份,則變成1月份 year = mSelYear + 1; month = 0; } else if (getMonthDays(year, month) == day) { //如果當前日期為該月最后一點,當向前推的時候,就需要改變選中的日期 month = month + 1; day = getMonthDays(year, month); } else { month = month + 1; } return init(year, month, day, 1); } |
給adapter設置item的點擊事件
mAdapter.setOnItemClickLitener(new GalleryAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position, String date) { Toast.makeText(context.getApplicationContext(), date, Toast.LENGTH_SHORT).show(); } });
以上完成后即可在activity中使用
大功告成。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。