ViewPager的简单实现
2014-02-26 10:52
253 查看
MainActivity:
private ViewPager pager_splash_ad;
private pageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager_splash_ad = (ViewPager) findViewById(R.id.pager_splash_ad);
List<View> views = new ArrayList<View>();
View view = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad = (ImageView) view.findViewById(R.id.iv_ad);
iv_ad.setImageResource(R.drawable.tt);
views.add(view);
View view1 = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad1 = (ImageView) view1.findViewById(R.id.iv_ad);
iv_ad1.setImageResource(R.drawable.ww);
views.add(view1);
View view2 = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad2 = (ImageView) view2.findViewById(R.id.iv_ad);
iv_ad2.setImageResource(R.drawable.jj);
views.add(view2);
adapter = new pageAdapter(views);
pager_splash_ad.setAdapter(adapter);
}
MainActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.pagerdemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager_splash_ad"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
pagecell layout
<?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:orientation="vertical" >
<ImageView
android:id="@+id/iv_ad"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
PagerAdapter class:
public class pageAdapter extends PagerAdapter {
private List<View> mListViews;
public pageAdapter(List<View> mListViews){
this.mListViews = mListViews;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListViews == null ? 0 : mListViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;//官方提示这样写
}
public void destroyItem(ViewGroup container,int position,Object object){
container.removeView(mListViews.get(position));
}
public Object instantiateItem(ViewGroup container,int position){
container.addView(mListViews.get(position), 0);
return mListViews.get(position);
}
}
private ViewPager pager_splash_ad;
private pageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager_splash_ad = (ViewPager) findViewById(R.id.pager_splash_ad);
List<View> views = new ArrayList<View>();
View view = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad = (ImageView) view.findViewById(R.id.iv_ad);
iv_ad.setImageResource(R.drawable.tt);
views.add(view);
View view1 = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad1 = (ImageView) view1.findViewById(R.id.iv_ad);
iv_ad1.setImageResource(R.drawable.ww);
views.add(view1);
View view2 = LayoutInflater.from(this).inflate(
R.layout.pagecell, null);
ImageView iv_ad2 = (ImageView) view2.findViewById(R.id.iv_ad);
iv_ad2.setImageResource(R.drawable.jj);
views.add(view2);
adapter = new pageAdapter(views);
pager_splash_ad.setAdapter(adapter);
}
MainActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.pagerdemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager_splash_ad"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
pagecell layout
<?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:orientation="vertical" >
<ImageView
android:id="@+id/iv_ad"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
PagerAdapter class:
public class pageAdapter extends PagerAdapter {
private List<View> mListViews;
public pageAdapter(List<View> mListViews){
this.mListViews = mListViews;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListViews == null ? 0 : mListViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;//官方提示这样写
}
public void destroyItem(ViewGroup container,int position,Object object){
container.removeView(mListViews.get(position));
}
public Object instantiateItem(ViewGroup container,int position){
container.addView(mListViews.get(position), 0);
return mListViews.get(position);
}
}
相关文章推荐
- ViewPager实现简单的画廊效果
- 简单利用viewpagerindicator的两个类,而非整个工程实现程序引导页
- 用最简单的方法实现ViewPager无限循环滑动(跑马灯)效果
- Android ViewPager简单实现 - 倒计时、引导页左右滑动
- 用最简单最实用的方式实现ViewPager无限循环两种方式
- Android TabLayout+ViewPager+Fragment简单实现
- ViewPager 嵌套 ListView实现简单动态滑动
- 用Scroller实现简单viewpager滑动
- Banner+Viewpager+GridView实现首页简单布局
- 关于viewpager的简单实现引导页
- 运用ViewPager实现图片简单滑屏效果【转自http://blog.csdn.net/harvic880925/article/details/38453725 】
- viewpager+Fragment实现简单滑动切换效果
- android,侧滑栏SlidingLayout、ViewPager实现画廊、简单用viewpager实现画廊、圆形图片、简单的跑马灯动画效果、可拖拽的弹簧式水泡动画集合
- ViewPager导航图片——简单实现方法
- 简单的ViewPager+Fragment+小圆点的实现
- 实现变色TextView及ViewPager指示器(原来可以这么简单)
- viewpager实现启动页面的无限滑动、可点击导航点(导航点定位)(简单易懂)
- android简单实现无限滚动,自动滚动的ViewPager
- Material Design学习之 TabLayout(ViewPager实现页卡最简单方法)
- android之ViewPager简单实现局部页面滑动效果