Android ViewPager(多页面左右滑动,可做app使用导航)
来源:互联网 发布:linux虚拟机nat上网 编辑:程序博客网 时间:2024/05/22 01:39
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.
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。
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;
}
}效果:
- Android ViewPager(多页面左右滑动,可做app使用导航)
- 在Android中使用ViewPager实现左右滑动页面
- Android ViewPager多页面滑动切换以及底部滑动导航
- android ViewPager页面左右滑动切换
- android ViewPager页面左右滑动切换
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- Android代码库-ViewPager滑动导航页面
- android 使用ViewPager实现左右滑动
- Android使用ViewPager实现左右滑动效果
- android 使用ViewPager实现左右滑动
- viewpager Android实现导航菜单左右滑动效果
- 使用ViewPager和Tab制作实现左右滑动导航
- Android ViewPager 无限循环左右滑动(可自动) 实现
- Android 多页面左右滑动
- android开发步步为营之92:ViewPager+TabLayout+Fragment构造带标签的可左右滑动页面
- Android-ViewPager滑动导航
- android viewpager+Imagerview的可滑动主界面导航栏
- 使用viewPager时页面中横向滑动不响应viewPager左右滑动
- 【JavaSE】day13_多线程
- 堆排序代码
- 完美洗牌算法
- Java集合转换【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- Odoo(OpenERP)学习资源
- Android ViewPager(多页面左右滑动,可做app使用导航)
- 学习初期写的JAVA代码 20150817---20150901
- 在secureCRT下常用的一些shell 命令
- UVa 725
- LeetCode(49)Group Anagrams
- iOS:学习runtime的理解和心得
- 简易火车票售票系统
- Titanic: Machine Learning from Disaster
- qq聊天界面七:表情的发送接收(用富文本现实)