viewpager实现界面滑动

Viewpager用来实现界面的左右滑动,它是android扩展包v4包中的类,先说一下viewpager的性质:
viewpager是一个容器类,继承了viewpager类,并且viewpager需一个要pagerAdapter类。
在使用viewpager时,:注意导入  android.support.v4 。

下面的例子最终的实现如图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="100.0dip"
        android:background="#FFFFFF" >
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡1"
            android:textColor="#000000"
            android:textSize="22.0dip" />
        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡2"
            android:textColor="#000000"
            android:textSize="22.0dip" />
        <TextView
            android:id="@+id/text3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡3"
            android:textColor="#000000"
            android:textSize="22.0dip" />
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />
</LinearLayout>





具体的使用方式如下:

在xml文件中添加控件 viewpager,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="100.0dip"
        android:background="#FFFFFF" >
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡1"
            android:textColor="#000000"
            android:textSize="22.0dip" />
        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡2"
            android:textColor="#000000"
            android:textSize="22.0dip" />
        <TextView
            android:id="@+id/text3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="页卡3"
            android:textColor="#000000"
            android:textSize="22.0dip" />
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />
</LinearLayout>
2.MainActivity.java里实现如下:
package com.example.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
    public ViewPager viewpager;
    private TextView tab1,tab2,tab3;
    private List<View> list = new ArrayList<View>();
    private FragmentPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiview();
        intiViewPager();    
    }
    private void intiViewPager() {
        // TODO Auto-generated method stub
        LayoutInflater Inflater = getLayoutInflater();
        list.add(Inflater.inflate(R.layout.tab1, null));
        list.add(Inflater.inflate(R.layout.tab2, null));
        list.add(Inflater.inflate(R.layout.tab3, null));
        viewpager.setAdapter(new myPagerAdapter(list));
        viewpager.setCurrentItem(0);    
    }
    private void intiview() {
        // TODO Auto-generated method stub
        viewpager = (ViewPager)findViewById(R.id.vPager);
        tab1 = (TextView)findViewById(R.id.text1);
        tab2 = (TextView)findViewById(R.id.text2);
        tab3 = (TextView)findViewById(R.id.text3);
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                viewpager.setCurrentItem(0);    
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                viewpager.setCurrentItem(1);    
            }
        });
        tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                viewpager.setCurrentItem(2);
            }
        });
    }
}



3.对于上述代码中调用的myPagerAdapter,这是viewpager添加适配器,具体代码如下:
package com.example.viewpager;
import java.util.List;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
public class myPagerAdapter extends PagerAdapter {
        public List<View> mListViews;
        public myPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }      
         public void destroyItem(View view, int arg1, Object arg2) {
            ((ViewPager) view).removeView(mListViews.get(arg1));
        }
        @Override
         public void finishUpdate(View arg0) {
        }
        @Override
        public int getCount() {
             return mListViews.size();
        }
        @Override
        public Object instantiateItem(View view, int arg1) {
            ((ViewPager) view).addView(mListViews.get(arg1),0);
             return mListViews.get(arg1);
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
             return arg0 == (arg1);
        }
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }
        @Override
         public Parcelable saveState() {
             return null;
         }
         @Override
        public void startUpdate(View arg0) {
         }
     }


上面代码继承pageradapter,至少必须重写下面的四个方法
1 instantiateItem(ViewGroup, int)
当要显示的图片可以进行缓存的时候,会调用这个方法进行显示图片的初始化,将要显示的ImageView加入到ViewGroup中。
2. destroyItem(ViewGroup, int, Object)
当滑动超过范围,则调用该函数将图片销毁,即从viewGroup中移除当前view。
3. getCount()
获取滑动的控件的数量
4. isViewFromObject(View arg0, Object arg1)
判断是否是同一张照片,所以这个函数内只需写 return arg0 == arg1。









你可能感兴趣的:(android,viewpager)