【第22期】观点:IT 行业加班,到底有没有价值?

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

原创 2015年07月06日 17:33:49

  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(){

		@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进行图片的切换效果




版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

ViewPager定时器切换图片

主Activity: public class LsnViewPagerActivity extends Activity { private int[] imageResIds; privat...

ViewPager自动切换及手动滑动重新计时的问题

ViewPager自动切换及手动滑动重新计时的问题

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

使用ViewPager实现图片自动切换

1、ViewPager讲解    ViewPager是中的控件,使用时直接在布局文件中引用,跟正常控件一样。但是,要给ViewPager设置适配器PagerAdapter,其中有四个方法是适配器Pag...

Android开发--ViewPager自动循环滚动,手动换页

ViewPager以前有简单的用过,但没做过自动滚动和循环滚动,在实现起来还是有很多细节需要注意。总体思路:      1.实现循环滚动的思路就是:             1)将PagerAdapt...
  • smbroe
  • smbroe
  • 2014-12-05 09:30
  • 2234

ViewPager实现每隔两秒自动切换图片

布局代码: 1 RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns...

ViewPager,实现真正的无限循环(定时+手动)

原文:http://blog.csdn.net/gaojinshan/article/details/18038181 利用定时器,实现循环轮播,很简单;只需在定时器的消息里加如下代码即可: ...

自定义ViewPager实现图片自动切换

最近项目刚上线测试版,现在闲下来两天,赶紧就捣鼓了些新功能。于是乎就写了个ViewPager的图片切换器。这个功能很实用,大多APP都有用到,所以花了点时间,给以后开发储备点 成熟的资源。 先给大家...

【ViewPager的学习】三、实现自动循环切换

要实现效果如图: (一)实现循环滑动: (1)在MyAdapter中首先修改getCount()方法中的大小,让ViewPager可滑动页面增多。 (2)instantiateItem()方法中将...

ViewPager实现每隔两秒自动切换图片 (再续)

Meng_fengyang
  • Meng_fengyang
  • 2016-06-06 10:27
  • 369
  • Android三种实现定时器的方法

    方法一:Handler+Thread package com.xunfang.handerDemo; import android.app.Activity; import android.o...
    收藏助手
    不良信息举报
    您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
    举报原因:
    原因补充:

    (最多只允许输入30个字)