Friday, October 30, 2015

View Pager Displays Specific Fragment Page

How To Get View Pager Displays Specific Fragment Page
1) Follow the tutorial to create a sample project.
2) Edit Main Controller
File Name:
package com.notarazi.myviewpagertablayout1;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
   protected void onCreate(Bundle savedInstanceState) {
       Toolbar toolbar = (Toolbar) findViewById(;
       // Get the ViewPager and set it's PagerAdapter so that it can display items
       ViewPager viewPager = (ViewPager) findViewById(;
       viewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(),
       // Give the TabLayout the ViewPager
       TabLayout tabLayout = (TabLayout) findViewById(;
       // tab index ={0,1,2}
       // display tab index 1
       FloatingActionButton fab = (FloatingActionButton) findViewById(;
       fab.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
               //        .setAction("Action", null).show();
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(, menu);
       return true;
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       //noinspection SimplifiableIfStatement
       if (id == {
           return true;
       return super.onOptionsItemSelected(item);
The Android Developer Page explains …


No comments:

Post a Comment