现在写Android项目一般都会用到下拉创新以及上拉加载的功能,要想实现这一功能,就必须要在fragment和activity上实现Scrollable这一功能,基于这一功能,我整理了一下自己写的接口类ScrollableHelper,主要用于为fragment加上相应的功能,这里开始讲一下ScrollableHelper。
首先定义一个接口,用于得到fragment所包含的的view包括ScrollView/ListView/RecycelerView等。代码如图:
/**
* a viewgroup whitch contains ScrollView/ListView/RecycelerView..
*/
public interface ScrollableContainer{
/**
* @return ScrollView/ListView/RecycelerView..'s instance
*/
View getScrollableView();
}
public void setCurrentScrollableContainer(ScrollableContainer scrollableContainer) {
this.mCurrentScrollableCainer = scrollableContainer;
}
private View getScrollableView() {
if (mCurrentScrollableCainer == null) {
return null;
}
return mCurrentScrollableCainer.getScrollableView();
}
再接下来就是具体的实现判断各种view是否处于可以滑动的状态,以及滑动到了的状态,即该view是否是在滑动顶部等。具体的代码实现如下:
/**
*
* 判断是否滑动到顶部方法,ScrollAbleLayout根据此方法来做一些逻辑判断
* 目前只实现了AdapterView,ScrollView,RecyclerView
* 需要支持其他view可以自行补充实现
* @return
*/
public boolean isTop() {
View scrollableView = getScrollableView();
if (scrollableView == null) {
// throw new NullPointerException("You should call ScrollableHelper.setCurrentScrollableContainer() to set ScrollableContainer.");
return true;
}
if (scrollableView instanceof AdapterView) {
return isAdapterViewTop((AdapterView) scrollableView);
}
if (scrollableView instanceof ScrollView) {
return isScrollViewTop((ScrollView) scrollableView);
}
if (scrollableView instanceof RecyclerView) {
return isRecyclerViewTop((RecyclerView) scrollableView);
}
if (scrollableView instanceof WebView) {
return isWebViewTop((WebView) scrollableView);
}
throw new IllegalStateException("scrollableView must be a instance of AdapterView|ScrollView|RecyclerView");
}
private static boolean isRecyclerViewTop(RecyclerView recyclerView) {
if (recyclerView != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
View childAt = recyclerView.getChildAt(0);
if (childAt == null || (firstVisibleItemPosition == 0 && childAt.getTop() == 0)) {
return true;
}
}
}
return false;
}
private static boolean isAdapterViewTop(AdapterView adapterView){
if(adapterView != null){
int firstVisiblePosition = adapterView.getFirstVisiblePosition();
View childAt = adapterView.getChildAt(0);
if(childAt == null || (firstVisiblePosition == 0 && childAt.getTop() == 0)){
return true;
}
}
return false;
}
private static boolean isScrollViewTop(ScrollView scrollView){
if(scrollView != null) {
int scrollViewY = scrollView.getScrollY();
return scrollViewY <= 0;
}
return false;
}
private static boolean isWebViewTop(WebView scrollView){
if(scrollView != null) {
int scrollViewY = scrollView.getScrollY();
return scrollViewY <= 0;
}
return false;
}
@SuppressLint("NewApi")
public void smoothScrollBy(int velocityY, int distance, int duration) {
View scrollableView = getScrollableView();
if (scrollableView instanceof AbsListView) {
AbsListView absListView = (AbsListView) scrollableView;
if (Build.VERSION.SDK_INT >= 21) {
absListView.fling(velocityY);
} else {
absListView.smoothScrollBy(distance, duration);
}
} else if (scrollableView instanceof ScrollView) {
((ScrollView) scrollableView).fling(velocityY);
} else if (scrollableView instanceof RecyclerView) {
((RecyclerView) scrollableView).fling(0, velocityY);
} else if (scrollableView instanceof WebView) {
((WebView) scrollableView).flingScroll(0, velocityY);
}
}
简单的实现的一个帮助接口,使各种view的滑动加以集成,并且使得实现下拉刷新上拉加载这一功能更加顺滑,希望对大家有所帮助。