Android realizes similar wechat page switching effect based on viewpager

This article is only for personal learning Android, recording growth and convenient review!

Realize viewpager and realize page switching similar to wechat. This paper realizes it through fragment adapter

First, in the layout file activity_ Add viewpager to main.xml

Viewpager is a page, and pagertabstrip and pagertitlestrip are used to set the title

Next, create three layout files to reference the viewpager adapter

view1.xml

view2.xml

view3.xml

Create three classes to inherit the fragment, and implement the oncreateview method through the fragment to convert the view.xml layout into view myfragment1.java

MyFragment2.java

MyFragment3.java

Create an adapter, create a new class (myadapter), and inherit the fragmentpageradapter

Instantiate components and adapters in activity, add listeners, add various data sources, etc

Monitor implementation interface onpagechangelistener

Viewpager, add listening method setonpagechangelistener() method has been removed by as. It is recommended to use addonpagechangelistener() method.

MainActivity.java

PS: many of the above use the classes under Android. Support. V4. * for downward compatibility

Viewpager has three adapters that adapt to different data sources.

1. The adapter inherited from pageradapter adapts the data of list < View >

2. The adapter inherited from the fragmentadapter adapts the data of list < fragment >

3. The adapter inherited from the fragmentstateadapter adapts the data of list < fragment >

The difference between the latter two is: 2 there is no creation and destruction process, 3 there is

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