标签:
1.activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.PagerTabStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="@color/background_material_dark" android:textColor="#fff" android:paddingTop="4dp" android:paddingBottom="4dp"/> </android.support.v4.view.ViewPager>
2.fragment_page.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"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:contentDescription="@string/app_name"/> </LinearLayout>
3.MainActivity
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.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { MyFragmentStatePagerAdapter mMyFragmentStatePagerAdapter; ViewPager mViewPager; String[] titles={"1","2","3","4","5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar ab=this.getSupportActionBar(); if(ab!=null){ ab.hide(); } mMyFragmentStatePagerAdapter=new MyFragmentStatePagerAdapter(getSupportFragmentManager()); mViewPager= (ViewPager) findViewById(R.id.viewpager); mViewPager.setAdapter(mMyFragmentStatePagerAdapter); } public class MyFragmentStatePagerAdapter extends FragmentPagerAdapter{ public MyFragmentStatePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment=new MyFragment(); Bundle args=new Bundle(); args.putInt(MyFragment.WHICH,position); fragment.setArguments(args); return fragment; } @Override public int getCount() { return titles.length; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } } public static class MyFragment extends Fragment{ public static final String WHICH="which"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_page,container,false); Bundle args=getArguments(); ImageView iv= (ImageView) v.findViewById(R.id.img); int which=args.getInt(WHICH); int resId=0; switch (which){ case 0: resId=R.drawable.mei; break; case 1: resId=R.drawable.s; break; case 2: resId=R.drawable.s1; break; case 3: resId=R.drawable.s2; break; case 4: resId=R.drawable.s3; break; } iv.setImageResource(resId); return v; } } }
使用ViewPager的一般步骤
1.创建一个包含ViewPager的主界面布局。需要标题栏,则可将PagerTabStrip或PagerTitleStrip嵌套在ViewPager中
2.实现一个FragmentPagerAdapter或FragmentPagerStateAdapter的子类,并覆盖其中的getItem和getCount方法
3.创建实现的PagerAdapter的对象,并调用ViewPager的setAdapter将它作为ViewPager的Adapter。
标签:
原文地址:http://blog.csdn.net/qq_29481375/article/details/51361732