ViewPager 实现图片轮翻效果 - java 代码片段
很多 App 都有这种效果,特别一些电商类的 App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的。今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的状态也在变化),用定时器来实现不难。
import java.util.ArrayList; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import android.app.Activity; 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 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 ArrayList<ImageView> images; private ArrayList<View> dots; private TextView title; private ViewPager mViewPager; private ViewPagerAdapter adapter; private int oldPosition = 0;//记录上一次点的位置 private int currentItem; //当前页面 private ScheduledExecutorService scheduledExecutorService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //图片 ID imageIds = new int[]{ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; //图片标题 titles = new String[]{ "巩俐不低俗,我就不能低俗", "扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网 TV 版大派送", "热血屌丝的反杀" }; //显示的图片 images = new ArrayList<ImageView>(); for(int i =0; i < imageIds.length; i++){ ImageView imageView = new ImageView(this); imageView.setBackgroundResource(imageIds[i]); images.add(imageView); } //显示的点 dots = new ArrayList<View>(); dots.add(findViewById(R.id.dot_0)); dots.add(findViewById(R.id.dot_1)); dots.add(findViewById(R.id.dot_2)); dots.add(findViewById(R.id.dot_3)); dots.add(findViewById(R.id.dot_4)); title = (TextView) findViewById(R.id.title); title.setText(titles[0]); mViewPager = (ViewPager) findViewById(R.id.vp); adapter = new ViewPagerAdapter(); mViewPager.setAdapter(adapter); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { title.setText(titles[position]); dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal); dots.get(position).setBackgroundResource(R.drawable.dot_focused); oldPosition = position; currentItem = position; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } private class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return images.size(); } //是否是同一张图片 @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup view, int position, Object object) { view.removeView(images.get(position)); } @Override public Object instantiateItem(ViewGroup view, int position) { view.addView(images.get(position)); return images.get(position); } } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); //每隔 2 秒钟切换一张图片 scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(), 2, 2, TimeUnit.SECONDS); } //切换图片 private class ViewPagerTask implements Runnable { @Override public void run() { currentItem = (currentItem +1) % imageIds.length; //更新界面 handler.obtainMessage().sendToTarget(); } } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //设置当前页面 mViewPager.setCurrentItem(<span style="font-family: Arial, Helvetica, sans-serif;">currentItem</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span> } }; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论