带月标题的日历(GridView)(Google标头查看)

时间:2016-08-09 14:27:47

标签: android android-layout android-gridview android-calendar

我有一个日程表,我需要在6个月前退出,而不是6个月前进,并且在6个月前让它变得有点麻烦。我如何让这个日历从当月开始,然后向下滚动到过去的6个月呢?

此外,我使用Google HeaderView显示日历网格视图的标题视图。如何在每月结束后添加空格以便能够将该月的标题放在该区域? Google HeaderView似乎只在GridView的开头填充一次。

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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我只使用内置的Calendar小部件,而不使用完全自定义的日历。我无法让自定义日历按照我想要的方式工作。

使用内置的Calendar小部件,我只能让用户只查看当前月份,同时也停止日历在用户设备的当前日期返回超过6个月。