Android realizes the clickable and slidable effect of the top navigation bar (imitating wechat and Douban)

Use viewpager, pagerslidingtabstrip and swiperefreshlayout to create a top navigation bar that can be clicked and sideswiped.

First, briefly introduce the two open source libraries used.

PagerSlidingTabStrip

GitHub address

Usage:

1. Add dependency to build.gradle in app module

2. Add the pagerslidingtabstrip control to your layout, which is usually placed on the top of viewpager

3. Bind pagerslidingtabstrip with viewpager in your oncreate () method (or oncreateview () method in fragment)

4. If you want to set pagerchange listening

As for many other methods in it, I won't talk more here. Students who need to know can go to GitHub.

SwipeRefreshLayout

Swiperefreshlayout is a drop-down refresh control officially released by Google. It only supports drop-down refresh and does not support pull-up loading. Of course, some people in GitHub have written an open-source framework for pull-up loading based on this control. Hee hee, I'll introduce it to you in the next article.

Usage:

1. Add android.support.v4.widget.swiperefreshlayout in the XML file

2. If departure event is required during drop-down, swiperefreshlayout.onrefreshlistener shall be implemented and onrefresh() method shall be overridden.

Imitation watercress for sliding effect

First, let's see mainactivity. Class

There should be nothing to say about the whole mainactivity. I have annotated all the difficult parts. Then we write fragments. Here we write two fragments. The first fragment is the home page. To implement listview and drop-down refresh, the refreshfragment.class code is as follows:

The setcolorschemeresources() method of swiperefreshlayout is to set the color of the circle refreshed by the drop-down. At most four colors can be set. This framework is a horizontal line color change before Android 5.0, which is very general and not good-looking at all, but it becomes a circle change after Android 5.0. It is used by many apps, such as Betta TV.

In addition, in the onrefresh () method, we set the refresh to complete in three seconds, that is, the state in setrefreshing () changes from true to false.

Next, we create another fragment. The code of pagerfragment.class is as follows:

Layout file for mainactivity:

The layout file of pagerfragment is as follows:

The layout file of refreshfragment is as follows:

The above is what Xiaobian introduced to you. Android realizes the clickable and sliding effect of the top navigation bar (imitating wechat and imitating Douban). 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
分享
二维码
< <上一篇
下一篇>>