Android ViewPager的使用以及定时切换实现
2015-07-06 17:33
495 查看
ViewPager的页面布局如下:
viewpager的实现要设置一个VewPagerAdapter 还有 一个 OnViewPagerChanged()的监听事件代码如下:
创建一个newSingleThreadScheduledExecutor进行图片的切换效果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <FrameLayout android:layout_width="fill_parent" android:layout_height="200dp"> <android.support.v4.view.ViewPager android:id="@+id/myviewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="35dp" android:orientation="vertical" android:layout_gravity="bottom" android:gravity="center" android:background="#33000000"> <TextView android:id="@+id/txttitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:orientation="horizontal"> <View android:id="@+id/dotnumber_1" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/mydotfocus" /> <View android:id="@+id/dotnumber_2" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/mydotnormal" /> <View android:id="@+id/dotnumber_3" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/mydotnormal" /> <View android:id="@+id/dotnumber_4" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/mydotnormal" /> <View android:id="@+id/dotnumber_5" android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/mydotnormal" /> </LinearLayout> </LinearLayout> </FrameLayout> </RelativeLayout>包括文字和浮点的切换以及图片的切换,效果图如下:
viewpager的实现要设置一个VewPagerAdapter 还有 一个 OnViewPagerChanged()的监听事件代码如下:
package com.shao.ViewPagerLearn; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private int[] imageids; private String[] titles; private List<ImageView> imageslist; private List<View> dotslist; private TextView txttitle; private ViewPager mViewPager; private ScheduledExecutorService excutorservice; private int oldposition; private int currentposition; private Handler handle = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); mViewPager.setCurrentItem(currentposition); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageids = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e}; titles = new String[]{"巩俐不低俗,我就不能低俗","扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀"}; imageslist = new ArrayList<ImageView>(); for(int i = 0; i < imageids.length;i++) { ImageView imageview = new ImageView(getApplicationContext()); imageview.setBackgroundResource(imageids[i]); imageslist.add(imageview); } dotslist = new ArrayList<View>(); dotslist.add(findViewById(R.id.dotnumber_1)); dotslist.add(findViewById(R.id.dotnumber_2)); dotslist.add(findViewById(R.id.dotnumber_3)); dotslist.add(findViewById(R.id.dotnumber_4)); dotslist.add(findViewById(R.id.dotnumber_5)); txttitle = (TextView)findViewById(R.id.txttitle); txttitle.setText(titles[0]); mViewPager = (ViewPager)findViewById(R.id.myviewpager); mViewPager.setAdapter(new MyViewpagerAdaper()); //创建一个页面更换监听 mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub txttitle.setText(titles[arg0]); dotslist.get(oldposition).setBackgroundResource(R.drawable.mydotnormal); dotslist.get(arg0).setBackgroundResource(R.drawable.mydotfocus); oldposition = arg0; currentposition = arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } private class MyViewpagerAdaper extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return imageids.length; } //判断是不是同一张图片 @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } /*显示内存就放三张图片 滑动的时候 出去的图片执行destroy 进到内存的图片就是instaint*/ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub //super.destroyItem(container, position, object); //移除这个位置的图片 container.removeView(imageslist.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(imageslist.get(position)); return imageslist.get(position); } } @Override protected void onStart() { // TODO Auto-generated method stub excutorservice = Executors.newSingleThreadScheduledExecutor(); excutorservice.scheduleWithFixedDelay(new MyTask(), 2, 2, TimeUnit.SECONDS); super.onStart(); } private class MyTask implements Runnable { @Override public void run() { // TODO Auto-generated method stub //当前图片+1进行图片的切换 currentposition = (currentposition + 1)%imageids.length; handle.sendEmptyMessage(0); } } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
创建一个newSingleThreadScheduledExecutor进行图片的切换效果
相关文章推荐
- Android Studio 中的NDK开发
- Android 用代码设置颜色
- android ADB * daemon not running.starting it now on port 5037 *
- android 玩愤怒的小鸟等游戏的时候全屏TP失败
- Android 百分比布局库(percent-support-lib) 解析与扩展
- android 步骤控件的使用
- 【android】activity、fragment传值例子
- Android开发最佳实践
- Android设置虚线、圆角、渐变
- Android开发中的一个小功能 清空搜索框的文字
- Android设计模式—策略模式
- Android设计模式系列-单例模式
- 【基于Android的ARM汇编语言系列】之六:NEON指令集与VFP指令集
- Android设计模式系列--工厂方法模式
- 【基于Android的ARM汇编语言系列】之五:ARM指令集与Thumb指令集
- Android设计模式系列-适配器模式
- 【基于Android的ARM汇编语言系列】之四:ARM处理器的寻址方式
- 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构
- Android设计模式系列--原型模式
- Android设计模式系列--观察者模式