How To Use Fragments In ViewPager In Android

Step1: ViewPager Tag in XML

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


Step2: Make Fragment (Fragment1.java)
package android.com.myapplication;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by admin on 29-03-2016.
 */
public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView= (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        return rootView;
    }
}

Step3: Make xml (fragment1.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Fragment 1"/>
</LinearLayout>


(Make 2 More Fragments same as above)

Step3: Make PageAdapter
(PageAdapter.java)


package android.com.myapplication;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by admin on 29-03-2016.
 */
public class PageAdapter extends FragmentPagerAdapter {

List<Fragment> fragments;

    public PageAdapter(FragmentManager fm,List<Fragment> fragments)
    {
        super(fm);
        this.fragments=fragments;
    }



    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

Step4: MainActivity.java

package android.com.myapplication;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewParent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Fragment> fragments=new ArrayList<>();

        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());




        PageAdapter pageAdapter=new PageAdapter(getSupportFragmentManager(),fragments);

        ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(pageAdapter);
    }
}
Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment