Ich habe einen Kalender, den ich 6 Monate zurück gehen muss, nicht 6 Monate nach vorne und mit ein wenig Mühe, damit es 6 Monate zurück gehen. Wie würde ich diesen Kalender 6 Monate zurückgehen lassen, beginnend mit dem aktuellen Monat, und dann bis zu den letzten 6 Monaten scrollen?Kalender (GridView) mit Monats-Header (Google HeaderView)
Außerdem verwende ich die Google HeaderView, um eine Kopfansicht für die Gridview des Kalenders anzuzeigen. Wie würde ich Leerzeichen nach dem Ende jedes Monats hinzufügen, um die Kopfzeile dieses Monats in diesem Bereich platzieren zu können? Der Google HeaderView scheint nur einmal zu Beginn der GridView zu füllen.
public class CalendarAt;ctivity extends Activity implements AppCompatCallback {
private AppCompatDelegate delegate;
Toolbar mToolbar;
private HeaderGridView mGridView;
private TextView mHeaderTextView;
private LinearLayout calendar_view_header;
private ImageView mCalendarButtonClicked, mCalendarButton, settingsBtn, settingsBtnChecked;
private PopupWindow mPopUp;
//default 6 months +- half a day
private static final int DAYS_COUNT = 182;
// current displayed month
private Calendar currentDate = Calendar.getInstance();
private EventListener eventListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HashSet<Date> events = new HashSet<>();
events.add(new Date());
//Show Toolbar without extending AppCompatActivity
delegate = AppCompatDelegate.create(this, this);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.calendar_main);
delegate.setSupportActionBar(mToolbar);
mGridView = (HeaderGridView) findViewById(R.id.calendar_grid);
settingsBtn = (ImageView) findViewById(R.id.settingsBtn);
settingsBtnChecked = (ImageView) findViewById(R.id.settingsBtnClicked);
mCalendarButton = (ImageView) findViewById(R.id.eReceiptSearchBtn);
mCalendarButton.setVisibility(View.GONE);
mCalendarButtonClicked = (ImageView) findViewById(R.id.eReceiptSearchBtnClicked);
calendar_view_header = (LinearLayout) findViewById(R.id.calendar_view_header);
mCalendarButtonClicked.setVisibility(View.VISIBLE);
updateCalendar();
mCalendarButtonClicked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
settingsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPreferencesPopup();
mCalendarButtonClicked.setVisibility(View.GONE);
mCalendarButton.setVisibility(View.VISIBLE);
}
});
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Replace with just EReceipts for the specific date which was clicked on
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
public void updateCalendar() {
updateCalendar(null);
}
private void updateCalendar(HashSet<Date> events) {
Calendar calendar = (Calendar) currentDate.clone();
int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1;
ArrayList<Date> cells = new ArrayList<>();
calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell);
while (cells.size() < DAYS_COUNT) {
cells.add(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 6);
}
String monthname=(String)android.text.format.DateFormat.format("MMMM", new Date());
String year = (String)android.text.format.DateFormat.format("yyyy", new Date());
Log.i("HERE", "updateCalendar: " + monthname + " " + year);
View headerView = getLayoutInflater().inflate(R.layout.calendar_view_header, null);
mGridView.addHeaderView(headerView);
mGridView.setAdapter(new CalendarAdapter(getApplicationContext(), cells, events));
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
private class CalendarAdapter extends ArrayAdapter<Date> {
private HashSet<Date> eventDays;
private LayoutInflater inflater;
public CalendarAdapter(Context context, ArrayList<Date> days, HashSet<Date> eventDays) {
super(context, R.layout.control_calendar_day, days);
this.eventDays = eventDays;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Date date = getItem(position);
int day = date.getDate();
int month = date.getMonth();
int year = date.getYear();
String fullDate;
if (view == null) {
view = inflater.inflate(R.layout.control_calendar_day, parent, false);
view.setBackgroundResource(0);
if (eventDays != null) {
for (Date eventDate : eventDays) {
if (eventDate.getDate() == day && eventDate.getMonth() == month &&
eventDate.getYear() == year) {
view.setBackgroundResource(R.color.colorBlack);
((TextView) view).setTypeface(null, Typeface.BOLD);
break;
}
}
}
if(day == Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH)) {
Log.i("ADAM", "getView: " + day);
}
}
((TextView) view).setText(String.valueOf(date.getDate()));
return view;
}
}
public void setEventListener(EventListener eventListener) {
this.eventListener = eventListener;
}
public interface EventListener {
void onDayLongPress(Date date);
}
private void showPreferencesPopup() {
LayoutInflater mInflater = (LayoutInflater) CalendarActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mPopUpLayout = mInflater.inflate(R.layout.settings_btn_click,
(ViewGroup) findViewById(R.id.eReceiptsSettingsPopUp));
mPopUp = new PopupWindow(mPopUpLayout, FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, true);
settingsBtn.setVisibility(View.GONE);
settingsBtnChecked.setVisibility(View.VISIBLE);
mPopUp.setOutsideTouchable(true);
mPopUp.showAsDropDown(settingsBtn, 0, 0);
mPopUp.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mPopUp.dismiss();
settingsBtn.setVisibility(View.VISIBLE);
settingsBtnChecked.setVisibility(View.GONE);
}
});
}
}