<pre name="code" class="html"><strong><em>主XML</em></strong>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v4.view.PagerTabStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
</pre><pre name="code" class="html"><strong><em>Color1</em></strong>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/picture2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
主方法
package ustc.myfirstapk;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/**初始化页面*/
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**构造适配器*/
List<Fragment> fragments=new ArrayList<Fragment>();
List<String> titleList=new ArrayList<String>();
/**加入队列*/
fragments.add(new ViewFragment(R.layout.color1));
fragments.add(new ViewFragment(R.layout.color2));
fragments.add(new ViewFragment(R.layout.color3));
titleList.add("@我");
titleList.add("分享");
titleList.add("收藏");
/**为ViewPager建立适配器*/
ViewPager vp=(ViewPager)findViewById(R.id.viewPager);
vp.setAdapter(new FragmentAdapter(getSupportFragmentManager(),fragments,titleList));
}
/**适配器*/
class FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments; //页面集合
private List<String> titleList; //标题集合
public FragmentAdapter(FragmentManager fm, List<Fragment> fragments){
super(fm);
this.fragments=fragments;
}
public FragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> title) {
super(fm);
this.fragments = fragments;
this.titleList = title;
}
@Override
public CharSequence getPageTitle(int position) {
return (titleList.size() > position) ? titleList.get(position) : "";
}
/**得到页面*/
@Override
public Fragment getItem(int i) {
return (fragments.get(i) == null || fragments.size() == 0) ? null : fragments.get(i);
}
/**获得页面总个数*/
@Override
public int getCount() {
return fragments == null ? 0 : fragments.size();
}
}
}
<strong><em>Fragment 定义碎片</em></strong>
<pre name="code" class="java">package ustc.myfirstapk;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by yale on 14-11-27.
* Email : lovel1491@gmail.com
*/
@SuppressLint("ValidFragment")
public class ViewFragment extends Fragment {
private int resources;
@SuppressLint("ValidFragment")
public ViewFragment(int arg0){
resources=arg0;
}
/**构图*/
@Override
public View onCreateView(LayoutInflater layoutInflater,ViewGroup container,Bundle savedInstanceState){
View view=layoutInflater.inflate(resources,container,false);
return view;
}
}
</pre><pre name="code" class="java">