Android自定义日历滑动控件

  

本文实例为大家分享了Android自定义日历滑动控件的使用方法,供大家参考,具体内容如下

  

 Android自定义日历滑动控件“> <br/>
  </p>
  <p>最近公司项目需要做这个需求,自己才疏学浅,总算能写出个大概来,遂在这里记录下来。</p>
  <p> <>强分析</强> </p>
  <p>先来分析一下:<br/>
  </p>
  <p>首先,我们的需求是可以左右点击查看跳转到下一个月,中间的日历控件可以水平滚动选择日期,所以我们中间的日历控件用一个RecycleView来做,左右两位的为ImageVeiw。<br/>
  LRCalendarView总体流程:</p>
  <ul>
  <李>编写LRCalendarView的布局R.layout.calendar_view李</>
  <李>新建类LRCalendarView继承LinearLayout李</>
  <李> LRCalendarView添加布局R.layout.calendar_view李</>
  <李>数据初始化李</>
  <李>构建GalleryAdapter李</>
  <李>给RecycleView设置GalleryAdapter并且给左右按钮添加点击事件</李>
  <李>处理左右日历翻页逻辑李</>
  <李>按需要给RecycleView添加项的点击事件<br/>
  李</>
  </ul>
  <p> 1。R.layout.calendar_view </p>
  <p> </p>
  
  <pre类=   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   LinearLayout & lt;   xmlns: android=" http://schemas.android.com/apk/res/android "   android: id=癅 + id/activity_main”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:背景=" # ffffff "   android:填充=" 5 dp "   面向android:="水平"比;      & lt; ImageView   android: id=癅 + id/iv_left”   android: layout_gravity=" center_vertical "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "   android: layout_width=" 31 dp "   android: layout_height=?1 dp/比;      & lt; android.support.v7.widget.RecyclerView   android: layout_marginLeft=" 5 dp "   android: layout_marginRight=" 5 dp "   android: id=癅 + id/recyclerView”   android: layout_width=" wrap_content "   android: layout_weight=" 1 "   android: layout_height=" 50 dp”   android: layout_centerVertical=" true "   android:背景=" # ffffff "   android:滚动条="没有"/比;      & lt; ImageView   android: id=癅 + id/iv_right”   android: layout_gravity=" center_vertical "   android: src=" https://www.yisu.com/zixun/@drawable ic_launcher "   android: layout_width=" 30 dp "   android: layout_height=" 30 dp/比;      & lt;/LinearLayout>      

2。新建类LRCalendarView继承LinearLayout并添加布局

  

        公开课LRCalendarView LinearLayout{延伸      私人上下文语境;   私人ImageView ivLeft ivRight;   私人RecyclerView mRecyclerView;   私人GalleryAdapter mAdapter;   私人Listdata=https://www.yisu.com/zixun/new ArrayList <> ();   私人int mCurrYear mCurrMonth mCurrDay;   私人int mSelYear mSelMonth mSelDay;//今天的日期的位置   私人int todayPos;      公共LRCalendarView(上下文语境、AttributeSet attrs int defStyleAttr) {   超级(上下文、attrs defStyleAttr);   这一点。上下文=上下文;   setupView(上下文);   }      公共LRCalendarView(上下文语境,AttributeSet attrs) {   这(上下文、attrs 0);   }      公共LRCalendarView(上下文语境){   这(上下文,null);   }/* *   *初始化控件   */私人空间setupView(最终上下文语境){   视图视图=LayoutInflater.from(上下文).inflate (R.layout.calendar_view, null);   this.addView(查看);      data=init ();   mRecyclerView=(RecyclerView) findViewById (R.id.recyclerView);   ivLeft=(ImageView) findViewById (R.id.iv_left);   ivRight=(ImageView) findViewById (R.id.iv_right);//设置马槽   LinearLayoutManager LinearLayoutManager=new LinearLayoutManager(上下文);   linearLayoutManager.setOrientation (LinearLayoutManager.HORIZONTAL);   mRecyclerView.setLayoutManager (linearLayoutManager);//设置适配器   mAdapter=new GalleryAdapter(上下文,init ());   mRecyclerView.setAdapter (mAdapter);      }   }      

        公共类细胞{      私人的字符串;   私人弦月;   私人int mCurrDay;   私人布尔isSelect;      公共int getmCurrDay () {   返回mCurrDay;   }      公共空间setmCurrDay (int mCurrDay) {   这一点。mCurrDay=mCurrDay;   }      公共字符串getDay () {   返回一天;   }      公共空间setDay (String) {   这一点。一天=一天;   }      公共字符串getMonth () {   返回月;   }      公共空间setMonth (String) {   这一点。月=月;   }      公共布尔isSelect () {   返回isSelect;   }      公共空间setSelect(布尔选择){   isSelect=选择;   }      }      

Android自定义日历滑动控件