ViewPager
2015-12-24 20:35
585 查看
ViewPager的功能是页面滑动,可以用来制作引导页,也可以实现程序中多个视图切换的效果。
ViewPager填充可以使用PagerAdapter
下面用代码介绍其使用方法
guide.xml布局
red.xml布局
black.xml布局
green.xml 布局
Guide的实现
ViewPagerAdapter的实现
添加ViewPager控件的时候记得要写全android.support.v4.view.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
防止出现版本上的问题
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories