Android ViewPager的使用以及定时切换实现

来源:互联网 发布:阿里巴巴数据工具 编辑:程序博客网 时间:2024/05/16 17:41

  ViewPager的页面布局如下:

<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(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);mViewPager.setCurrentItem(currentposition);}};@Overrideprotected 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() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stubtxttitle.setText(titles[arg0]);dotslist.get(oldposition).setBackgroundResource(R.drawable.mydotnormal);dotslist.get(arg0).setBackgroundResource(R.drawable.mydotfocus);oldposition = arg0;currentposition = arg0;}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}});                   }private class MyViewpagerAdaper  extends PagerAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imageids.length;}//判断是不是同一张图片@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == arg1;}/*显示内存就放三张图片 滑动的时候 出去的图片执行destroy 进到内存的图片就是instaint*/@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// TODO Auto-generated method stub//super.destroyItem(container, position, object);//移除这个位置的图片container.removeView(imageslist.get(position));}@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stubcontainer.addView(imageslist.get(position));return imageslist.get(position);}}@Overrideprotected void onStart() {// TODO Auto-generated method stubexcutorservice = Executors.newSingleThreadScheduledExecutor();excutorservice.scheduleWithFixedDelay(new MyTask(), 2, 2, TimeUnit.SECONDS);super.onStart();}private class MyTask implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stub//当前图片+1进行图片的切换 currentposition = (currentposition + 1)%imageids.length;handle.sendEmptyMessage(0);}}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();}@Overridepublic 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进行图片的切换效果




0 0