weeks;
25 | private GetViewHelper getViewHelper;
26 |
27 | public WeekAdapter(GetViewHelper getViewHelper) {
28 | this.getViewHelper = getViewHelper;
29 | String[] weekdays = DateFormatSymbols.getInstance().getWeekdays();
30 | weeks = new ArrayList<>(Arrays.asList(weekdays));
31 | weeks.remove(0);
32 | }
33 |
34 | @Override
35 | public int getCount() {
36 | return DAYS_OF_WEEK;
37 | }
38 |
39 | @Override
40 | public Object getItem(int position) {
41 | return weeks.get(position);
42 | }
43 |
44 | @Override
45 | public long getItemId(int position) {
46 | return position;
47 | }
48 |
49 | @Override
50 | public View getView(int position, View convertView, ViewGroup parent) {
51 | return getViewHelper.getWeekView(position, convertView, parent, weeks.get(position));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/com/beiing/weekcalendar/listener/CustomPagerChandeListender.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar.listener;
2 |
3 | import android.support.v4.view.ViewPager;
4 |
5 | /**
6 | * Created by linechen on 2017/5/22.
7 | * 描述:
8 | *
9 | */
10 |
11 | public class CustomPagerChandeListender implements ViewPager.OnPageChangeListener {
12 | @Override
13 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
14 |
15 | }
16 |
17 | @Override
18 | public void onPageSelected(int position) {
19 |
20 | }
21 |
22 | @Override
23 | public void onPageScrollStateChanged(int state) {
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/com/beiing/weekcalendar/listener/DateSelectListener.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar.listener;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | /**
6 | * Created by linechen on 2017/5/19.
7 | * 描述:
8 | *
9 | */
10 |
11 | public interface DateSelectListener {
12 | void onDateSelect(DateTime selectDate);
13 | }
14 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/com/beiing/weekcalendar/listener/GetViewHelper.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar.listener;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 |
6 | import org.joda.time.DateTime;
7 |
8 | /**
9 | * Created by linechen on 2017/5/19.
10 | * 描述:
11 | *
12 | */
13 |
14 | public interface GetViewHelper {
15 |
16 | View getDayView(int position, View convertView, ViewGroup parent, DateTime dateTime, boolean select);
17 |
18 | View getWeekView(int position, View convertView, ViewGroup parent, String week);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/com/beiing/weekcalendar/listener/WeekChangeListener.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar.listener;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | /**
6 | * Created by linechen on 2017/5/22.
7 | * 描述:
8 | *
9 | */
10 |
11 | public interface WeekChangeListener {
12 | void onWeekChanged(DateTime firstDayOfWeek);
13 | }
14 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/com/beiing/weekcalendar/utils/CalendarUtil.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar.utils;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | /**
6 | * Created by linechen on 2017/5/19.
7 | * 描述:
8 | *
9 | */
10 |
11 | public class CalendarUtil {
12 | public static boolean isSameDay(DateTime t1, DateTime t2){
13 | return t1.toString("yyyyMMdd").equals(t2.toString("yyyyMMdd"));
14 | }
15 |
16 | public static boolean isToday(DateTime t){
17 | return isSameDay(t, new DateTime());
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/jackwharton_salvage/RecycleBin.java:
--------------------------------------------------------------------------------
1 | package jackwharton_salvage;
2 |
3 | import android.os.Build;
4 | import android.util.SparseArray;
5 | import android.view.View;
6 |
7 | /**
8 | * The RecycleBin facilitates reuse of views across layouts. The RecycleBin has two levels of
9 | * storage: ActiveViews and ScrapViews. ActiveViews are those views which were onscreen at the
10 | * start of a layout. By construction, they are displaying current information. At the end of
11 | * layout, all views in ActiveViews are demoted to ScrapViews. ScrapViews are old views that
12 | * could potentially be used by the adapter to avoid allocating views unnecessarily.
13 | *
14 | * This class was taken from Android's implementation of {@link android.widget.AbsListView} which
15 | * is copyrighted 2006 The Android Open Source Project.
16 | */
17 | public class RecycleBin {
18 | /**
19 | * Views that were on screen at the start of layout. This array is populated at the start of
20 | * layout, and at the end of layout all view in activeViews are moved to scrapViews.
21 | * Views in activeViews represent a contiguous range of Views, with position of the first
22 | * view store in mFirstActivePosition.
23 | */
24 | private View[] activeViews = new View[0];
25 | private int[] activeViewTypes = new int[0];
26 |
27 | /** Unsorted views that can be used by the adapter as a convert view. */
28 | private SparseArray[] scrapViews;
29 |
30 | private int viewTypeCount;
31 |
32 | private SparseArray currentScrapViews;
33 |
34 | public void setViewTypeCount(int viewTypeCount) {
35 | if (viewTypeCount < 1) {
36 | throw new IllegalArgumentException("Can't have a viewTypeCount < 1");
37 | }
38 | //noinspection unchecked
39 | SparseArray[] scrapViews = new SparseArray[viewTypeCount];
40 | for (int i = 0; i < viewTypeCount; i++) {
41 | scrapViews[i] = new SparseArray();
42 | }
43 | this.viewTypeCount = viewTypeCount;
44 | currentScrapViews = scrapViews[0];
45 | this.scrapViews = scrapViews;
46 | }
47 |
48 | protected boolean shouldRecycleViewType(int viewType) {
49 | return viewType >= 0;
50 | }
51 |
52 | /** @return A view from the ScrapViews collection. These are unordered. */
53 | View getScrapView(int position, int viewType) {
54 | if (viewTypeCount == 1) {
55 | return retrieveFromScrap(currentScrapViews, position);
56 | } else if (viewType >= 0 && viewType < scrapViews.length) {
57 | return retrieveFromScrap(scrapViews[viewType], position);
58 | }
59 | return null;
60 | }
61 |
62 | /**
63 | * Put a view into the ScrapViews list. These views are unordered.
64 | *
65 | * @param scrap The view to add
66 | */
67 | void addScrapView(View scrap, int position, int viewType) {
68 | if (viewTypeCount == 1) {
69 | currentScrapViews.put(position, scrap);
70 | } else {
71 | scrapViews[viewType].put(position, scrap);
72 | }
73 |
74 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
75 | scrap.setAccessibilityDelegate(null);
76 | }
77 | }
78 |
79 | /** Move all views remaining in activeViews to scrapViews. */
80 | void scrapActiveViews() {
81 | final View[] activeViews = this.activeViews;
82 | final int[] activeViewTypes = this.activeViewTypes;
83 | final boolean multipleScraps = viewTypeCount > 1;
84 |
85 | SparseArray scrapViews = currentScrapViews;
86 | final int count = activeViews.length;
87 | for (int i = count - 1; i >= 0; i--) {
88 | final View victim = activeViews[i];
89 | if (victim != null) {
90 | int whichScrap = activeViewTypes[i];
91 |
92 | activeViews[i] = null;
93 | activeViewTypes[i] = -1;
94 |
95 | if (!shouldRecycleViewType(whichScrap)) {
96 | continue;
97 | }
98 |
99 | if (multipleScraps) {
100 | scrapViews = this.scrapViews[whichScrap];
101 | }
102 | scrapViews.put(i, victim);
103 |
104 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
105 | victim.setAccessibilityDelegate(null);
106 | }
107 | }
108 | }
109 |
110 | pruneScrapViews();
111 | }
112 |
113 | /**
114 | * Makes sure that the size of scrapViews does not exceed the size of activeViews.
115 | * (This can happen if an adapter does not recycle its views).
116 | */
117 | private void pruneScrapViews() {
118 | final int maxViews = activeViews.length;
119 | final int viewTypeCount = this.viewTypeCount;
120 | final SparseArray[] scrapViews = this.scrapViews;
121 | for (int i = 0; i < viewTypeCount; ++i) {
122 | final SparseArray scrapPile = scrapViews[i];
123 | int size = scrapPile.size();
124 | final int extras = size - maxViews;
125 | size--;
126 | for (int j = 0; j < extras; j++) {
127 | scrapPile.remove(scrapPile.keyAt(size--));
128 | }
129 | }
130 | }
131 |
132 | static View retrieveFromScrap(SparseArray scrapViews, int position) {
133 | int size = scrapViews.size();
134 | if (size > 0) {
135 | // See if we still have a view for this position.
136 | for (int i = 0; i < size; i++) {
137 | int fromPosition = scrapViews.keyAt(i);
138 | View view = scrapViews.get(fromPosition);
139 | if (fromPosition == position) {
140 | scrapViews.remove(fromPosition);
141 | return view;
142 | }
143 | }
144 | int index = size - 1;
145 | View r = scrapViews.valueAt(index);
146 | scrapViews.remove(scrapViews.keyAt(index));
147 | return r;
148 | } else {
149 | return null;
150 | }
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/java/jackwharton_salvage/RecyclingPagerAdapter.java:
--------------------------------------------------------------------------------
1 | package jackwharton_salvage;
2 |
3 | import android.support.v4.view.PagerAdapter;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.AdapterView;
7 |
8 | /**
9 | * A {@link PagerAdapter} which behaves like an {@link android.widget.Adapter} with view types and
10 | * view recycling.
11 | */
12 | public abstract class RecyclingPagerAdapter extends PagerAdapter {
13 | static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE;
14 |
15 | private final RecycleBin recycleBin;
16 | private DataChangeListener mDataChangeListener;
17 |
18 | public interface DataChangeListener {
19 | void notifyDataChange();
20 | }
21 |
22 | public DataChangeListener getDataChangeListener() {
23 | return mDataChangeListener;
24 | }
25 |
26 | public void setDataChangeListener(DataChangeListener dataChangeListener) {
27 | this.mDataChangeListener = dataChangeListener;
28 | }
29 |
30 | public RecyclingPagerAdapter() {
31 | this(new RecycleBin());
32 | }
33 |
34 | RecyclingPagerAdapter(RecycleBin recycleBin) {
35 | this.recycleBin = recycleBin;
36 | recycleBin.setViewTypeCount(getViewTypeCount());
37 | }
38 |
39 | @Override
40 | public void notifyDataSetChanged() {
41 | recycleBin.scrapActiveViews();
42 | if(mDataChangeListener!=null)
43 | mDataChangeListener.notifyDataChange();
44 | super.notifyDataSetChanged();
45 | }
46 |
47 | @Override
48 | public final Object instantiateItem(ViewGroup container, int position) {
49 | int viewType = getItemViewType(position);
50 | View view = null;
51 | if (viewType != IGNORE_ITEM_VIEW_TYPE) {
52 | view = recycleBin.getScrapView(position, viewType);
53 | }
54 | view = getView(position, view, container);
55 | container.addView(view);
56 | return view;
57 | }
58 |
59 | @Override
60 | public final void destroyItem(ViewGroup container, int position, Object object) {
61 | View view = (View) object;
62 | container.removeView(view);
63 | int viewType = getItemViewType(position);
64 | if (viewType != IGNORE_ITEM_VIEW_TYPE) {
65 | recycleBin.addScrapView(view, position, viewType);
66 | }
67 | }
68 |
69 | @Override
70 | public final boolean isViewFromObject(View view, Object object) {
71 | return view == object;
72 | }
73 |
74 | /**
75 | *
76 | * Returns the number of types of Views that will be created by
77 | * {@link #getView}. Each type represents a set of views that can be
78 | * converted in {@link #getView}. If the adapter always returns the same
79 | * type of View for all items, this method should return 1.
80 | *
81 | *
82 | * This method will only be called when when the adapter is set on the
83 | * the {@link AdapterView}.
84 | *
85 | *
86 | * @return The number of types of Views that will be created by this adapter
87 | */
88 | public int getViewTypeCount() {
89 | return 1;
90 | }
91 |
92 | /**
93 | * Get the type of View that will be created by {@link #getView} for the specified item.
94 | *
95 | * @param position The position of the item within the adapter's data set whose view type we
96 | * want.
97 | * @return An integer representing the type of View. Two views should share the same type if one
98 | * can be converted to the other in {@link #getView}. Note: Integers must be in the
99 | * range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can
100 | * also be returned.
101 | * @see #IGNORE_ITEM_VIEW_TYPE
102 | */
103 | @SuppressWarnings("UnusedParameters") // Argument potentially used by subclasses.
104 | public int getItemViewType(int position) {
105 | return 0;
106 | }
107 |
108 | /**
109 | * Get a View that displays the data at the specified position in the data set. You can either
110 | * create a View manually or inflate it from an XML layout file. When the View is inflated, the
111 | * parent View (GridView, ListView...) will apply default layout parameters unless you use
112 | * {@link android.view.LayoutInflater#inflate(int, ViewGroup, boolean)}
113 | * to specify a root view and to prevent attachment to the root.
114 | *
115 | * @param position The position of the item within the adapter's data set of the item whose view
116 | * we want.
117 | * @param convertView The old view to reuse, if possible. Note: You should check that this view
118 | * is non-null and of an appropriate type before using. If it is not possible to convert
119 | * this view to display the correct data, this method can create a new view.
120 | * Heterogeneous lists can specify their number of view types, so that this View is
121 | * always of the right type (see {@link #getViewTypeCount()} and
122 | * {@link #getItemViewType(int)}).
123 | * @param container The parent that this view will eventually be attached to
124 | * @return A View corresponding to the data at the specified position.
125 | */
126 | public abstract View getView(int position, View convertView, ViewGroup container);
127 | }
128 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/layout/item_calendar.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/layout/layout_calendar_content.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/layout/layout_calender_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 48dp
5 |
6 | 64dp
7 |
8 |
--------------------------------------------------------------------------------
/weekcalendar/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | weekcalendar
3 |
4 |
--------------------------------------------------------------------------------
/weekcalendar/src/test/java/com/beiing/weekcalendar/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.beiing.weekcalendar;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/布局.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/Week_Calendar/d940ee40fbe7e112f9aeea6cbe29c78d8ca43883/布局.jpg
--------------------------------------------------------------------------------