Android--ViewPager

ViewPager实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。
ViewPagerActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

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

/** * Created by Administrator on 2015/10/20. */
public class ViewPagerActivity extends Activity {
    private ViewPager viewPager;
    private PagerTitleStrip pageTab;

    private List<View> views;
    private List<String> titles;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        pageTab = (PagerTitleStrip) findViewById(R.id.pagertitle);

        views = new ArrayList<View>();

        LayoutInflater inflater = getLayoutInflater();

        View view1 = inflater.inflate(R.layout.view1, null);
        View view2 = inflater.inflate(R.layout.view2,null);
        View view3 = inflater.inflate(R.layout.view3,null);

        views.add(view1);
        views.add(view2);
        views.add(view3);

        titles = new ArrayList<String>();
        titles.add("tab1");
        titles.add("tab2");
        titles.add("tab3");

        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public void destroyItem(View container, int position, Object object) {
                // TODO Auto-generated method stub
                ((ViewPager)container).removeView(views.get(position));
            }

            @Override
            public Object instantiateItem(View container, int position) {
                // TODO Auto-generated method stub
                ((ViewPager)container).addView(views.get(position));
                return views.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position){
                return titles.get(position);
            }

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

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        };

        viewPager.setAdapter(pagerAdapter);
    }
}

activity_viewpager.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.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:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"></android.support.v4.view.PagerTabStrip>

    </android.support.v4.view.ViewPager>

</LinearLayout>

view1.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:text="页卡1" android:layout_gravity="center_horizontal|center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

view2.xml与view3.xml与view1.xml类似,不在赘述。
效果图如下所示:
Android--ViewPager_第1张图片

你可能感兴趣的:(viewpager)