项目中我们经常会用到各种列表需求,有的时候列表数据过多,用户滑动到最下面 ,返回到最上面是很不方便的,所以我们就需要一个一键置顶的功能。
效果图:
这里面需要注意一点是 这个按钮加一个判断,当可见区域超过一屏时候显示此按钮 否则隐藏
判断逻辑代码:
mListRefreshView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int lastVisiblePosition = view.getLastVisiblePosition(); //判断置顶按钮显示隐藏 if(lastVisiblePosition > PAGE_SIZE){ topBtn.setVisibility(View.VISIBLE); }else { topBtn.setVisibility(View.GONE); } if (totalItemCount > 10 && lastVisiblePosition == totalItemCount - 10) { if (mStandardList != null && mStandardList.size() < (PAGE_INDEX - 1) * PAGE_SIZE) { mListRefreshView.onRefreshComplete(); mListRefreshView.setPullToRefreshEnabled(false); mTvLoadAll.setVisibility(View.VISIBLE); } else { getData(1, 1); } } } });
一键置顶按钮布局
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.jky.mobilebzt.pulltorefresh.PullToRefreshListView android:id="@+id/plv_standard" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="2dp" android:background="@color/white" android:cacheColorHint="@null" android:groupIndicator="@null" android:scrollbars="none" /> <include android:id="@+id/no_data_view" layout="@layout/layout_no_data_view" android:visibility="gone"/> </FrameLayout> <TextView android:id="@+id/top_btn" android:layout_width="50dp" android:layout_height="50dp" android:padding="@dimen/padding_10" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="6dp" android:layout_marginRight="6dp" android:background="@drawable/icon_top" /> </RelativeLayout>
点击逻辑
case R.id.top_btn: setListViewPos(0); break; private void setListViewPos(int pos) { if (android.os.Build.VERSION.SDK_INT >= 8) { mListRefreshView.getRefreshableView().smoothScrollToPosition(pos); } else { mListRefreshView.getRefreshableView().setSelection(pos); } }