Skip to content

Commit

Permalink
加入adadpter模式
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Nov 6, 2019
1 parent 83c0b57 commit 7144eea
Show file tree
Hide file tree
Showing 20 changed files with 625 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.necer.ncalendar;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.necer.calendar.MonthCalendar;
import com.necer.painter.CalendarAdapter;

import org.joda.time.LocalDate;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TestAdapterCalendarActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_adapter);


MonthCalendar monthCalendar = findViewById(R.id.monthCalendar);

monthCalendar.setCalendarAdapter(new TestAdapter());
}


public static class TestAdapter extends CalendarAdapter {

@Override
public View getCalendarView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.item_calendar, null);
return view;
}

@Override
public void onBindToadyView(View view, LocalDate localDate, boolean isCheck) {
TextView textView = view.findViewById(R.id.tv_item);
textView.setText(localDate.getDayOfMonth()+"");
view.setBackgroundColor(isCheck ? Color.parseColor("#ff00ff") : Color.parseColor("#ffffff"));

}

@Override
public void onBindCurrentMonthOrWeekView(View view, LocalDate localDate, boolean isCheck) {
TextView textView = view.findViewById(R.id.tv_item);
textView.setText(localDate.getDayOfMonth()+"");
view.setBackgroundColor(isCheck ? Color.parseColor("#ff00ff") : Color.parseColor("#ffffff"));

}
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_month_adapter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.necer.calendar.MonthCalendar
android:id="@+id/monthCalendar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/item_calendar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:text="asdf"
android:layout_height="wrap_content"/>

</LinearLayout>
71 changes: 0 additions & 71 deletions ncalendar/src/main/java/com/necer/adapter/BaseCalendarAdapter.java

This file was deleted.

81 changes: 81 additions & 0 deletions ncalendar/src/main/java/com/necer/adapter/BasePagerAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.necer.adapter;

import android.content.Context;

import androidx.viewpager.widget.PagerAdapter;

import android.view.View;
import android.view.ViewGroup;

import com.necer.enumeration.CalendarBuild;
import com.necer.utils.Attrs;
import com.necer.view.CalendarView;
import com.necer.view.ICalendarView;

import org.joda.time.LocalDate;

/**
* Created by necer on 2017/8/25.
* QQ群:127278900
*/

public abstract class BasePagerAdapter extends PagerAdapter {


protected Context mContext;
protected int mCount;//总页数
protected int mCurrIndex;//总页数
protected LocalDate mInitializeDate;
protected int mFirstDayOfWeek;
protected boolean mIsAllMonthSixLine;
protected CalendarBuild mCalendarBuild;

public BasePagerAdapter(Context context, CalendarBuild calendarBuild, LocalDate initializeDate, int count, int currIndex, int firstDayOfWeek, boolean isAllMonthSixLine) {
this.mContext = context;
this.mCalendarBuild = calendarBuild;
this.mInitializeDate = initializeDate;
this.mCount = count;
this.mCurrIndex = currIndex;
this.mFirstDayOfWeek = firstDayOfWeek;
this.mIsAllMonthSixLine = isAllMonthSixLine;
}

// public BasePagerAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
// this.mContext = context;
// this.mInitializeDate = initializeDate;
// this.mCount = getIntervalCount(startDate, endDate, attrs.firstDayOfWeek) + 1;
// this.mCurr = getIntervalCount(startDate, initializeDate, attrs.firstDayOfWeek);
// this.mAttrs = attrs;
// }

@Override
public int getCount() {
return mCount;
}


@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ICalendarView iCalendarView = getCalendarView(container, position);
((View) iCalendarView).setTag(position);
container.addView((View) iCalendarView);
return iCalendarView;
}

protected abstract ICalendarView getCalendarView(ViewGroup container, int position);

protected abstract int getIntervalCount(LocalDate startDate, LocalDate endDate, int weekFirstDayType);


}

This file was deleted.

47 changes: 47 additions & 0 deletions ncalendar/src/main/java/com/necer/adapter/MonthPagerAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.necer.adapter;

import android.content.Context;
import android.view.ViewGroup;

import com.necer.enumeration.CalendarBuild;
import com.necer.utils.Attrs;
import com.necer.utils.CalendarUtil;
import com.necer.view.ICalendarView;
import com.necer.view.MonthView;
import com.necer.view.MonthView2;

import org.joda.time.LocalDate;

import java.util.List;

/**
* Created by necer on 2018/9/11.
* qq群:127278900
*/
public class MonthPagerAdapter extends BasePagerAdapter {


public MonthPagerAdapter(Context context, CalendarBuild calendarBuild, LocalDate initializeDate, int count, int currIndex, int firstDayOfWeek, boolean isAllMonthSixLine) {
super(context, calendarBuild, initializeDate, count, currIndex, firstDayOfWeek, isAllMonthSixLine);
}

@Override
protected ICalendarView getCalendarView(ViewGroup container, int position) {
LocalDate localDate = mInitializeDate.plusMonths(position - mCurrIndex);
List<LocalDate> dateList = CalendarUtil.getMonthCalendar(localDate, mFirstDayOfWeek, mIsAllMonthSixLine);

// return new MonthView(mContext, container, localDate, dateList);

if (mCalendarBuild == CalendarBuild.DRAW) {
return new MonthView(mContext, container, localDate, dateList);
} else {
return new MonthView2(mContext, container, localDate, dateList);
}
// return new MonthView2(mContext, container, localDate, dateList);
}

@Override
protected int getIntervalCount(LocalDate startDate, LocalDate endDate, int type) {
return CalendarUtil.getIntervalMonths(startDate, endDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.view.ViewGroup;

import com.necer.enumeration.CalendarBuild;
import com.necer.utils.Attrs;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
Expand All @@ -16,15 +17,17 @@
* Created by necer on 2018/9/11.
* qq群:127278900
*/
public class WeekCalendarAdapter extends BaseCalendarAdapter {
public WeekCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
super(context, startDate, endDate, initializeDate, attrs);
public class WeekPagerAdapter extends BasePagerAdapter {


public WeekPagerAdapter(Context context, CalendarBuild calendarBuild, LocalDate initializeDate, int count, int currIndex, int firstDayOfWeek, boolean isAllMonthSixLine) {
super(context, calendarBuild, initializeDate, count, currIndex, firstDayOfWeek, isAllMonthSixLine);
}

@Override
protected CalendarView getCalendarView(ViewGroup container, int position) {
LocalDate localDate = mInitializeDate.plusDays((position - mCurr) * 7);
List<LocalDate> dateList = CalendarUtil.getWeekCalendar(localDate, mAttrs.firstDayOfWeek);
LocalDate localDate = mInitializeDate.plusDays((position - mCurrIndex) * 7);
List<LocalDate> dateList = CalendarUtil.getWeekCalendar(localDate, mFirstDayOfWeek);
return new WeekView(mContext, container, localDate, dateList);
}

Expand Down
Loading

0 comments on commit 7144eea

Please sign in to comment.