Detailed explanation of setonpagechangelistener method of viewpager

background

Recently, you need to implement a custom notification function. I found the code on the Internet, and the solution is to implement it through remoteviews. However, there are many problems encountered in the implementation process, and there are no good articles on the Internet to describe these problems, so I make a summary here. I hope you can avoid detours.

realization

Remoteviews custom view

This is the most basic knowledge. Although those who have made custom notifications should be clear, I think it is still necessary to bring them. It is mainly used for AppWidget and notification. It describes a view displayed in other processes. The following is an example code. We can see that remoteviews provides some methods to change the value of its child view, such as setting the text of textview.

Here are a few points to note.

setInt

This method is used to call the method that needs an int parameter in the child view. As shown in the following code, close with ID is called_ IV's setcolorfilter method. The parameter is the return value of geticoncolor().

Set different areas and click pendingintent

The default notification can only set the overall click event through setcontentintent. However, through remoteviews, we can set different click events in different places. Of course, the event here refers to pendingintent. As shown below, click r.id.notice_ view_ type_ 0 open an activity and click r.id.close_ IV will send a broadcast. You can do something through the broadcast receiver of this broadcast, such as closing the current notification. You can also open a service.

Set custom view for notifications

Above, we get the customized remoteviews. The following code can generate the notification of the custom view. Note that the setcontent () method is used here. This is the method that will be used to customize notification on the Internet.

But it has a problem.

The notification obtained through the setcontent () method is fixed high. If the height of the view is larger than the default height, some parts will not be displayed. As shown below

By default, the notification height is 64DP. Of course, there may be some differences between ROMs. Generally, text can be displayed when it is less than two lines.

So how to wrap_ content。 Need to use some black technology. As follows:

In order to understand the above code, we need to clarify a problem that we can easily ignore, that is, notifications can be expanded and collapsed. Please look at the following two pictures. It is also the notice of Netease cloud music. Figure 1 is larger than Figure 2. In fact, figure 1 shows the unfolding state of Netease cloud music notification. You can retract it by sliding up with two fingers, that is, figure 2.

In the above code, we set bigcontentview, which is the expanded custom view, and contentview, which is the view when it is stowed.

Note that bigcontentview was introduced in SDK 16, so it needs to be judged. If it is less than sdk16, it can only be set high.

Note that the maximum height of bigcontentview is 256dp

Note that the settings of bigcontentview and contentview cannot be reversed. In the pro test, the contentview will not be displayed.

In addition, it should be noted that some ROMs may not support the expansion and collapse notification. After bigcontentview is set, only the expanded view is displayed, and only the collapsed view is displayed by default. For example, Meizu's flyme and other ROMs have not been tested. If readers know, they can share it.

Background color adaptation

The notification background colors of different ROMs are different, so you need to pay attention to them on the UI. There are two main situations.

There are two main schemes.

Fixed background color

That is, set a fixed background color, and the text and icon colors can be fixed. See the figure below.

This has a disadvantage, which we can also see in the figure, that is, some ROM notifications will have a left and right padding, such as MIUI, which is particularly obvious. If the background color is fixed, it will be very ugly.

Therefore, although this method is simple, it is not recommended.

Transparent background color

Another way is to make the background transparent. So what about the color of text and icon? Very simple, follow the text style in the notification of the system. The textview style is set as the info style in the default notification as follows. Other related styles include textappearance.statusbar.eventcontent.line2, textappearance.statusbar.eventcontent.info, etc.

It should be noted that after Android 5.0, different style names are used to represent notification styles. We need to create a layout-v21 folder and create a custom notification style to be used after 5.0. The following is the same way to set the textview style to info, but we use @ Android: style / textappearance.material.notification.info.

In addition, if there is icon in the custom view, the color of icon also needs to adapt to the background. You can select a gray, such as #999999. The color of text content in black and white of native Android is this value.

Or set different colors according to different background colors through the setint method mentioned above. The setcolorfilter method of ImageView can set the pattern color to a certain solid color. But at present, I haven't found a good way to get the background color of the default notification, if the reader finds the lookout notification.

In addition, it should be noted that the ROM of many mobile phones may not modify the above style, but adopt their own style, which is more painful. Referring to the method of Netease cloud music, under API 21, because most mobile phones use black background, they use transparent background and text color following system. When the API is greater than or equal to 21, because the native Android adopts a white notification background, and the notifications of ROM of many mobile phone manufacturers are still black background, a fixed black background is adopted for general use, and the text color also follows the system. On this basis, some specific mobile phones ROM are adapted. For example, Huawei and others, the notification background below API 21 is also white. This should be a perfect practice, but it will take more time, so for simplicity, we'd better adopt scheme 1.

Final effect

summary

The above are some problems and solutions I encountered in customizing notification. At present, there are two points to be further supplemented and improved.

Gets the default notification background color, or a scheme that adapts the icon color to the background color.

The ROM for notification expansion and retraction is not supported. At present, only flyme is known.

Sample code address

https://github.com/beautifulSoup/CNotification

The above is a detailed explanation of the setonpagechangelistener method of viewpager introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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