2 u011404046 u011404046 于 2013.11.26 22:28 提问

android ScrollView实现原理,求助!

能解释下ScrollView滚动显示的原理吗?就比如说你的ScollView有LinearLayout然后LinearLayout内有100个Button(id=1,2,3,4......),创建初,屏幕显示id=1~10的Button,当你向下拖的时候如何重新绘制控件,显示id=3~12的Button,我看ScrollView的源代码,并没有找到这部分功能,不知道能否解释下到底是怎么实现的,在哪里实现的?多谢!!

1个回答

u012968919
u012968919   2013.11.27 09:19
已采纳

01.package com.ql.view;

02.

03.import android.content.Context;

04.import android.os.Handler;

05.import android.os.Message;

06.import android.util.AttributeSet;

07.import android.view.MotionEvent;

08.import android.view.View;

09.import android.widget.ScrollView;

10.

11.public class LazyScrollView extends ScrollView{

12. private static final String tag="LazyScrollView";

13. private Handler handler;

14. private View view;

15. public LazyScrollView(Context context) {

16. super(context);

17. // TODO Auto-generated constructor stub

18. }

19. public LazyScrollView(Context context, AttributeSet attrs) {

20. super(context, attrs);

21. // TODO Auto-generated constructor stub

22. }

23. public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {

24. super(context, attrs, defStyle);

25. // TODO Auto-generated constructor stub

26. }

27. //这个获得总的高度

28. public int computeVerticalScrollRange(){

29. return super.computeHorizontalScrollRange();

30. }

31. public int computeVerticalScrollOffset(){

32. return super.computeVerticalScrollOffset();

33. }

34. private void init(){

35.

36. this.setOnTouchListener(onTouchListener);

37. handler=new Handler(){

38. @Override

39. public void handleMessage(Message msg) {

40. // process incoming messages here

41. super.handleMessage(msg);

42. switch(msg.what){

43. case 1:

44. if(view.getMeasuredHeight() <= getScrollY() + getHeight()) {

45. if(onScrollListener!=null){

46. onScrollListener.onBottom();

47. }

48.

49. }else if(getScrollY()==0){

50. if(onScrollListener!=null){

51. onScrollListener.onTop();

52. }

53. }

54. else{

55. if(onScrollListener!=null){

56. onScrollListener.onScroll();

57. }

58. }

59. break;

60. default:

61. break;

62. }

63. }

64. };

65.

66. }

67.

68. OnTouchListener onTouchListener=new OnTouchListener(){

69.

70. @Override

71. public boolean onTouch(View v, MotionEvent event) {

72. // TODO Auto-generated method stub

73. switch (event.getAction()) {

74. case MotionEvent.ACTION_DOWN:

75. break;

76. case MotionEvent.ACTION_UP:

77. if(view!=null&&onScrollListener!=null){

78. handler.sendMessageDelayed(handler.obtainMessage(1), 200);

79. }

80. break;

81.

82. default:

83. break;

84. }

85. return false;

86. }

87.

88. };

89.

90. /**
91. * 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。
92. /

93. public void getView(){

94. this.view=getChildAt(0);

95. if(view!=null){

96. init();

97. }

98. }

99.

100. /
*
101. * 定义接口
102. * @author admin
103. *
104. */

105. public interface OnScrollListener{

106. void onBottom();

107. void onTop();

108. void onScroll();

109. }

110. private OnScrollListener onScrollListener;

111. public void setOnScrollListener(OnScrollListener onScrollListener){

112. this.onScrollListener=onScrollListener;

113. }

114.}

u011404046
u011404046 这是什么?我问的是源代码里的问题···
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片