You need 2 class files.
FragmentPage.java
--------------------
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentPage extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, null);
return v;
}
}
SwapMainPage.java
---------------------
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class SwapMainPage extends FragmentActivity {
/** Called when the activity is first created. */
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The ViewPager that will host the section contents.
*/
ViewPager mViewPager;
String name;
int a;
int len;
ArrayList<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data.add("Ashwin");
data.add("Ashwin");
data.add("Ashwin");
data.add("Ashwin");
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A FragmentPagerAdapter that returns a fragment corresponding to one of
* the primary sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context c;
public SectionsPagerAdapter(Context c, FragmentManager fm) {
super(fm);
this.c = c;
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
fragment = new FragmentPage();
return fragment;
}
@Override
public int getCount() {
return data.size();
}
@SuppressLint("DefaultLocale")
@Override
public CharSequence getPageTitle(int position) {
String s = data.get(position)
.toUpperCase();
return s;
}
}
}
2 xml Files
fragment.xml
-------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
main.xml
---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
app:context=".MainActivity" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#fff" >
</android.support.v4.view.PagerTitleStrip>
<TextView
android:id="@+id/webV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.view.ViewPager>
</LinearLayout>
FragmentPage.java
--------------------
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentPage extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, null);
return v;
}
}
SwapMainPage.java
---------------------
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class SwapMainPage extends FragmentActivity {
/** Called when the activity is first created. */
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The ViewPager that will host the section contents.
*/
ViewPager mViewPager;
String name;
int a;
int len;
ArrayList<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data.add("Ashwin");
data.add("Ashwin");
data.add("Ashwin");
data.add("Ashwin");
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A FragmentPagerAdapter that returns a fragment corresponding to one of
* the primary sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context c;
public SectionsPagerAdapter(Context c, FragmentManager fm) {
super(fm);
this.c = c;
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
fragment = new FragmentPage();
return fragment;
}
@Override
public int getCount() {
return data.size();
}
@SuppressLint("DefaultLocale")
@Override
public CharSequence getPageTitle(int position) {
String s = data.get(position)
.toUpperCase();
return s;
}
}
}
2 xml Files
fragment.xml
-------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
main.xml
---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
app:context=".MainActivity" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#fff" >
</android.support.v4.view.PagerTitleStrip>
<TextView
android:id="@+id/webV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.view.ViewPager>
</LinearLayout>
Very informative post.I am really impressed by this incredible precious site.
ReplyDelete