当前位置:   article > 正文

Android中ScrollView嵌套ListView_android scrollview嵌套listview

android scrollview嵌套listview

参考网址:https://blog.csdn.net/wanghao200906/article/details/51084975

1.ScrollView嵌套ListView(listView所有条目展示出来,并且不能滑动了)

  1. package com.view;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.MotionEvent;
  5. import android.widget.ListView;
  6. public class ListViewForScrollView extends ListView {
  7. public ListViewForScrollView(Context context) {
  8. super(context);
  9. }
  10. public ListViewForScrollView(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. }
  13. public ListViewForScrollView(Context context, AttributeSet attrs,
  14. int defStyle) {
  15. super(context, attrs, defStyle);
  16. }
  17. @Override
  18. /**
  19. * 重写该方法,达到使ListView适应ScrollView的效果
  20. */
  21. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  22. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  23. MeasureSpec.AT_MOST);
  24. super.onMeasure(widthMeasureSpec, expandSpec);
  25. }
  26. // @Override
  27. // public boolean dispatchTouchEvent(MotionEvent ev) {
  28. // getParent().requestDisallowInterceptTouchEvent(true);
  29. // return super.dispatchTouchEvent(ev);
  30. // }
  31. }

2.ScrollView嵌套ListView(ListView可以指定高度,嵌套后ListView还可以进行滑动,焦点的分发)

  1. package com.view;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.widget.ListView;
  7. public class MyListView extends ListView {
  8. private static final String TAG = "MyListView";
  9. public MyListView(Context context) {
  10. this(context, null);
  11. }
  12. public MyListView(Context context, AttributeSet attrs) {
  13. this(context, attrs, 0);
  14. }
  15. public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
  16. super(context, attrs, defStyleAttr);
  17. }
  18. @Override
  19. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  20. int defaultsize=measureHight(Integer.MAX_VALUE >> 2, heightMeasureSpec);
  21. int expandSpec = MeasureSpec.makeMeasureSpec(defaultsize, MeasureSpec.AT_MOST);
  22. super.onMeasure(widthMeasureSpec, expandSpec);
  23. }
  24. private int measureHight(int size, int measureSpec) {
  25. int result = 0;
  26. int specMode = MeasureSpec.getMode(measureSpec);
  27. int specSize = MeasureSpec.getSize(measureSpec);
  28. if (specMode == MeasureSpec.EXACTLY) {
  29. Log.i(TAG, "exactly" );
  30. result = specSize;
  31. } else {
  32. result = size;//最小值是200px ,自己设定
  33. if (specMode == MeasureSpec.AT_MOST) {
  34. result = Math.min(result, specSize);
  35. }
  36. Log.i(TAG, "specMode:"+specMode+"--result:"+result );
  37. }
  38. return result;
  39. }
  40. /**
  41. * 改listview滑到底端了
  42. *
  43. * @return
  44. */
  45. public boolean isBottom() {
  46. int firstVisibleItem = getFirstVisiblePosition();//屏幕上显示的第一条是list中的第几条
  47. int childcount = getChildCount();//屏幕上显示多少条item
  48. int totalItemCount = getCount();//一共有多少条
  49. if ((firstVisibleItem + childcount) >=totalItemCount) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. /**
  55. * 改listview在顶端
  56. *
  57. * @return
  58. */
  59. public boolean isTop() {
  60. int firstVisibleItem = getFirstVisiblePosition();
  61. if (firstVisibleItem ==0) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. float down = 0;
  67. float y;
  68. @Override
  69. public boolean dispatchTouchEvent(MotionEvent event) {
  70. switch (event.getAction()) {
  71. case MotionEvent.ACTION_DOWN:
  72. down = event.getRawY();
  73. getParent().requestDisallowInterceptTouchEvent(true);
  74. break;
  75. case MotionEvent.ACTION_MOVE:
  76. y = event.getRawY();
  77. if (isTop()) {
  78. if (y - down > 1) {
  79. // 到顶端,向下滑动 把事件教给父类
  80. getParent().requestDisallowInterceptTouchEvent(false);
  81. } else {
  82. // 到顶端,向上滑动 把事件拦截 由自己处理
  83. getParent().requestDisallowInterceptTouchEvent(true);
  84. }
  85. }
  86. if (isBottom()) {
  87. if (y - down > 1) {
  88. // 到底端,向下滑动 把事件拦截 由自己处理
  89. getParent().requestDisallowInterceptTouchEvent(true);
  90. } else {
  91. // 到底端,向上滑动 把事件教给父类
  92. getParent().requestDisallowInterceptTouchEvent(false);
  93. }
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. return super.dispatchTouchEvent(event);
  100. }
  101. }

禁止ListView条目点击(在Adapter中重写以下方法)

  1. @Override
  2. public boolean isEnabled(int position) {
  3. return false;
  4. }

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

闽ICP备14008679号