Android ViewPager(多页面左右滑动,可做app使用导航)

来源:互联网 发布:linux虚拟机nat上网 编辑:程序博客网 时间:2024/05/22 01:39
很多app中都有这个应用,我们来看几张图片

这种导航我们都不陌生,如何做到就需要使用ViewPager
ViewPager的基本使用很简单,首先需要引入V4包, ViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view,GooGLE API中这样描述:
Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.
ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter andFragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.
其实就说明了3点:

  1)ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类。

  2)ViewPager类需要一个PagerAdapter适配器类给它提供数据。

  3)ViewPager经常和Fragment一起使用,并且提供了专门的FragmentPagerAdapter和FragmentStatePagerAdapter类供Fragment中的ViewPager使用。

ViewPager继承PagerAdapter必须实现4个复写方法

public Object instantiateItem(ViewGroup container, int position)

public void destroyItem(ViewGroup container, int position,Object object) 

public int getCount()

public boolean isViewFromObject(View arg0, Object arg1) 

初始化引入布局,ViewPager滑动页面,每个页面需要写一个布局,然后在代码中添加到list中用adapter对象传给adapter,实现4个方法,我们要解释一下运行机制(对应api给出翻译):

PagerAdapter比AdapterView的使用更加普通.ViewPager使用回调函数来表示一个更新的步骤,而不是使用一个视图回收机制。在需要的时候pageradapter也可以实现视图的回收或者使用一种更为巧妙的方法来管理视图,比如采用可以管理自身视图的fragment。

viewpager 不直接处理每一个视图而是将各个视图与一个键联系起来。这个键用来跟踪且唯一代表一个页面,不仅如此,该键还独立于这个页面所在adapter的位置。当 pageradapter将要改变的时候他会调用startUpdate函数,接下来会调用一次或多次的instantiateItem或者 destroyItem。最后在更新的后期会调用finishUpdate。当finishUpdate返回时 instantiateItem返回的对象应该添加到父ViewGroup destroyItem返回的对象应该被ViewGroup删除。methodisViewFromObject(View, Object)代表了当前的页面是否与给定的键相关联。
 
对于非常简单的pageradapter或许你可以选择用page本身作为键,在创建并且添加到viewgroup后instantiateItem方法里返回该page本身即可
destroyItem将会将该page从viewgroup里面移除。isViewFromObject方法里面直接可以返回view == object。
 
pageradapter 支持数据集合的改变,数据集合的改变必须要在主线程里面执行,然后还要调用notifyDataSetChanged方法。和baseadapter非常 相似。数据集合的改变包括页面的添加删除和修改位置。viewpager要维持当前页面是活动的,所以你必须提供getItemPosition方法。
原文:

PagerAdapter is more general than the adapters used for AdapterViews. Instead of providing a View recycling mechanism directly ViewPager uses callbacks to indicate the steps taken during an update. A PagerAdapter may implement a form of View recycling if desired or use a more sophisticated method of managing page Views such as Fragment transactions where each page is represented by its own Fragment.

ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/ordestroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup). By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to destroyItem should be removed. The method isViewFromObject(View, Object) identifies whether a page View is associated with a given key object.

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged() similar to AdapterView adapters derived fromBaseAdapter. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the methodgetItemPosition(Object).

4个方法中getcount和destoryItem,顾名思义是有效view个数和销毁给定位置的view

另外两个比较难:

instantiateItem (ViewGroup container, int position)

这个函数的实现的功能是创建指定位置的页面视图。适配器有责任增加即将创建的View视图到这里给定的container中,这是为了确保在finishUpdate(viewGroup)返回时this is be done!

返回值:返回一个代表新增视图页面的Object(Key),这里没必要非要返回视图本身,也可以这个页面的其它容器。应该是可以代表当前页面的任意值,只要你可以与你增加的View对应即可,比如position(我返回了当前view)也可以做为Key

isViewFromObject (View view, Object object)

该函数用来判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表的同一个视图(即它俩是否是对应的,对应的表示同一个View)

返回值:如果对应的是同一个View,返回True,否则返回False。直接填的view==object



基本应用:
public class MainActivity extends Activity {
private LayoutInflater mInflater;
private ViewPager mViewPager;
private List mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInflater=getLayoutInflater();
mViewPager= (ViewPager) findViewById(R.id.viewpager);
mData=new ArrayList<>();
mData.add(mInflater.inflate(R.layout.item1,null));
mData.add(mInflater.inflate(R.layout.item2, null));
mData.add(mInflater.inflate(R.layout.item3, null));
ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(mData);
mViewPager.setAdapter(viewPagerAdapter);

}
}
public class ViewPagerAdapter extends PagerAdapter {
private List views;

public ViewPagerAdapter(List mData) {
this.views = mData;
}

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

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) views.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView((View) views.get(position));
return views.get(position);
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}效果:
0 0
原创粉丝点击