查看: 920|回复: 0

[手机开发] Android实现轮播图无限循环效果

发表于 2017-8-1 08:00:04

本文实例为大家分享了Android轮播图无限循环的具体代码,供大家参考,具体内容如下

实现无限循环

在getCount()方法中,返回一个很大的值,Integer.MAX_VALUE

在instantiateItem()方法中,获取当前View的索引时,进行取于操作,传递进来的int position是个非常大的数,对他进行求余数

在destroyItem()方法中,同样

在onPageSelected()监听方法中,对传递进来的索引进行取于

反向的无限循环

调用ViewPager对象的setCurrentItem()方法,设置第一次进来时候的当前页,参数:int数字,我们把它定位到Integer.MAX_VALUE的中间,并且保证第一条是当前位置,Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size())

目前有个左划空白页的问题没解决

  1. package com.tsh.myviewpager;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.support.v4.view.PagerAdapter;
  7. import android.support.v4.view.ViewPager;
  8. import android.support.v4.view.ViewPager.OnPageChangeListener;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.TextView;
  14. public class MainActivity extends Activity {
  15. private ViewPager vp_banner;
  16. private LinearLayout ll_points;
  17. private TextView tv_title;
  18. private List<View> banners;
  19. private String[] titles=new String[]{
  20. "新闻标题1",
  21. "新闻标题2",
  22. "新闻标题3"
  23. };
  24. private List<ImageView> points=new ArrayList<ImageView>();
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. // 初始化
  30. vp_banner = (ViewPager) findViewById(R.id.vp_banner);
  31. ll_points = (LinearLayout) findViewById(R.id.ll_points);
  32. tv_title = (TextView) findViewById(R.id.tv_title);
  33. // banner部分
  34. banners = new ArrayList<>();
  35. ImageView img1 = new ImageView(this);
  36. img1.setImageResource(R.drawable.a);
  37. banners.add(img1);
  38. ImageView img2 = new ImageView(this);
  39. img2.setImageResource(R.drawable.b);
  40. banners.add(img2);
  41. ImageView img3 = new ImageView(this);
  42. img3.setImageResource(R.drawable.c);
  43. banners.add(img3);
  44. //小图标
  45. ll_points=(LinearLayout) findViewById(R.id.ll_points);
  46. for(int i=0;i<banners.size();i++){
  47. ImageView image=new ImageView(this);
  48. image.setImageResource(R.drawable.point_selector);
  49. LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(5, 5);
  50. params.rightMargin=4;
  51. image.setLayoutParams(params);
  52. if(i==0){
  53. image.setEnabled(true);
  54. }else{
  55. image.setEnabled(false);
  56. }
  57. points.add(image);
  58. ll_points.addView(image);
  59. }
  60. // 设置适配器
  61. vp_banner.setAdapter(new MyPagerAdapter());
  62. vp_banner.setCurrentItem(Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size()));
  63. vp_banner.setOnPageChangeListener(new OnPageChangeListener() {
  64. @Override
  65. public void onPageSelected(int arg0) {
  66. tv_title.setText(titles[arg0%banners.size()]);
  67. //小图标处理
  68. for(ImageView point:points){
  69. point.setEnabled(false);
  70. }
  71. points.get(arg0%banners.size()).setEnabled(true);
  72. }
  73. @Override
  74. public void onPageScrolled(int arg0, float arg1, int arg2) {
  75. // TODO Auto-generated method stub
  76. }
  77. @Override
  78. public void onPageScrollStateChanged(int arg0) {
  79. // TODO Auto-generated method stub
  80. }
  81. });
  82. }
  83. private class MyPagerAdapter extends PagerAdapter {
  84. @Override
  85. public void destroyItem(ViewGroup container, int position, Object object) {
  86. System.out.println("销毁:"+position);
  87. container.removeView((View)object);
  88. object=null;
  89. }
  90. @Override
  91. public Object instantiateItem(ViewGroup container, int position) {
  92. System.out.println("初始化:"+position);
  93. position=position%banners.size();
  94. //解决右划出错
  95. if(banners.get(position).getParent()!=null){
  96. //container.removeView(banners.get(position));
  97. }else{
  98. container.addView(banners.get(position));
  99. }
  100. return banners.get(position);
  101. }
  102. @Override
  103. public int getCount() {
  104. return Integer.MAX_VALUE;
  105. }
  106. @Override
  107. public boolean isViewFromObject(View arg0, Object arg1) {
  108. return arg0 == arg1;
  109. }
  110. }
  111. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条