Wheelview implements up and down sliding selectors

This example shares the specific code of wheelview to realize the up and down sliding selector for your reference. The specific contents are as follows

1. Get wheel

Wheel is an open source control on GitHub. We can download it directly on GitHub at https://github.com/maarek/android-wheel , after downloading, we can directly use the wheel file as a library, or copy the Java classes and XML files in the wheel to our project.

2. Usage

First, let's look at the main layout file:

Well, in the main layout file, we use three wheelviews to represent provinces, cities and counties. In mainactivity, we first need to get these three controls:

After we get it, we need to use the arraywheel adapter data adapter for data adaptation. Here we need two parameters, one is the context, and the other is an array. This array is what we want to show, that is, we need to save provinces, cities, districts and counties as an array. However, considering that a province corresponds to multiple cities and a city corresponds to multiple districts and counties, In order to associate provinces, cities and counties, we also need a map set. Therefore, the data structure we design is as follows:

The first array stores the data of all provinces, the second map stores the data of cities corresponding to all provinces, and the third map stores the data of districts and counties corresponding to all cities. Now we want to assign values to these three data sets. Let's take a look at our JSON data format: [{"name": "Beijing", "city": [{"name": "Beijing", "area": ["Dongcheng District", "Xicheng District", "Chongwen District", "Xuanwu District"...]}]}]}....]

Our JSON data is in this format. The JSON data is stored in the assets folder. Let's see how to parse the JSON data and assign it to the above three data sets:

There are no technical difficulties in JSON parsing. The logic here is a little complex. Three nested for loops are used. It's not difficult for you to think about it slowly. Well, when there is data in the dataset, we can set adapters for the three wheels:

After setting the adapter, we also set some default values, which are very simple. Just look at the comments directly. We set two listening events here. Let's see:

Almost every line of code has comments, so I don't want to play dirty games

Well, the functions we want are basically realized here, but we can see that the default style of the system is slightly ugly. Then we can get the style we want by modifying the source code. First, look at the black edge from top to bottom:

In the wheelview.java file, this line of code defines the color change of the upper and lower black edges. The three parameters are the start color, transition color and end color respectively. Then we can remove the upper and lower black edges by modifying the source code here, and the transparent East black black rag in the middle. We want to change it. We found the file wheel through the source code_ val.xml:

The style of the transparent bar in the middle is defined here. We can modify it according to our own needs. Well, the source code here is not much and it is not difficult. You can think about it yourself. That's all we have to say about wheel.

This article demo download https://github.com/lenve/wheelTest

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
分享
二维码
< <上一篇
下一篇>>