Android uses Viewpager to implement the Welcome Guide page

Source: Internet
Author: User

Android uses Viewpager to implement the Welcome Guide page Most apps start in the first time, there will be a boot interface, left and right swipe, to the last one, the user click to enter the main interface again. When you start the second time, you go directly to the main interface. This effect is generally implemented using Viewpager. Today, I would like to introduce you to the use of Viewpager. Implementation steps: Use Sharedperferences to record whether the app was launched for the first time, and if so, skip to the guide page if it's not the first time you start. Jump to the main activity.mainactivity: This activity as the logo page entered, using handler to achieve the page delay jump. For students who don't know handler, you can come here to learn about the asynchronous messaging mechanism for Android: http://blog.csdn.net/guolin_blog/article/details/9991569 (Thanks to Guo Shen for good articles)
PrivateHandler Mhandler =NewHandler () {@Override         Public void Handlemessage(Message msg) {Switch(Msg.what) { CaseMain:intent Intent =NewIntent (mainactivity. This, Main.class);                    StartActivity (Intent); Finish (); Break; CaseGuide:intent Intent2 =NewIntent (mainactivity. This, Guideview.class);                    StartActivity (Intent2); Finish (); Break; }        }    };

Also use sharedpreferences to read user boot information

mSharedPreferences = getSharedPreferences("conf",MODE_PRIVATE);isFirst = mSharedPreferences.getBoolean("first",true);

After making a simple judgment, this is not a liability.

Guideactivity

This interface is the interface of the boot page, we first add a viewpager under its layout file and then get this viewpager in the Java file, and add an adapter for this viewpager. This adapter needs to inherit pageradapter to rewrite it, which will be explained later.

 mViewPager = (ViewPager) findViewById(R.id.viewpager);        ViewPagerAdapter adapter = new ViewPagerAdapter(mList);        mViewPager.setAdapter(adapter);

Note that the passed-in parameter of this adapter is a generic view list. We first add our view to the list and then add a listener event for the last View button to jump to the main page;

Mlist = new Arraylist<> ();View view = Layoutinflater. from(this). Inflate(R. Layout. Layout1,null);Mlist. Add(view);View = Layoutinflater. from(this). Inflate(R. Layout. Layout2,null);Mlist. Add(view);View = Layoutinflater. from(this). Inflate(R. Layout. Layout3,null);Mlist. Add(view);Mbutton = (Button) view. Findviewbyid(R. ID. Button_enter);Mbutton. Setonclicklistener(New View. Onclicklistener() {@Override public void OnClick (view view) {Intent Intent = new Intent (guideview. this, Main. Class);StartActivity (Intent);Finish ();}        });
Viewpageradapter

This is the Viewpager adapter, which inherits from the Pageradapter and implements two main methods:
Destroyitem ();
Instantiateitem ();
As the name implies, the view is removed when the method, one is to add the view method.
Here you can see a very familiar parameter container, yes, and Baseadapter's parameters are similar, are used to cache the current view. The next thing you need to do is call his Removeview and AddView. It is important to note that the downward transition to Viewpager is required.

publicvoiddestroyItemintobject) {        ((ViewPager)container).removeView(mList.get(position));    }    @Override    publicinstantiateItemint position) {        Log.i("wing","viewAdatper here");        ((ViewPager)container).addView(mList.get(position));        return mList.get(position);    }

Also don't forget to rewrite his construction method. Pass in a generic to view list

    publicViewPagerAdapter(ArrayList list){        mList = list;    }

So the adapter will be finished, you can write a few more times you will be proficient. By the right, don't forget to give each view a layout file.

See below:

Source

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses Viewpager to implement the Welcome Guide page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.