当前位置: 移动技术网 > 移动技术>移动开发>Android > android实现切换日期左右无限滑动效果

android实现切换日期左右无限滑动效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

大家在进行安卓开发的时候,经常用到日期比如在课程安排,工作安排,日志等地方,今天给大家带来的是让日期左右无限的滑动,并支持自定义显示效果的方法。一起来学习下。

以上是本次所要达到的效果

使用说明:

xml布局

新建xml布局

recyclerview的layout_behavior为com.ldf.calendar.behavior.recyclerviewbehavior

<android.support.design.widget.coordinatorlayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
 
    <com.ldf.calendar.view.monthpager
      android:id="@+id/calendar_view"
      android:layout_width="match_parent"
      android:layout_height="300dp"
      android:background="#fff">
    </com.ldf.calendar.view.monthpager>
 
    <android.support.v7.widget.recyclerview
      android:id="@+id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_behavior="com.ldf.calendar.behavior.recyclerviewbehavior"
      android:background="#c2c2c2"
      android:layout_gravity="bottom"/>
 
  </android.support.design.widget.coordinatorlayout>

自定义日历样式
新建customdayview继承自dayview并重写refreshcontent 和 copy 两个方法

@override
  public void refreshcontent() {
    //你的代码 你可以在这里定义你的显示规则
    super.refreshcontent();
  }
 
  @override
  public idayrenderer copy() {
    return new customdayview(context , layoutresource);
  }

新建customdayview实例,并作为参数构建calendarviewadapter

customdayview customdayview = new customdayview(
      context , r.layout.custom_day);
  calendaradapter = new calendarviewadapter(
        context ,
        onselectdatelistener ,
        calendar.month_type ,
        customdayview);

初始化view
目前来看 相比于dialog选择日历 我的控件更适合于activity/fragment在activity的oncreate 或者fragment的oncreateview 你需要实现这两个方法来启动日历并装填进数据

@override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_syllabus);
    initcalendarview();
  }
  
  private void initcalendarview() {
    initlistener();
    customdayview customdayview = new customdayview(
      context , r.layout.custom_day);
    calendaradapter = new calendarviewadapter(
        context ,
        onselectdatelistener ,
        calendar.month_type ,
        customdayview);
    initmarkdata();
    initmonthpager();
  }

使用此方法回调日历点击事件

private void initlistener() {
    onselectdatelistener = new onselectdatelistener() {
      @override
      public void onselectdate(calendardate date) {
        //your code
      }
 
      @override
      public void onselectothermonth(int offset) {
        //偏移量 -1表示上一个月 , 1表示下一个月
        monthpager.selectothermonth(offset);
      }
    };
  }

使用此方法初始化日历标记数据

private void initmarkdata() {
    hashmap markdata = new hashmap<>();
    //1表示红点,0表示灰点
    markdata.put("2017-8-9" , "1");
    markdata.put("2017-7-9" , "0");
    markdata.put("2017-6-9" , "1");
    markdata.put("2017-6-10" , "0");
    calendaradapter.setmarkdata(markdata);
  }

使用此方法给monthpager添加上相关监听

monthpager.addonpagechangelistener(new monthpager.onpagechangelistener() {
      @override
      public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {
      }
 
      @override
      public void onpageselected(int position) {
        mcurrentpage = position;
        currentcalendars = calendaradapter.getallitems();
        if(currentcalendars.get(position % currentcalendars.size()) instanceof calendar){
          //you code
        }
      }
 
      @override
      public void onpagescrollstatechanged(int state) {
      }
    });

重写onwindowfocuschanged方法,使用此方法得知calendar和day的尺寸

 @override
  public void onwindowfocuschanged(boolean hasfocus) {
    super.onwindowfocuschanged(hasfocus);
    if(hasfocus && !initiated) {
      calendardate today = new calendardate();
      calendaradapter.notifydatachanged(today);
      initiated = true;
    }
  }
download

gradle: step 1. add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
  ...
  maven { url 'https://www.jitpack.io' }
  }
}
step 2. add the dependency

  dependencies {
      compile 'com.github.magicmashroom:supercalendar:v1.3.1'
  }

以上就是本次效果所用到的所有代码和说明,大家有任何问题可以在下方的留言地方讨论。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网