1. What is ViewPager?
Ans:-Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with Fragment , which is a convenient way to supply and manage the lifecycle of each page.
Here we will see how to implement ViewPager in Android Application.For this Example we will create 3 Layouts and their JAVA class and add these Layouts to MainActivity.java.Following are the site map.
1.MainActiviry.java | activity_main.xml
2.ViewPager1.java | view_pager1.xml
3.ViewPager2.java | view_pager2.xml
4.ViewPager3.java | view_pager3.xml
Step 1:- main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="supriya.com.viewpagerexample.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Step 2:- MainActivity.java
<?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"
android:gravity="center_vertical"
android:background="#B4045F">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage2"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 6:- ViewPage2.java
<?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"
android:gravity="center_vertical"
android:background="#0174DF">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage3"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 8:- ViewPage3.java
Ans:-Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with Fragment , which is a convenient way to supply and manage the lifecycle of each page.
Here we will see how to implement ViewPager in Android Application.For this Example we will create 3 Layouts and their JAVA class and add these Layouts to MainActivity.java.Following are the site map.
1.MainActiviry.java | activity_main.xml
2.ViewPager1.java | view_pager1.xml
3.ViewPager2.java | view_pager2.xml
4.ViewPager3.java | view_pager3.xml
Step 1:- main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="supriya.com.viewpagerexample.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Step 2:- MainActivity.java
package supriya.com.viewpagerexample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { ViewPager vpager; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vpager=(ViewPager)findViewById(R.id.vPager); vpager.setAdapter(new setViewadapter(getSupportFragmentManager())); } public class setViewadapter extends FragmentPagerAdapter { public setViewadapter(FragmentManager fm) { super(fm); } @Override
public Fragment getItem(int position) { if(position==0) { return new ViewPage1(); } else if (position==1){ return new ViewPage2(); } else
return new ViewPage3(); } @Override
public int getCount() { return 3; } } }
Step 3:- view_page1.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"
android:gravity="center_vertical"
android:background="#3ADF00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage1"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 4:- ViewPage1.java
Step 5:- view_page2.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"
android:gravity="center_vertical"
android:background="#3ADF00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage1"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 4:- ViewPage1.java
package supriya.com.viewpagerexample; 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 Supriya on 9/11/2016. */
public class ViewPage1 extends Fragment { @Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.view_page1,null); return v; } }
<?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"
android:gravity="center_vertical"
android:background="#B4045F">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage2"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 6:- ViewPage2.java
package supriya.com.viewpagerexample; 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 Supriya on 9/11/2016. */
public class ViewPage2 extends Fragment { @Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.view_page2,null); return v; } }Step 7:- view_page3.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"
android:gravity="center_vertical"
android:background="#0174DF">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPage3"
android:textSize="25sp"
android:textAlignment="center"
android:textColor="#ffffff"/>
</LinearLayout>
Step 8:- ViewPage3.java
package supriya.com.viewpagerexample; 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 Supriya on 9/11/2016. */public class ViewPage3 extends Fragment { @Nullable @Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.view_page3,null); return v; } }
thanks
ReplyDeleteWow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeleteapple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai