Navigation is one of the most important aspects of mobile apps and plays a vital role in whether the user experience is good or bad. Good navigation makes an app easier to use and lets users get started quickly. Conversely, poor app navigation can be annoying and user-forsaken. To create a smooth user navigation experience, we have to rely on one of the most common features of smartphones: touch.
Touch changing the application view is now one of the most popular navigation designs. In this blog, we will go through the necessary steps to achieve horizontal sliding navigation within the app.
This article uses the code first method without hesitation. Let's take a look at the main code snippets that need to be done for sliding navigation.
<span style="font-family:arial;font-size:14px;"> Public classMainactivity extends Activity {sectionspageradapter msectionspageradapter; Viewpager Mviewpager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Msectionspageradapter=NewSectionspageradapter (Getfragmentmanager ()); Mviewpager=(Viewpager) Findviewbyid (R.id.pager); Mviewpager.setadapter (Msectionspageradapter); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Publicboolean onoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } returnsuper.onoptionsitemselected (item); } Public classSectionspageradapter extends Fragmentpageradapter { PublicSectionspageradapter (fragmentmanager FM) {super (FM); } @Override PublicFragment GetItem (intposition) { Switch(position) { Case 0: return Newmoviesfragment (); Case 1: return Newtvshowsfragment (); Case 2: return Newsongsfragment (); } return NULL; } @Override Public intGetCount () {//Show 3 total pages. return 3; } }}</span>
Our main activity contains the Viewpager component, which encapsulates several different menu screens with a different fragment on each screen. The first thing we want to do is declare a fragmentpageradapter and use it to switch between the unused menu fragment. As you can see in the code above, we declare a sectionspageradapter, which inherits from the Fragmentpageradaper class. It is then instantiated in the OnCreate method.
For the Sectionspageradapter class, you need to override two methods. The first method is the GetCount () method, which returns the number of fragment that are navigated. The second and most GetItem () method, which is used to instantiate and return the associated fragment, is displayed based on its position in the menu.
These different menu fragment must inherit the fragment class, and then populate their own layout, let's look at one of these classes:
<span style= font-family:arial; font-size:14px; >public class Moviesfragment extends Fragment {@Override pub Lic View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View Rootview = inflater.inflate (r.layout.movies_fragment, container , false ); return Rootview; }} </span>
Putting these together creates a very intuitive sliding navigation for our Android app . Our hard-working native ads can make the application profitable without compromising the user experience. Quickly integrate native, In-stream and customizable ad units, where you can check out the SDK for native ads.
Here you can get the full source code for this tutorial:swipenavigationexample.
Wirelessly how to implement sliding navigation