start replacing month view with a viewpager

This commit is contained in:
tibbi 2016-09-15 12:29:25 +02:00
parent 111e9171bf
commit b88e3b89df
5 changed files with 132 additions and 17 deletions

View File

@ -1,18 +1,13 @@
package com.simplemobiletools.calendar.activities;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
@ -24,11 +19,14 @@ import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.adapters.MyPagerAdapter;
import com.simplemobiletools.calendar.models.Day;
import com.simplemobiletools.calendar.views.MyViewPager;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindDimen;
@ -37,15 +35,18 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends SimpleActivity implements Calendar {
@BindView(R.id.top_left_arrow) ImageView mLeftArrow;
/*@BindView(R.id.top_left_arrow) ImageView mLeftArrow;
@BindView(R.id.top_right_arrow) ImageView mRightArrow;
@BindView(R.id.top_text) TextView mMonthTV;
@BindView(R.id.calendar_holder) View mCalendarHolder;
@BindView(R.id.calendar_holder) View mCalendarHolder;*/
@BindView(R.id.view_pager) MyViewPager mPager;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
@BindDimen(R.dimen.activity_margin) int mActivityMargin;
private static final int PREFILLED_MONTHS = 73;
private CalendarImpl mCalendar;
private Resources mRes;
private String mPackageName;
@ -68,8 +69,8 @@ public class MainActivity extends SimpleActivity implements Calendar {
mTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.HIGH_ALPHA);
mWeakTextColor = Utils.adjustAlpha(baseColor, Constants.LOW_ALPHA);
mWeakTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.LOW_ALPHA);
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
//mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
//mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mSundayFirst = mConfig.getIsSundayFirst();
mPackageName = getPackageName();
@ -77,10 +78,15 @@ public class MainActivity extends SimpleActivity implements Calendar {
mTodayTextSize /= mRes.getDisplayMetrics().density;
setupLabels();
final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mCalendarHolder.getLayoutParams();
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
/*final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mCalendarHolder.getLayoutParams();
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);*/
mCalendar = new CalendarImpl(this, getApplicationContext());
final String today = new DateTime().toString(Formatter.DAYCODE_PATTERN);
final List<String> codes = getMonths(today);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), codes);
mPager.setAdapter(adapter);
}
@Override
@ -127,6 +133,16 @@ public class MainActivity extends SimpleActivity implements Calendar {
startActivity(intent);
}
private List<String> getMonths(String code) {
final List<String> months = new ArrayList<>(PREFILLED_MONTHS);
final DateTime today = Formatter.getDateTimeFromCode(code);
for (int i = -PREFILLED_MONTHS / 2; i <= PREFILLED_MONTHS / 2; i++) {
months.add(Formatter.getDayCodeFromDateTime(today.plusMonths(i)));
}
return months;
}
private void updateDays(List<Day> days) {
final int len = days.size();
@ -169,7 +185,7 @@ public class MainActivity extends SimpleActivity implements Calendar {
startActivity(intent);
}
@OnClick(R.id.top_left_arrow)
/*@OnClick(R.id.top_left_arrow)
public void leftArrowClicked() {
mCalendar.getPrevMonth();
}
@ -201,7 +217,7 @@ public class MainActivity extends SimpleActivity implements Calendar {
});
alertDialog.show();
}
}*/
private void hideDayPicker(DatePicker datePicker) {
final LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
@ -214,12 +230,12 @@ public class MainActivity extends SimpleActivity implements Calendar {
@Override
public void updateCalendar(String month, List<Day> days) {
updateMonth(month);
updateDays(days);
/*updateMonth(month);
updateDays(days);*/
}
private void updateMonth(String month) {
mMonthTV.setText(month);
//mMonthTV.setText(month);
}
private void setupLabels() {

View File

@ -0,0 +1,36 @@
package com.simplemobiletools.calendar.adapters;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.fragments.MonthFragment;
import java.util.List;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private final List<String> mCodes;
public MyPagerAdapter(FragmentManager fm, List<String> codes) {
super(fm);
mCodes = codes;
}
@Override
public int getCount() {
return mCodes.size();
}
@Override
public Fragment getItem(int position) {
final Bundle bundle = new Bundle();
final String code = mCodes.get(position);
bundle.putString(Constants.DAY_CODE, code);
final MonthFragment fragment = new MonthFragment();
fragment.setArguments(bundle);
return fragment;
}
}

View File

@ -0,0 +1,22 @@
package com.simplemobiletools.calendar.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.R;
public class MonthFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.calendar_layout, container, false);
final String code = getArguments().getString(Constants.DAY_CODE);
return view;
}
}

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.calendar.views;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
// we are creating a custom viewpager just to fix "pointerIndex out of range" exception at onInterceptTouchEvent
public class MyViewPager extends ViewPager {
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
try {
return super.onTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
}

View File

@ -6,7 +6,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/calendar_layout"/>
<com.simplemobiletools.calendar.views.MyViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/calendar_fab"