Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Recently saw the next month NetEase News released the 3.5 version, found two more obvious changes:
1. Change the boot page to swipe up or down. 2. Added to the aggregation of reading, the animation is also very creative. So I couldn't help imitating it.
This time first look at this slide up and down the Guide page:
How exactly does this effect work?
First directly on GitHub, directly to see if there are related to open source projects, sure enough, I found:
Https://github.com/JakeWharton/Android-DirectionalViewPager
Jakewharton, is it familiar to see this name? Yes, Actionbarsherlock,viewpagerinicator and other famous open source projects are in the hands of this person.
This project is actually based on the Viewpager, has made an extension, added to the upper and lower direction of the sliding support
As the project name, using the project, you can flexibly change the direction of the Viewpager, and only with a simple property settings can be done, I only give the example of the project to do some simple changes, to achieve the NetEase News this guide page effect.
Don't say much nonsense, go straight to the point:
First look at the following items:
The most important part of the project:
Directionalviewpager: directly inherit from Viewpager, mainly in Onintercepttouchevent () and Ontouchevent () Added support for vertical sliding. and provides the SetOrientation () method,
We can go with this method, directly set the sliding effect of viewpager, vertical or horizontal
Verticalviewpagercompat: Provides a way to set Datasetobserver: Setdatasetobserver ()
Next look at the specific use of the section:
[Java]View Plaincopy print?
- Mainactivity:
Mainactivity:
[Java]View Plaincopy print?
- Package com.way.verticalguider;
- Import Android.os.Bundle;
- Import android.support.v4.app.FragmentActivity;
- Import Com.way.directionalviewpager.DirectionalViewPager;
- Import COM.WAY.NEWVERSION.R;
- Public class Mainactivity extends Fragmentactivity {
- private Directionalviewpager Mdirectionalviewpager;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- //Set up the pager
- Mdirectionalviewpager = (Directionalviewpager) Findviewbyid (R.id.pager);
- Mdirectionalviewpager.setadapter (new Testfragmentadapter (
- Getsupportfragmentmanager ()));
- <span style="Color:rgb (255, 0, 0); >mdirectionalviewpager.setorientation (directionalviewpager.vertical); //Set Viewpager slide direction </span>
- }
- }
In practical application, we directly use Directionalviewpager instead of Viewpager, and then set its sliding direction can be either horizontal or vertical, a setoritation directly done.
See, the effect of sliding up and down is as simple as this. We have to pay tribute to Jakewharton's contribution to the Open source project, which has benefited our developers quite a lot.
In the other parts of the example, I only replaced the resource picture:
Testfragment:
[Java]View Plaincopy print?
- Package com.way.verticalguider;
- Import COM.WAY.NEWVERSION.R;
- Import Android.os.Bundle;
- Import android.support.v4.app.Fragment;
- Import Android.view.LayoutInflater;
- Import Android.view.View;
- Import Android.view.ViewGroup;
- Import Android.widget.Button;
- Import Android.widget.ImageView;
- Public class Testfragment extends Fragment {
- private static final String key_content = "Testfragment:content";
- private static final String key_islastpic = "Testfragment:islastpic";
- private int mcontent;
- private Boolean mislastpic;
- public static testfragment newinstance (int content, boolean islastpic) {
- Testfragment fragment = new Testfragment ();
- fragment.mcontent = content;
- Fragment.mislastpic = Islastpic;
- return fragment;
- }
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- if ((savedinstancestate! = null)
- && Savedinstancestate.containskey (key_content)) {
- Mcontent = Savedinstancestate.getint (key_content);
- Mislastpic = Savedinstancestate.getboolean (key_islastpic);
- }
- View root = Inflater
- . Inflate (R.layout.fragment_layout, container, false);
- ImageView IV = (ImageView) Root.findviewbyid (R.ID.IV);
- Iv.setimageresource (mcontent);
- Button btn = (button) Root.findviewbyid (R.ID.BTN);
- if (mislastpic)
- Btn.setvisibility (view.visible);
- Else
- Btn.setvisibility (View.gone);
- return root;
- }
- @Override
- public void Onsaveinstancestate (Bundle outstate) {
- super.onsaveinstancestate (outstate);
- Outstate.putint (Key_content, mcontent);
- Outstate.putboolean (Key_islastpic, mislastpic);
- }
- }
Testfragmentadapter:[Java]View Plaincopy print?
- Package com.way.verticalguider;
- Import COM.WAY.NEWVERSION.R;
- Import android.support.v4.app.Fragment;
- Import Android.support.v4.app.FragmentManager;
- Import Android.support.v4.app.FragmentPagerAdapter;
- Class Testfragmentadapter extends Fragmentpageradapter {
- protected static final int[] CONTENT = new int[] {
- R.DRAWABLE.BIZ_AD_NEW_VERSION1_IMG0,
- R.DRAWABLE.BIZ_AD_NEW_VERSION1_IMG1,
- R.drawable.biz_ad_new_version1_img2,
- R.DRAWABLE.BIZ_AD_NEW_VERSION1_IMG3};
- Public Testfragmentadapter (fragmentmanager FM) {
- super (FM);
- }
- @Override
- Public Fragment GetItem (int position) {
- Boolean islastpic = false;
- if (position = = Content.length- 1)
- Islastpic = true;
- return Testfragment.newinstance (Content[position], islastpic);
- }
- @Override
- public int GetCount () {
- return content.length;
- }
- }
Here is the download link for my revised example:
http://download.csdn.net/detail/t12x3456/5789713
In fact, no matter like this Slide guide page effect, there are many ways to achieve, here only to provide not to say is the optimization, but must be the most convenient one way.
When I have time, I will study the animation effect of the aggregation reading, in this first record, if you have a good opinion, welcome to discuss.
If reproduced, please declare the source: http://blog.csdn.net/t12x3456
Android Imitation NetEase news v3.5: Slide the Guide page up and down