ViewPager

来源:互联网 发布:中超风云球员数据 编辑:程序博客网 时间:2024/04/28 23:57

ViewPager的功能是页面滑动,可以用来制作引导页,也可以实现程序中多个视图切换的效果。
ViewPager填充可以使用PagerAdapter
下面用代码介绍其使用方法

guide.xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v4.view.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#FF000000"        ></android.support.v4.view.ViewPager></LinearLayout>

red.xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#EE0000"></LinearLayout>

black.xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#000000"></LinearLayout>

green.xml 布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#AACC44"></LinearLayout>

Guide的实现

import android.app.Activity;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import java.util.ArrayList;import java.util.List;public class Guide extends Activity {    private ViewPagerAdapter viewPagerAdapter;    private ViewPager viewPager;    private List<View> views;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.guide);        InitView();    }    private void InitView(){        LayoutInflater inflater = LayoutInflater.from(this);        viewPager = (ViewPager)findViewById(R.id.view_pager);        views = new ArrayList<View>();        views.add(inflater.inflate(R.layout.red,null));        views.add(inflater.inflate(R.layout.black,null));        views.add(inflater.inflate(R.layout.green,null));        viewPagerAdapter = new ViewPagerAdapter(this,views);        viewPager.setAdapter(viewPagerAdapter);    }}

ViewPagerAdapter的实现

import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import java.util.List;public class ViewPagerAdapter extends PagerAdapter {    private List<View> views;    private Context context;    public ViewPagerAdapter(Context context,List<View> views){        this.context = context;        this.views = views;    }    @Override    public int getCount(){        return views.size();    }    @Override    public Object instantiateItem(ViewGroup container,int position){        container.addView(views.get(position));        return views.get(position);    }    @Override    public boolean isViewFromObject(View view,Object o){        return (view == o);    }    @Override    public void destroyItem(ViewGroup container,int position,Object object){        container.removeView(views.get(position));    }}

添加ViewPager控件的时候记得要写全android.support.v4.view.ViewPager
防止出现版本上的问题

1 0
原创粉丝点击