/**
|
* Copyright 2015 bingoogolapple
|
* <p/>
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
* <p/>
|
* http://www.apache.org/licenses/LICENSE-2.0
|
* <p/>
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
package com.hdl.photovoltaic.widget.refreshlayout;
|
|
import android.content.Context;
|
import android.graphics.Rect;
|
import android.util.DisplayMetrics;
|
import android.view.View;
|
import android.view.ViewParent;
|
import android.view.WindowManager;
|
import android.webkit.WebView;
|
import android.widget.AbsListView;
|
import android.widget.ScrollView;
|
|
import androidx.core.view.ViewCompat;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
|
|
|
import java.lang.reflect.Field;
|
|
/**
|
* 作者:王浩 邮件:bingoogolapple@gmail.com
|
* 创建时间:15/10/28 上午2:26
|
* 描述:
|
*/
|
public class BGARefreshScrollingUtil {
|
|
private BGARefreshScrollingUtil() {
|
}
|
|
public static boolean isScrollViewOrWebViewToTop(View view) {
|
return view != null && view.getScrollY() == 0;
|
}
|
|
public static boolean isAbsListViewToTop(AbsListView absListView) {
|
if (absListView != null) {
|
int firstChildTop = 0;
|
if (absListView.getChildCount() > 0) {
|
// 如果AdapterView的子控件数量不为0,获取第一个子控件的top
|
firstChildTop = absListView.getChildAt(0).getTop() - absListView.getPaddingTop();
|
}
|
if (absListView.getFirstVisiblePosition() == 0 && firstChildTop == 0) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public static boolean isRecyclerViewToTop(RecyclerView recyclerView) {
|
if (recyclerView != null) {
|
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
|
if (manager == null) {
|
return true;
|
}
|
if (manager.getItemCount() == 0) {
|
return true;
|
}
|
|
if (manager instanceof LinearLayoutManager) {
|
LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
|
|
int firstChildTop = 0;
|
if (recyclerView.getChildCount() > 0) {
|
// 处理item高度超过一屏幕时的情况
|
View firstVisibleChild = recyclerView.getChildAt(0);
|
if (firstVisibleChild != null && firstVisibleChild.getMeasuredHeight() >= recyclerView.getMeasuredHeight()) {
|
if (android.os.Build.VERSION.SDK_INT < 14) {
|
return !(ViewCompat.canScrollVertically(recyclerView, -1) || recyclerView.getScrollY() > 0);
|
} else {
|
return !ViewCompat.canScrollVertically(recyclerView, -1);
|
}
|
}
|
|
// 如果RecyclerView的子控件数量不为0,获取第一个子控件的top
|
// 解决item的topMargin不为0时不能触发下拉刷新
|
View firstChild = recyclerView.getChildAt(0);
|
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) firstChild.getLayoutParams();
|
firstChildTop = firstChild.getTop() - layoutParams.topMargin - getRecyclerViewItemTopInset(layoutParams) - recyclerView.getPaddingTop();
|
}
|
|
if (layoutManager.findFirstCompletelyVisibleItemPosition() < 1 && firstChildTop == 0) {
|
return true;
|
}
|
} else if (manager instanceof StaggeredGridLayoutManager) {
|
StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
|
|
int[] out = layoutManager.findFirstCompletelyVisibleItemPositions(null);
|
if (out[0] < 1) {
|
return true;
|
}
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 通过反射获取RecyclerView的item的topInset
|
*
|
* @param layoutParams
|
* @return
|
*/
|
private static int getRecyclerViewItemTopInset(RecyclerView.LayoutParams layoutParams) {
|
try {
|
Field field = RecyclerView.LayoutParams.class.getDeclaredField("mDecorInsets");
|
field.setAccessible(true);
|
// 开发者自定义的滚动监听器
|
Rect decorInsets = (Rect) field.get(layoutParams);
|
return decorInsets.top;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
public static boolean isStickyNavLayoutToTop(BGAStickyNavLayout stickyNavLayout) {
|
return isScrollViewOrWebViewToTop(stickyNavLayout) && stickyNavLayout.isContentViewToTop();
|
}
|
|
|
public static boolean isWebViewToBottom(WebView webView) {
|
return webView != null && webView.getContentHeight() * webView.getScale() == (webView.getScrollY() + webView.getMeasuredHeight());
|
}
|
|
public static boolean isScrollViewToBottom(ScrollView scrollView) {
|
if (scrollView != null) {
|
int scrollContentHeight = scrollView.getScrollY() + scrollView.getMeasuredHeight() - scrollView.getPaddingTop() - scrollView.getPaddingBottom();
|
int realContentHeight = scrollView.getChildAt(0).getMeasuredHeight();
|
if (scrollContentHeight == realContentHeight) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public static boolean isNestedScrollLayoutToBottom(NestedScrollLayout nestedScrollLayout) {
|
if (nestedScrollLayout != null) {
|
int scrollContentHeight = nestedScrollLayout.getScrollY() + nestedScrollLayout.getMeasuredHeight() - nestedScrollLayout.getPaddingTop() - nestedScrollLayout.getPaddingBottom();
|
int realContentHeight = nestedScrollLayout.getChildAt(0).getMeasuredHeight();
|
if (scrollContentHeight == realContentHeight) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public static boolean isAbsListViewToBottom(AbsListView absListView) {
|
if (absListView != null && absListView.getAdapter() != null && absListView.getChildCount() > 0 && absListView.getLastVisiblePosition() == absListView.getAdapter().getCount() - 1) {
|
View lastChild = absListView.getChildAt(absListView.getChildCount() - 1);
|
|
BGAStickyNavLayout stickyNavLayout = getStickyNavLayout(absListView);
|
if (stickyNavLayout != null) {
|
// 处理BGAStickyNavLayout中lastChild.getBottom() <= absListView.getMeasuredHeight()失效问题
|
|
// 0表示x,1表示y
|
int[] location = new int[2];
|
lastChild.getLocationOnScreen(location);
|
int lastChildBottomOnScreen = location[1] + lastChild.getMeasuredHeight();
|
stickyNavLayout.getLocationOnScreen(location);
|
int stickyNavLayoutBottomOnScreen = location[1] + stickyNavLayout.getMeasuredHeight();
|
return lastChildBottomOnScreen + absListView.getPaddingBottom() <= stickyNavLayoutBottomOnScreen;
|
} else {
|
return lastChild.getBottom() <= absListView.getMeasuredHeight();
|
}
|
}
|
return false;
|
}
|
|
public static boolean isRecyclerViewToBottom(RecyclerView recyclerView) {
|
if (recyclerView != null) {
|
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
|
if (manager == null || manager.getItemCount() == 0) {
|
return false;
|
}
|
|
if (manager instanceof LinearLayoutManager) {
|
// 处理item高度超过一屏幕时的情况
|
View lastVisibleChild = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
|
if (lastVisibleChild != null && lastVisibleChild.getMeasuredHeight() >= recyclerView.getMeasuredHeight()) {
|
if (android.os.Build.VERSION.SDK_INT < 14) {
|
return !(ViewCompat.canScrollVertically(recyclerView, 1) || recyclerView.getScrollY() < 0);
|
} else {
|
return !ViewCompat.canScrollVertically(recyclerView, 1);
|
}
|
}
|
|
LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
|
if (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
|
BGAStickyNavLayout stickyNavLayout = getStickyNavLayout(recyclerView);
|
if (stickyNavLayout != null) {
|
// 处理BGAStickyNavLayout中findLastCompletelyVisibleItemPosition失效问题
|
View lastCompletelyVisibleChild = layoutManager.getChildAt(layoutManager.findLastCompletelyVisibleItemPosition());
|
if (lastCompletelyVisibleChild == null) {
|
return true;
|
} else {
|
// 0表示x,1表示y
|
int[] location = new int[2];
|
lastCompletelyVisibleChild.getLocationOnScreen(location);
|
int lastChildBottomOnScreen = location[1] + lastCompletelyVisibleChild.getMeasuredHeight();
|
stickyNavLayout.getLocationOnScreen(location);
|
int stickyNavLayoutBottomOnScreen = location[1] + stickyNavLayout.getMeasuredHeight();
|
return lastChildBottomOnScreen <= stickyNavLayoutBottomOnScreen;
|
}
|
} else {
|
return true;
|
}
|
}
|
} else if (manager instanceof StaggeredGridLayoutManager) {
|
StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
|
|
int[] out = layoutManager.findLastCompletelyVisibleItemPositions(null);
|
int lastPosition = layoutManager.getItemCount() - 1;
|
for (int position : out) {
|
if (position == lastPosition) {
|
return true;
|
}
|
}
|
}
|
}
|
return false;
|
}
|
|
|
public static void scrollToBottom(final ScrollView scrollView) {
|
if (scrollView != null) {
|
scrollView.post(new Runnable() {
|
@Override
|
public void run() {
|
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
|
}
|
});
|
}
|
}
|
|
public static void scrollToBottom(final AbsListView absListView) {
|
if (absListView != null) {
|
if (absListView.getAdapter() != null && absListView.getAdapter().getCount() > 0) {
|
absListView.post(new Runnable() {
|
@Override
|
public void run() {
|
absListView.setSelection(absListView.getAdapter().getCount() - 1);
|
}
|
});
|
}
|
}
|
}
|
|
public static void scrollToBottom(final RecyclerView recyclerView) {
|
if (recyclerView != null) {
|
if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
|
recyclerView.post(new Runnable() {
|
@Override
|
public void run() {
|
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
|
}
|
});
|
}
|
}
|
}
|
|
public static BGAStickyNavLayout getStickyNavLayout(View view) {
|
ViewParent viewParent = view.getParent();
|
while (viewParent != null) {
|
if (viewParent instanceof BGAStickyNavLayout) {
|
return (BGAStickyNavLayout) viewParent;
|
}
|
viewParent = viewParent.getParent();
|
}
|
return null;
|
}
|
|
public static int getScreenHeight(Context context) {
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
DisplayMetrics dm = new DisplayMetrics();
|
windowManager.getDefaultDisplay().getMetrics(dm);
|
return dm.heightPixels;
|
}
|
}
|