Android navigation navigation example?

I'm trying to figure out how to create three views for side navigation, but the examples provided by developer.android.com are useless, as they are for API 11 only. I was looking for a simple instance / insert of a navigation example navigation, but I had no luck.

I found tutorials for marking up between actions or different layouts, but that’s not what I want to do, I need to generate each “screen” programmatically. This is due to the fact that I'm working on something that should work like a home screen, and the number of screens can vary.

Can someone give an example / tutorial on how to programmatically create three views with side navigation? Something will help.

+4
source share
2 answers

This is the main activity:

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyPagerAdapter adapter = new MyPagerAdapter(); ViewPager myPager = (ViewPager) findViewById(R.id.home_pannels_pager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); } } 

Create another class called MyPagerAdapter in the same package:

  public class MyPagerAdapter extends PagerAdapter { // State number of pages public int getCount() { return 5; } // Set each screen content @Override public Object instantiateItem(View container, int position) { Context context = container.getContext(); LinearLayout layout = new LinearLayout(context); // Add elements TextView textItem = new TextView(context); buttonItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.phone"); // myFancyMethod(v); } }); switch (position) { case 0: textItem.setText("First Screen"); break; case 1: textItem.setText("Second Screen"); break; case 2: textItem.setText("Third Screen"); break; case 3: textItem.setText("Fourth Screen"); break; case 4: textItem.setText("Fifth Screen"); break; } layout.addView(textItem); ((ViewPager) container).addView(layout, 0); // This is the line I added return layout; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); } @Override public Parcelable saveState() { return null; } } 

Finally, create activity_main.xml inside / res / layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/home_pannels_pager"/> </LinearLayout> 
+12
source

Visit the website for the best library for navigating the navigation screen

+3
source

All Articles