当前位置:   article > 正文

横向滑动视图HorizontalScrollView精炼详解

horizontalscrollview

 

一、前期基础知识储备

由于移动设备物理显示空间一般有限,不可能一次性的把所有要显示的内容都显示在屏幕上。所以各大平台一般会提供一些可滚动的视图来向用户展示数据。Android平台框架中为我们提供了诸如ListView、GirdView、ScrollView、RecyclerView等滚动视图控件,这几个视图控件也是我们平常使用最多的。本节内容我们来分析一下横向滚动视图HorizontalScrollView。

HorizontalScrollView是FrameLayout的子类,这意味着你只能在它下面放置一个子控件,这个子控件可以包含很多数据内容。有可能这个子控件本身就是一个布局控件,可以包含非常多的其他用来展示数据的控件。这个布局控件一般使用的是一个水平布局的LinearLayout

本节内容使用HorizontalScrollView分为两种情形:

①横向布局视图中放入文字;

②横向布局视图中放入图片

二、上代码,具体实现文字类的横向布局

(1)布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.administrator.hscrollview.MainActivity">
  11. <HorizontalScrollView
  12. android:id="@+id/horizontalScrollView"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:background="#007b12">
  16. <LinearLayout
  17. android:id="@+id/horizontalScrollViewItemContainer"
  18. android:layout_width="wrap_content"
  19. android:layout_height="match_parent"
  20. android:orientation="horizontal" />
  21. </HorizontalScrollView>
  22. <TextView
  23. android:id="@+id/testTextView"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_centerHorizontal="true"
  27. android:layout_centerVertical="true"
  28. android:text="TextView_Test" />
  29. </RelativeLayout>

(2)主Activity代码文件

  1. public class MainActivity extends AppCompatActivity
  2. {
  3. private HorizontalScrollView horizontalScrollView;
  4. private LinearLayout container;
  5. private String cities[] = new String[]{"London", "Bangkok", "Paris", "Dubai", "Istanbul", "New York",
  6. "Singapore", "Kuala Lumpur", "Hong Kong", "Tokyo", "Barcelona",
  7. "Vienna", "Los Angeles", "Prague", "Rome", "Seoul", "Mumbai", "Jakarta",
  8. "Berlin", "Beijing", "Moscow", "Taipei", "Dublin", "Vancouver"};
  9. private ArrayList<String> data = new ArrayList<>();
  10. private TextView testTextView;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState)
  13. {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_centerlockhorizontalscrollview);
  16. bindData();
  17. setUIRef();
  18. bindHZSWData();
  19. }
  20. //将集合中的数据绑定到HorizontalScrollView上
  21. private void bindHZSWData()
  22. { //为布局中textview设置好相关属性
  23. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  24. ViewGroup.LayoutParams.WRAP_CONTENT);
  25. layoutParams.gravity = Gravity.CENTER;
  26. layoutParams.setMargins(20, 10, 20, 10);
  27. for (int i = 0; i < data.size(); i++)
  28. {
  29. TextView textView = new TextView(this);
  30. textView.setText(data.get(i));
  31. textView.setTextColor(Color.WHITE);
  32. textView.setLayoutParams(layoutParams);
  33.             container.addView(textView);  
  34.             container.invalidate();  
  35. }
  36. }
  37. //初始化布局中的控件
  38. private void setUIRef()
  39. {
  40. horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
  41. container = (LinearLayout) findViewById(R.id.horizontalScrollViewItemContainer);
  42. testTextView = (TextView) findViewById(R.id.testTextView);
  43. }
  44. //将字符串数组与集合绑定起来
  45. private void bindData()
  46. {
  47. //add all cities to our ArrayList
  48. Collections.addAll(data, cities);
  49. }
  50. }

运行效果如图:

(3)为HorizontalScrollView中的item设置点击事件

在上面的代码中添加两段代码

  1. private void bindHZSWData() {
  2. ....
  3. ....
  4. for (int i = 0; i < data.size(); i++) {
  5. TextView textView = new TextView(this);
  6. textView.setText(data.get(i));
  7. textView.setTextColor(Color.WHITE);
  8. textView.setLayoutParams(layoutParams);
  9. textView.setOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View view) {
  12. performItemClick(view);
  13. }
  14. });
  15. ....
  16. }
  17. }
  1. private void performItemClick(View view) {
  2. //------get Display's Width--------
  3. DisplayMetrics displayMetrics = new DisplayMetrics();
  4. getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  5. int screenWidth = displayMetrics.widthPixels;
  6. int scrollX = (view.getLeft() - (screenWidth / 2)) + (view.getWidth() / 2);
  7. //smooth scrolling horizontalScrollView
  8. horizontalScrollView.smoothScrollTo(scrollX, 0);
  9. //additionally we set current center textView data to our testTextView
  10. String s = "CenterLocked Item: "+((TextView)view).getText();
  11. testTextView.setText(s);
  12. }

为了展示显示效果,将每次item中的text设置到界面中,进行显示,运行效果如图:

三、上代码,具体实现图片类的横向布局

(1)主布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.administrator.hscrollview.MainActivity">
  11. <HorizontalScrollView
  12. android:id="@+id/horizontalScrollView"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:background="#007b12">
  16. <LinearLayout
  17. android:id="@+id/horizontalScrollViewItemContainer"
  18. android:layout_width="wrap_content"
  19. android:layout_height="match_parent"
  20. android:orientation="horizontal" />
  21. </HorizontalScrollView>
  22. </RelativeLayout>

(2)主Activity代码

  1. public class MainActivity extends AppCompatActivity {
  2. private HorizontalScrollView horizontalScrollView;
  3. private LinearLayout container;
  4. private Integer mImgIds[] = new Integer[]{R.drawable.aa, R.drawable.bb, R.drawable.cc, R.drawable.dd,
  5. R.drawable.ee, R.drawable.ff, R.drawable.gg, R.drawable.hh, R.drawable.ii, R.drawable.aaa,
  6. R.drawable.bbb, R.drawable.ccc, R.drawable.ddd,
  7. R.drawable.eee, R.drawable.fff, R.drawable.ggg, R.drawable.hhh, R.drawable.iii};
  8. private ArrayList<Integer> data = new ArrayList<>();
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. bindData();
  14. setUIRef();
  15. bindHZSWData();
  16. }
  17. private void bindHZSWData() {
  18. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  19. ViewGroup.LayoutParams.WRAP_CONTENT);
  20. layoutParams.gravity = Gravity.CENTER;
  21. layoutParams.setMargins(20, 10, 20, 10);
  22. for (int i = 0; i < data.size(); i++) {
  23. ImageView imageView = new ImageView(this);
  24. imageView.setImageResource(data.get(i));
  25. imageView.setLayoutParams(layoutParams);
  26. container.addView(imageView);
  27. container.invalidate();
  28. }
  29. }
  30. //初始化布局中定义的控件
  31. private void setUIRef() {
  32. horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
  33. container = (LinearLayout) findViewById(R.id.horizontalScrollViewItemContainer);
  34. testTextView = (TextView) findViewById(R.id.testTextView);
  35. }
  36. //将字符串数组中的数据加入到集合当中
  37. private void bindData() {
  38. //add all cities to our ArrayList
  39. Collections.addAll(data, mImgIds);
  40. }
  41. }

 

运行效果如图:

 

 

当然了,最简单的运用图片类的HorizontalScrollView,就是直接将图片放置在HorizontalScrollView的子布局中进行显示,只需要一个布局文件进行控制,这样做非常简单,UI是通过布局文件进行控制。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.administrator.horizontalscrollview.MainActivity">
  8. <HorizontalScrollView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content">
  11. <LinearLayout
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:orientation="horizontal">
  15. <ImageView
  16. android:layout_width="200dp"
  17. android:layout_height="200dp"
  18. android:background="#ff00ff" />
  19. <ImageView
  20. android:layout_width="200dp"
  21. android:layout_height="200dp"
  22. android:background="#000000" />
  23. <ImageView
  24. android:layout_width="200dp"
  25. android:layout_height="200dp"
  26. android:background="#b7a500" />
  27. <ImageView
  28. android:layout_width="200dp"
  29. android:layout_height="200dp"
  30. android:background="#c1070e" />
  31. <ImageView
  32. android:layout_width="200dp"
  33. android:layout_height="200dp"
  34. android:background="#ff00ff" />
  35. <ImageView
  36. android:layout_width="200dp"
  37. android:layout_height="200dp"
  38. android:background="#000000" />
  39. <ImageView
  40. android:layout_width="200dp"
  41. android:layout_height="200dp"
  42. android:background="#b7a500" />
  43. <ImageView
  44. android:layout_width="200dp"
  45. android:layout_height="200dp"
  46. android:background="#c1070e" />
  47. </LinearLayout>
  48. </HorizontalScrollView>
  49. </RelativeLayout>

注意:无论使用何种方式,注意HoriztalScrollview都只有一个直接子view。否则会报错:

Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child

三、HorizontalScrollView添加自动滚动和回弹效果

1)添加自动滚动效果

HorizontalScrollView并没有内置自动滚动的API方法,所以要自己实现,滚动类似平移,所以采用平移动画实现。

  1. private AnimatorSet mAnimatorSetLeft, mAnimatorSetRight;
  2. private ObjectAnimator mItemsliding;
  3. private ObjectAnimator mItemsAlpha;
  4. //初始化布局中的控件
  5. private void setUIRef() {
  6. horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
  7. UITools.elasticPadding(horizontalScrollView, 300); // 可选 为左右回弹效果实现
  8. //container 为HorizontalScrollView的直接子布局
  9. container = (LinearLayout) findViewById(R.id.horizontalScrollViewItemContainer);
  10. mAnimatorSetLeft = new AnimatorSet();
  11. mAnimatorSetRight = new AnimatorSet();
  12. mItemsliding = ObjectAnimator.ofFloat(container,"translationX",0,-300);
  13. mItemsAlpha = ObjectAnimator.ofFloat(container,"alpha",1,1);
  14. mAnimatorSetLeft.setDuration(0);
  15. mAnimatorSetLeft.play(mItemsliding).with(mItemsAlpha);
  16. mAnimatorSetLeft.start();
  17. mAnimatorSetLeft.addListener(new AnimatorListenerAdapter() {
  18. @Override
  19. public void onAnimationEnd(Animator animation) {
  20. super.onAnimationEnd(animation);
  21. mItemsliding = ObjectAnimator.ofFloat(container,"translationX",-300,0);
  22. mItemsAlpha = ObjectAnimator.ofFloat(container,"alpha",1,1);
  23. mAnimatorSetRight.setStartDelay(500);
  24. mAnimatorSetRight.setDuration(500);
  25. mAnimatorSetRight.play(mItemsliding).with(mItemsAlpha);
  26. mAnimatorSetRight.start();
  27. }
  28. });
  29. testTextView = (TextView) findViewById(R.id.testTextView);
  30. }

注意,这里的动画绑定对象不是HoriztalScrollView而是其直接子布局对象container。

效果如下:

2)添加回弹效果

HorizontalScrollView添加回弹效果,有两种方案:①自定义HorizontalScrollView;②使用工具类;

①自定义HorizontalScrollView,使用时直接作为布局元素替换掉旧的HorizontalScrollView即可;

  1. public class BouncyHScrollView extends HorizontalScrollView {
  2. private static final int MAX_X_OVERSCROLL_DISTANCE = 200;
  3. private Context mContext;
  4. private int mMaxXOverscrollDistance;
  5. public BouncyHScrollView(Context context) {
  6. super(context);
  7. // TODO Auto-generated constructor stub
  8. mContext = context;
  9. initBounceDistance();
  10. }
  11. public BouncyHScrollView(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. // TODO Auto-generated constructor stub
  14. mContext = context;
  15. initBounceDistance();
  16. }
  17. public BouncyHScrollView(Context context, AttributeSet attrs, int defStyle) {
  18. super(context, attrs, defStyle);
  19. // TODO Auto-generated constructor stub
  20. mContext = context;
  21. initBounceDistance();
  22. }
  23. private void initBounceDistance(){
  24. final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
  25. mMaxXOverscrollDistance = (int) (metrics.density * MAX_X_OVERSCROLL_DISTANCE);
  26. }
  27. @Override
  28. protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){
  29. //这块是关键性代码
  30. return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, mMaxXOverscrollDistance, maxOverScrollY, isTouchEvent);
  31. }
  32. }

②工具类;调用代码

  1. public class UITools {
  2. /**HorizontalScrollView添加阻尼效果
  3. * ScrollView效果不太好
  4. * 利用父元素的Padding给ScrollView添加弹性
  5. * @param scrollView
  6. * @param padding
  7. */
  8. public static void elasticPadding(final ScrollView scrollView, final int padding){
  9. View child = scrollView.getChildAt(0);
  10. //记录以前的padding
  11. final int oldpt = child.getPaddingTop();
  12. final int oldpb = child.getPaddingBottom();
  13. //设置新的padding
  14. child.setPadding(child.getPaddingLeft(), padding+oldpt, child.getPaddingRight(), padding+oldpb);
  15. //添加视图布局完成事件监听
  16. scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  17. private boolean inTouch = false; //手指是否按下状态
  18. @SuppressLint("NewApi")
  19. private void disableOverScroll(){
  20. scrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
  21. }
  22. /** 滚动到顶部 */
  23. private void scrollToTop(){
  24. scrollView.smoothScrollTo(scrollView.getScrollX(), padding-oldpt);
  25. }
  26. /** 滚动到底部 */
  27. private void scrollToBottom(){
  28. scrollView.smoothScrollTo(scrollView.getScrollX(), scrollView.getChildAt(0).getBottom()-scrollView.getMeasuredHeight()-padding+oldpb);
  29. }
  30. /** 检测scrollView结束以后,复原位置 */
  31. private final Runnable checkStopped = new Runnable() {
  32. @Override
  33. public void run() {
  34. int y = scrollView.getScrollY();
  35. int bottom = scrollView.getChildAt(0).getBottom()-y-scrollView.getMeasuredHeight();
  36. if(y <= padding && !inTouch){
  37. scrollToTop();
  38. }else if(bottom<=padding && !inTouch){
  39. scrollToBottom();
  40. }
  41. }
  42. };
  43. @SuppressWarnings("deprecation")
  44. @Override
  45. public void onGlobalLayout() {
  46. //移除监听器
  47. scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  48. //设置最小高度
  49. //scrollView.getChildAt(0).setMinimumHeight(scrollView.getMeasuredHeight());
  50. //取消overScroll效果
  51. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD){
  52. disableOverScroll();
  53. }
  54. scrollView.setOnTouchListener(new OnTouchListener() {
  55. @Override
  56. public boolean onTouch(View v, MotionEvent event) {
  57. if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_POINTER_DOWN){
  58. inTouch = true;
  59. }else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  60. inTouch = false;
  61. //手指弹起以后检测一次是否需要复原位置
  62. scrollView.post(checkStopped);
  63. }
  64. return false;
  65. }
  66. });
  67. scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
  68. @Override
  69. public void onScrollChanged() {
  70. if(!inTouch && scrollView!=null && scrollView.getHandler()!=null){//如果持续滚动,移除checkStopped,停止滚动以后只执行一次检测任务
  71. scrollView.getHandler().removeCallbacks(checkStopped);
  72. scrollView.postDelayed(checkStopped, 100);
  73. }
  74. }
  75. });
  76. //第一次加载视图,复原位置
  77. scrollView.postDelayed(checkStopped, 300);
  78. }
  79. });
  80. }
  81. /**
  82. * 利用父元素的Padding给HorizontalScrollView添加弹性
  83. * @param scrollView
  84. * @param padding
  85. */
  86. public static void elasticPadding(final HorizontalScrollView scrollView, final int padding){
  87. Log.i("", "elasticPadding>>>>!!");
  88. View child = scrollView.getChildAt(0);
  89. //记录以前的padding
  90. final int oldpt = child.getPaddingTop();
  91. final int oldpb = child.getPaddingBottom();
  92. //设置新的padding
  93. child.setPadding(padding+oldpt, child.getPaddingTop(), padding+oldpb, child.getPaddingBottom());
  94. //添加视图布局完成事件监听
  95. scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  96. private boolean inTouch = false; //手指是否按下状态
  97. @SuppressLint("NewApi")
  98. private void disableOverScroll(){
  99. scrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
  100. }
  101. /** 滚动到左边 */
  102. private void scrollToLeft(){
  103. scrollView.smoothScrollTo(padding-oldpt, scrollView.getScrollY());
  104. }
  105. /** 滚动到底部 */
  106. private void scrollToRight(){
  107. scrollView.smoothScrollTo(scrollView.getChildAt(0).getRight()-scrollView.getMeasuredWidth()-padding+oldpb, scrollView.getScrollY());
  108. }
  109. /** 检测scrollView结束以后,复原位置 */
  110. private final Runnable checkStopped = new Runnable() {
  111. @Override
  112. public void run() {
  113. int x = scrollView.getScrollX();
  114. int bottom = scrollView.getChildAt(0).getRight()-x-scrollView.getMeasuredWidth();
  115. if(x <= padding && !inTouch){
  116. scrollToLeft();
  117. }else if(bottom<=padding && !inTouch){
  118. scrollToRight();
  119. }
  120. }
  121. };
  122. @SuppressWarnings("deprecation")
  123. @Override
  124. public void onGlobalLayout() {
  125. //移除监听器
  126. scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  127. //取消overScroll效果
  128. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD){
  129. disableOverScroll();
  130. }
  131. scrollView.setOnTouchListener(new OnTouchListener() {
  132. @Override
  133. public boolean onTouch(View v, MotionEvent event) {
  134. if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_POINTER_DOWN){
  135. inTouch = true;
  136. }else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  137. inTouch = false;
  138. //手指弹起以后检测一次是否需要复原位置
  139. scrollView.post(checkStopped);
  140. }
  141. return false;
  142. }
  143. });
  144. scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
  145. @Override
  146. public void onScrollChanged() {
  147. //如果持续滚动,移除checkStopped,停止滚动以后只执行一次检测任务
  148. if(!inTouch && scrollView!=null && scrollView.getHandler()!=null){
  149. scrollView.getHandler().removeCallbacks(checkStopped);
  150. scrollView.postDelayed(checkStopped, 100);
  151. }
  152. }
  153. });
  154. //第一次加载视图,复原位置
  155. scrollView.postDelayed(checkStopped, 300);
  156. }
  157. });
  158. }
  159. }

调用代码:

UITools.elasticPadding(horizontalScrollView, 200);

传入HorizontalScrollView对象和一个int类型(表示回弹的距离)的数值即可.

效果如下:

最后补充两个HorizontalScrollView的滚动方法:

HorizontalScrollView属于Scroll类家族成员,自然少不了控制其滚动的方法:

①滚动到指定位置 —— smoothScrollTo (intx, inty);

②滚动指定距离 —— smoothScrollBy (intx, inty);

 

2019.04.21添加:HorizontalScrollView点击子项自动居中的实现,利用smoothScrollTo ()方法实现:

  1. public class HorCenterActivity extends AppCompatActivity implements View.OnClickListener {
  2. private HorizontalScrollView hor;
  3. private LinearLayout ll;
  4. @Override
  5. protected void onCreate(@Nullable Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_hor);
  8. hor = findViewById(R.id.hor);
  9. ll = findViewById(R.id.ll);
  10. for (int i = 0; i < ll.getChildCount(); i++) {
  11. ll.getChildAt(i).setOnClickListener(this);
  12. }
  13. }
  14. // 实现Horizon自动滚动居中
  15. private void autoScroll(int i) {
  16. // Width of the screen
  17. DisplayMetrics metrics = getResources()
  18. .getDisplayMetrics();
  19. int widthScreen = metrics.widthPixels;
  20. // Width of one child (Button)
  21. int widthChild = ll.getChildAt(i).getWidth(); // 获取对应位置的子View的宽度
  22. // Nb children in screen
  23. int nbChildInScreen = widthScreen / widthChild;
  24. // Child position left
  25. int positionLeftChild = ll.getChildAt(i).getLeft(); // 获取对应位置的子View的左边位置 - 坐标
  26. // Auto scroll to the middle
  27. hor.smoothScrollTo((positionLeftChild - ((nbChildInScreen * widthChild) / 2) + widthChild / 2), 0);
  28. // hor.smoothScrollTo((positionLeftChild - (widthScreen / 2) + widthChild / 2), 0);
  29. }
  30. @Override
  31. public void onClick(View v) {
  32. switch (v.getId()) {
  33. case R.id.a:
  34. autoScroll(0);
  35. break;
  36. case R.id.aa:
  37. autoScroll(1);
  38. break;
  39. case R.id.aaa:
  40. autoScroll(2);
  41. break;
  42. case R.id.aaaa:
  43. autoScroll(3);
  44. break;
  45. case R.id.aaaaa:
  46. autoScroll(4);
  47. break;
  48. case R.id.aaaaaa:
  49. autoScroll(5);
  50. break;
  51. case R.id.aaaaaaa:
  52. autoScroll(6);
  53. break;
  54. case R.id.aaaaaaaa:
  55. autoScroll(7);
  56. break;
  57. }
  58. }
  59. }

如上autoScroll()方法,我们传入子项的索引值即可,从0开始,注意,此实现方式不论子项是否可见,索引值都是不变的,比如一共有7个子项,索引值是0~6,然后将前三个子项设为不可见,此时所有子项的索引值仍然是0~6,而不会有所变化。

效果如下:

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/466558
推荐阅读
相关标签
  

闽ICP备14008679号