Android imitation boss direct employment text date mixed wheel selector example

1. Demand analysis

There is an IOS style wheel selector Android pickerview on GitHub, which is divided into time selector code timepickerview and option pickerview. You can select not only the time and date, but also our customized data, such as gender, age, etc. I always use it. Until recently, there was a demand. Its options include both text and time. The general effect is as follows: the time selection function in adding project experience to boss direct employment:

As can be seen from the figure, in addition to the conventional selection of year and month, the options also contain text. Among them, the latest time is "so far", while the earliest alternative time is "before 1900". So it seems that neither timepickerview nor optionspickerview can achieve this function. We are all frustrated that we have to customize the controls or modify the Android pickerview library. But on second thought, why should we separate "time selection" from "option selection"? Time choice is actually a kind of option choice. For example, I want to select December 2017, that is, select 2017 from the year and 12 from the month. Just set the primary and secondary options.

2. Option structure analysis

With this in mind, let's analyze the data structure of options. The year can be divided into three cases:

I have added some restrictions on the basis of boss direct employment: the corresponding selectable month range in the current year can only be from month to the current month. For example, it is February 2018. After the selected year is 2018, only 1 and 2 can be selected. In this way, there are four situations in the month:

It can be summarized in the following table:

3. Writing code

Before starting to write code, I suggest you go to GitHub to see the usage of Android pickerview. It uses constructor mode, which is very simple to use.

Now, start writing our code.

3.1 interface layout

The layout is a button. Click to pop up the wheel selector. Click OK to display the data on the textview.

3.2 activity code

With the help of the powerful pickerview, our implementation is very simple. Please see the following code:

The code is very few, and the comments are very clear. I believe it is easy for everyone to understand. We focus on the setpicker method of optionspickerview, which can pass in three parameters. Each parameter is a collection, but the type of each parameter is different. The first parameter is list, the second parameter is list < list >, and the third parameter is list < list < list > >. You can see here that the month data corresponding to each year is a set (of course, the set size is different). For example, the month corresponding to year 2017 is a set with 12 elements. After clarifying this point, you can understand the data settings in the initdata method.

Finally, when displaying data in textview, we naturally need to classify it. For "up to now" and "before 1990", we have displayed text so far, and others are spliced to look like time.

Let's take a look at the final rendering:

4. Summary

Using some good third-party libraries in the project can greatly save our development time, but we should also be flexible in the use process. For example, we need to use the wheel selector many times in a page (such as selecting the start time and end time), so it's too troublesome to set the wheel style and write a click event every time. At this time, we can extract the setting code of wheel style:

Then when the scroll wheel is displayed, just write this:

Click events can also be encapsulated. Let our activity inherit optionspickerview.onoptionsselectlistener, and then implement click events:

So how does optionspickerview get the ID of the clicked view? When we call the show method, we can just pass in the object to click view. The above is my personal experience. I hope it will be helpful to you.

Finally, give me the source code: source code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>