Skip to content

Commit

Permalink
Feature - Replace Header with XML Layout (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin41500 authored Aug 10, 2018
1 parent af9ecab commit ec536df
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.prolificinteractive.materialcalendarview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
Expand All @@ -11,10 +14,14 @@ class CalendarPager extends ViewPager {

private boolean pagingEnabled = true;

public CalendarPager(Context context) {
public CalendarPager(@NonNull final Context context) {
super(context);
}

public CalendarPager(@NonNull final Context context, @Nullable final AttributeSet attrs) {
super(context, attrs);
}

/**
* enable disable viewpager scroll
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prolificinteractive.materialcalendarview;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
Expand All @@ -14,11 +15,10 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -175,8 +175,8 @@ public class MaterialCalendarView extends ViewGroup {
private final TitleChanger titleChanger;

private final TextView title;
private final DirectionButton buttonPast;
private final DirectionButton buttonFuture;
private final ImageView buttonPast;
private final ImageView buttonFuture;
private final CalendarPager pager;
private CalendarPagerAdapter<?> adapter;
private CalendarDay currentMonth;
Expand Down Expand Up @@ -257,11 +257,14 @@ public MaterialCalendarView(Context context, AttributeSet attrs) {
setClipToPadding(true);
}

buttonPast = new DirectionButton(getContext());
buttonPast.setContentDescription(getContext().getString(R.string.previous));
title = new AppCompatTextView(getContext());
buttonFuture = new DirectionButton(getContext());
buttonFuture.setContentDescription(getContext().getString(R.string.next));
final LayoutInflater inflater =
(LayoutInflater) getContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE);
final View content = inflater.inflate(R.layout.calendar_view, null, false);

topbar = content.findViewById(R.id.header);
buttonPast = content.findViewById(R.id.previous);
title = content.findViewById(R.id.month_name);
buttonFuture = content.findViewById(R.id.next);
pager = new CalendarPager(getContext());

buttonPast.setOnClickListener(onClickListener);
Expand Down Expand Up @@ -415,22 +418,7 @@ public void transformPage(View page, float position) {
}

private void setupChildren() {
topbar = new LinearLayout(getContext());
topbar.setOrientation(LinearLayout.HORIZONTAL);
topbar.setClipChildren(false);
topbar.setClipToPadding(false);
addView(topbar, new LayoutParams(1));

buttonPast.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
topbar.addView(buttonPast, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));

title.setGravity(Gravity.CENTER);
topbar.addView(title, new LinearLayout.LayoutParams(
0, LayoutParams.MATCH_PARENT, DEFAULT_DAYS_IN_WEEK - 2
));

buttonFuture.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
topbar.addView(buttonFuture, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
addView(topbar);

pager.setId(R.id.mcv_pager);
pager.setOffscreenPageLimit(1);
Expand All @@ -441,8 +429,8 @@ private void setupChildren() {

private void updateUi() {
titleChanger.change(currentMonth);
buttonPast.setEnabled(canGoBack());
buttonFuture.setEnabled(canGoForward());
enableView(buttonPast, canGoBack());
enableView(buttonFuture, canGoForward());
}

/**
Expand Down Expand Up @@ -2008,4 +1996,15 @@ private void commit(State state) {
invalidateDecorators();
updateUi();
}

/**
* Used for enabling or disabling views, while also changing the alpha.
*
* @param view The view to enable or disable.
* @param enable Whether to enable or disable the view.
*/
private static void enableView(final View view, final boolean enable) {
view.setEnabled(enable);
view.setAlpha(enable ? 1f : 0.1f);
}
}
45 changes: 45 additions & 0 deletions library/src/main/res/layout/calendar_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>

<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/mcv_arrow_margin"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/previous"
android:padding="@dimen/mcv_arrow_padding"
app:srcCompat="@drawable/mcv_action_previous"
/>

<TextView
android:id="@+id/month_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
tools:text="August 2018"
style="?android:attr/textAppearanceMedium"
/>

<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/mcv_arrow_margin"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/next"
android:padding="@dimen/mcv_arrow_padding"
app:srcCompat="@drawable/mcv_action_next"
/>

</LinearLayout>
5 changes: 5 additions & 0 deletions library/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="mcv_arrow_padding">4dp</dimen>
<dimen name="mcv_arrow_margin">4dp</dimen>
</resources>

0 comments on commit ec536df

Please sign in to comment.