下拉刷新和上拉加载更多的前提是能够判断当前显示的数据在数据源中的位置,第一个数据和最后一个数据是否处于可视状态
<pre style="font-family: 宋体; font-size: 12pt; background-color: rgb(39, 40, 34);"><span style="color: rgb(248, 248, 242);"><span style="color:#a6e22e;"><strong>listView</strong></span><span style="color:#f72671;">.</span>setOnScrollListener(<span style="color:#f72671;"><strong>new </strong></span><span style="color:#66d9ef;"><strong><em>AbsListView.OnScrollListener</em></strong></span>() { <span style="color:#f72671;"><strong>boolean </strong></span><span style="color:#a6e22e;"><strong>isLastRow </strong></span><span style="color:#f72671;">= </span><span style="color:#f72671;"><strong>false</strong></span>; <span style="color:#f72671;"><strong>boolean </strong></span><span style="color:#a6e22e;"><strong>isFirstVisible </strong></span><span style="color:#f72671;">= </span><span style="color:#f72671;"><strong>true</strong></span>; <span style="color:#f72671;"><strong>public void </strong></span>onScrollStateChanged(AbsListView <span style="color:#a6e22e;">view</span>, <span style="color:#f72671;"><strong>int </strong></span><span style="color:#a6e22e;">scrollState</span>) { <span style="color:#808080;"><em>//正在滚动时回调,回调2-3次,手指没抛则回调2次。scrollState = 2的这次不回调 </em></span><span style="color:#808080;"><em> //回调顺序如下 </em></span><span style="color:#808080;"><em> //第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动 </em></span><span style="color:#808080;"><em> //第2次:scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作(手指离开屏幕前,用力滑了一下) </em></span><span style="color:#808080;"><em> //第3次:scrollState = SCROLL_STATE_IDLE(0) 停止滚动 </em></span><span style="color:#808080;"><em> //当屏幕停止滚动时为0;当屏幕滚动且用户使用的触碰或手指还在屏幕上时为1; </em></span><span style="color:#808080;"><em> //由于用户的操作,屏幕产生惯性滑动时为2 </em></span><span style="color:#808080;"><em> //当滚到最后一行且停止滚动时,执行加载 </em></span><span style="color:#808080;"><em> </em></span><span style="color:#f72671;"><strong>if </strong></span>(<span style="color:#a6e22e;"><strong>isLastRow </strong></span><span style="color:#f72671;">&& </span><span style="color:#a6e22e;">scrollState </span><span style="color:#f72671;">== </span>AbsListView<span style="color:#f72671;">.</span>OnScrollListener<span style="color:#f72671;">.</span><span style="color:#a6e22e;"><strong><em>SCROLL_STATE_IDLE</em></strong></span>) { <span style="color:#808080;"><em>//加载元素 </em></span><span style="color:#808080;"><em> </em></span><span style="color:#a6e22e;"><strong>isLastRow </strong></span><span style="color:#f72671;">= </span><span style="color:#f72671;"><strong>false</strong></span>; } <span style="color:#808080;"><em>/*判断第一项是否可见*/ </em></span><span style="color:#808080;"><em> </em></span><span style="color:#f72671;"><strong>if </strong></span>(<span style="color:#a6e22e;">scrollState </span><span style="color:#f72671;">== </span>AbsListView<span style="color:#f72671;">.</span>OnScrollListener<span style="color:#f72671;">.</span><span style="color:#a6e22e;"><strong><em>SCROLL_STATE_IDLE</em></strong></span>) { <span style="color:#f72671;"><strong>if </strong></span>(<span style="color:#a6e22e;"><strong>isFirstVisible</strong></span>) { </span><span style="color:#a6e22e;"><strong>...</strong></span><span style="color:#f8f8f2;"> } </span><span style="color: rgb(247, 38, 113); "><strong>else </strong></span><span style="color:#f8f8f2;">{ </span><span style="color:#a6e22e;"><strong>...</strong></span><span style="color:#f8f8f2;"> } } } </span><span style="color: rgb(247, 38, 113); "><strong>public void </strong></span><span style="color:#f8f8f2;">onScroll(AbsListView </span><span style="color: rgb(166, 226, 46);">absListView</span><span style="color:#f8f8f2;">, </span><span style="color: rgb(247, 38, 113); "><strong>int </strong></span><span style="color: rgb(166, 226, 46);">firstVisibleItem</span><span style="color:#f8f8f2;">, </span><span style="color: rgb(247, 38, 113); "><strong>int </strong></span><span style="color: rgb(166, 226, 46);">visibleItemCount</span><span style="color:#f8f8f2;">, </span><span style="color: rgb(247, 38, 113); "><strong>int </strong></span><span style="color: rgb(166, 226, 46);">totalItemCount</span><span style="color:#f8f8f2;">) { </span><span style="color: rgb(102, 217, 239); "><strong>DebugLog</strong></span><span style="color: rgb(247, 38, 113);">.</span><span style="color: rgb(248, 248, 242); font-style: italic;">i</span><span style="color:#f8f8f2;">(</span><span style="color: rgb(255, 231, 146); "><strong>"firstVisibleItem:" </strong></span><span style="color: rgb(247, 38, 113);">+ </span><span style="color: rgb(166, 226, 46);">firstVisibleItem</span><span style="color:#f8f8f2;">); </span><span style="color: rgb(102, 217, 239); "><strong>DebugLog</strong></span><span style="color: rgb(247, 38, 113);">.</span><span style="color: rgb(248, 248, 242); font-style: italic;">i</span><span style="color:#f8f8f2;">(</span><span style="color: rgb(255, 231, 146); "><strong>"visibleItemCount:" </strong></span><span style="color: rgb(247, 38, 113);">+ </span><span style="color: rgb(166, 226, 46);">visibleItemCount</span><span style="color:#f8f8f2;">); </span><span style="color: rgb(102, 217, 239); "><strong>DebugLog</strong></span><span style="color: rgb(247, 38, 113);">.</span><span style="color: rgb(248, 248, 242); font-style: italic;">i</span><span style="color:#f8f8f2;">(</span><span style="color: rgb(255, 231, 146); "><strong>"totalItemCount:" </strong></span><span style="color: rgb(247, 38, 113);">+ </span><span style="color: rgb(166, 226, 46);">totalItemCount</span><span style="color:#f8f8f2;">); </span><span style="color: rgb(128, 128, 128); "><em>/*只有当前几项可见的时候才显示问题详情*/ </em></span><span style="color: rgb(128, 128, 128); "><em> /*也可以判断firstVisible是否为0*/ </em></span><span style="color: rgb(128, 128, 128); "><em> </em></span><span style="color: rgb(247, 38, 113); "><strong>if </strong></span><span style="color:#f8f8f2;">(</span><span style="color: rgb(166, 226, 46);">firstVisibleItem </span><span style="color: rgb(247, 38, 113);"><</span><span style="color: rgb(174, 129, 255);">3</span><span style="color:#f8f8f2;">) { </span><span style="color: rgb(166, 226, 46); "><strong>isFirstVisible</strong></span><span style="color: rgb(247, 38, 113);">=</span><span style="color: rgb(247, 38, 113); "><strong>true</strong></span><span style="color:#f8f8f2;">; } </span><span style="color: rgb(247, 38, 113); "><strong>else </strong></span><span style="color:#f8f8f2;">{ </span><span style="color: rgb(166, 226, 46); "><strong>isFirstVisible</strong></span><span style="color: rgb(247, 38, 113);">=</span><span style="color: rgb(247, 38, 113); "><strong>false</strong></span><span style="color:#f8f8f2;">; } </span><span style="color: rgb(128, 128, 128); "><em>//滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。 </em></span><span style="color: rgb(128, 128, 128); "><em> //firstVisibleItem:当前能看见的第一个列表项ID(从0开始) </em></span><span style="color: rgb(128, 128, 128); "><em> //visibleItemCount:当前能看见的列表项个数(小半个也算) </em></span><span style="color: rgb(128, 128, 128); "><em> //totalItemCount:列表项共数 </em></span><span style="color: rgb(128, 128, 128); "><em> </em></span><span style="color: rgb(128, 128, 128); "><em> //判断是否滚到最后一行 </em></span><span style="color: rgb(128, 128, 128); "><em> </em></span><span style="color: rgb(247, 38, 113); "><strong>if </strong></span><span style="color:#f8f8f2;">(</span><span style="color: rgb(166, 226, 46);">firstVisibleItem </span><span style="color: rgb(247, 38, 113);">+ </span><span style="color: rgb(166, 226, 46);">visibleItemCount </span><span style="color: rgb(247, 38, 113);">== </span><span style="color: rgb(166, 226, 46);">totalItemCount </span><span style="color: rgb(247, 38, 113);">&& </span><span style="color: rgb(166, 226, 46);">totalItemCount </span><span style="color: rgb(247, 38, 113);">> </span><span style="color: rgb(174, 129, 255);">0</span><span style="color:#f8f8f2;">) { </span><span style="color: rgb(128, 128, 128); "><em>/*滚动到最后一行了*/ </em></span><span style="color: rgb(128, 128, 128); "><em> </em></span><span style="color: rgb(166, 226, 46); "><strong>isLastRow </strong></span><span style="color: rgb(247, 38, 113);">= </span><span style="color: rgb(247, 38, 113); "><strong>true</strong></span><span style="color:#f8f8f2;">; } } });</span>