Android weather forecast - calendar view

The calendar viewing function displays the current year, month and day by default, with today's date in black font on a gray background, other dates in this month in white font, and non-month dates in gray font. Users can flip the calendar by clicking the relevant buttons or sliding left or right. The specific operation process is shown in the figure

To implement the calendar function, first create a new class DayBean to store daily information. The code is shown in Figure 3-27.

Figure 3- 27 DayBean

Then customize an adapter to display calendar information on the interface. Create a new class DayAdapter that inherits from BaseAdapter. The code is shown in Figure 3-28.

Figure 3- 28 DayAdapte

In this adapter, getView() is the main function, which manages the display mode of calendar interface information. The specific implementation is shown in Figure 3-29.

Figure 3- 29 getView()

After completing the creation of the above two classes, design the calendar interface activity_day. Since ListView displays a single column and multiple rows, this feature is useful for displaying city information, but it is not friendly for displaying calendars. Therefore, the calendar in the layout uses a GridView that can display multiple rows and columns in a mesh form. After the layout is completed, create a new class Day and connect the calendar-related designs in it. The code is shown in Figure 3-30.

Figure 3- 30 Day

updateAdapter() is the key to calendar information display. The calendar display method in the design is 6 rows and 7 columns, displaying a total of 42 dates. In other words, the calendar displays not only the information of the current month, but also some dates of the previous month and the next month. Putting the dates of each month into their respective positions in the calendar is what updateAdapter() wants to achieve.

There are some critical issues in the calendar, such as January after December. In order to ensure that the designed calendar conforms to common sense, the commonly used calendar object Calendar is used in the design to dynamically set the time and effectively solve critical issues.

In updateAdapter(), first use setCurrentData() to set the current time as the initial time, and then use set() to know the day of the week on the 1st of the current month. At this time, the index of the week is 1-7, which correspond to Sunday and Monday to Saturday respectively. For the convenience of operation, the obtained index number is reduced by one, so that 0-6 correspond to Sunday and Monday to Saturday respectively. See Figure 3-31.

Figure 3- 31 The initial part of updateAdapter()

After starting the query, the days in the last month will be displayed together with the dates of this month. As shown in Figure 3-32.

Figure 3- 32 updateAdapter() queries last month’s information

First, set() sets the date to the previous month. Since the month in Calendar returns 0-11, corresponding to January-December respectively, the return value of other information is consistent with the actual situation, so the situation is different when using the index number of the month. Different treatments. Here, after get() returns the index number of this month, subtract one to get the index number corresponding to the previous month. For example, if this month is November, then the index number it returns is 10, and the index number is reduced by one to become 9. According to the rules of the Calendar object, the index number 9 corresponds to October, which is the current month. the previous month of the month.

Then pass the month and year of the previous month to the custom getMonth() to get the number of days in the last month. Since the time has been set to last month in the previous step, get() returns the index number of last month. According to the rules, adding one to this number is the actual number of last month. Continuing with the previous example, here the current month becomes October after set(), and its index number is 9. When calling getMonth(), you need to pass the month number 10, not its index number 9, so get() needs to add one to the index number.

Finally, after determining the week position of the 1st of this month and the number of days in the previous month, use a for loop to match the last few days of the last month to the positions before the 1st of this month until it is filled. Here you need to use the DayBean class set up previously to store information, store the date information that will appear in this month's calendar in the last month one by one into the bean, and finally store the bean in the dataList.

After getting the last month's date displayed with this month, start matching this month's date with the position in the calendar. As shown in Figure 3-33.

Figure 3- 33 updateAdapter() queries this month’s information

First, set() sets the time back to the current month. When querying last month's information, the month has been set to the previous month, so the index number returned by get() plus one is the index number of the current month. Then call getMonth() to get the number of days in this month. The index number obtained by get() at this time is the index number corresponding to this month, so adding one is the number of months in this month. The next operation is similar to querying the date of the previous month, except that in this step you need to call getFormatTime() to obtain the real year, month and day information nowDate and the year and month information currently displayed by the calendar in the software selectData, and then compare the real year Whether the month and the year and month displayed in the software are the same, it means that in the information currently displayed by the software, one day is the current day in real life, and the setCurrentDay() parameter is true at this time. As a supplementary explanation, setCurrentDay() and setCurrentMonth() are both functions set in the DayBean class. By passing true and false to them, we can determine whether the date being processed is the current month and day in real life. In the final calendar, the display method for today's day of this month, other days of this month, and non-month dates is determined by these two.

Through the above operation, the relevant information of the previous month and this month in the calendar is determined. The remaining date vacancies are filled with the date of the next month. The specific operation is similar to that of querying the previous month. It should be noted that the number of months and the index in the rules are The relationship between the numbers and the change of the week index number for the convenience of operation are shown in Figure 3-34.

Figure 3- 34 updateAdapter() queries next month’s information

After the above three queries are completed, use notifyDataSetChanged() to notify Day's adapter initAdapter() to update the data, and use set() to set the date to the current month. As shown in Figure 3-35.

Figure 3- 35 End of updateAdapter()

The above is the implementation method of updateAdapter() to control the display of calendar information. The other functions defined in Day involved are shown in Figure 3-36.

Figure 3-36 The remaining functions in Day

 

Guess you like

Origin blog.csdn.net/weixin_58963766/article/details/130567736