android:如何使用无限滚动制作无限滚动视图

前端之家收集整理的这篇文章主要介绍了android:如何使用无限滚动制作无限滚动视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要一个代码来让ScrollView显示相同的图像,并且无休止地滚动.

这对于布局非常好,我想知道将它添加到具有无限滚动的ScrollView所需的代码是什么.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <HorizontalScrollView
  12. android:id="@+id/horizontalScrollView1"
  13. android:layout_width="match_parent"
  14.  
  15. android:layout_height="wrap_content" >
  16.  
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"
  20. android:orientation="horizontal" >
  21.  
  22. <ImageView
  23. android:id="@+id/imageView1"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:src="@drawable/ic_launcher" />
  27.  
  28. <ImageView
  29. android:id="@+id/imageView2"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:src="@drawable/ic_launcher" />
  33.  
  34. <ImageView
  35. android:id="@+id/imageView3"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:src="@drawable/ic_launcher" />
  39. <ImageView
  40. android:id="@+id/imageView1"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:src="@drawable/ic_launcher" />
  44.  
  45. <ImageView
  46. android:id="@+id/imageView2"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:src="@drawable/ic_launcher" />
  50.  
  51. <ImageView
  52. android:id="@+id/imageView3"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:src="@drawable/ic_launcher" />
  56. <ImageView
  57. android:id="@+id/imageView1"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:src="@drawable/ic_launcher" />
  61.  
  62. <ImageView
  63. android:id="@+id/imageView2"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:src="@drawable/ic_launcher" />
  67.  
  68. <ImageView
  69. android:id="@+id/imageView3"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:src="@drawable/ic_launcher" />
  73.  
  74.  
  75. </LinearLayout>
  76. </HorizontalScrollView>

解决方法

使用ListView和一个稍微修改过的具有“无限”元素的适配器

以下是适配器的更改,它将支持此行为:

  1. @Override
  2. public int getCount()
  3. {
  4. return Integer.MAX_VALUE;
  5. }
  6.  
  7. @Override
  8. public ImageItem getItem(int position)
  9. {
  10. return mItems.get(position % mItems.size());
  11. }

基本上你通过告诉它计数是MAX_INT然后当你去获得一个项目使用mod来获得序列中的正确项目来欺骗它.

有些人已经提出了不同的解决方案.

见:Android Endless List

CommonsWare也有一个支持这种行为的组件:https://github.com/commonsguy/cwac-endless

猜你在找的Android相关文章