Skip to content

Commit

Permalink
处理RecyclerView reverseLayout的滑动冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
donkingliang committed Aug 13, 2022
1 parent f901aeb commit a3d68e1
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import androidx.core.view.ScrollingView;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand All @@ -34,12 +36,12 @@ static int computeVerticalScrollOffset(View view) {
}

try {
if (computeVerticalScrollOffsetMethod == null){
if (computeVerticalScrollOffsetMethod == null) {
computeVerticalScrollOffsetMethod = View.class.getDeclaredMethod("computeVerticalScrollOffset");
computeVerticalScrollOffsetMethod.setAccessible(true);
}
Object o = computeVerticalScrollOffsetMethod.invoke(scrolledView);
if (o != null){
if (o != null) {
return (int) o;
}
} catch (Exception e) {
Expand All @@ -61,7 +63,7 @@ static int computeVerticalScrollRange(View view) {
computeVerticalScrollRangeMethod.setAccessible(true);
}
Object o = computeVerticalScrollRangeMethod.invoke(scrolledView);
if (o != null){
if (o != null) {
return (int) o;
}
} catch (Exception e) {
Expand All @@ -78,13 +80,13 @@ static int computeVerticalScrollExtent(View view) {
}

try {
if (computeVerticalScrollExtentMethod == null){
if (computeVerticalScrollExtentMethod == null) {
computeVerticalScrollExtentMethod = View.class.getDeclaredMethod("computeVerticalScrollExtent");
computeVerticalScrollExtentMethod.setAccessible(true);
}

Object o = computeVerticalScrollExtentMethod.invoke(scrolledView);
if (o != null){
if (o != null) {
return (int) o;
}
} catch (Exception e) {
Expand Down Expand Up @@ -184,7 +186,22 @@ static boolean canScrollVertically(View view, int direction) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();

if (layoutManager != null && adapter != null && adapter.getItemCount() > 0) {
View itemView = layoutManager.findViewByPosition(direction > 0 ? adapter.getItemCount() - 1 : 0);

boolean isReverseLayout = false;
if (layoutManager instanceof LinearLayoutManager) {
isReverseLayout = ((LinearLayoutManager) layoutManager).getReverseLayout();
} else if (layoutManager instanceof StaggeredGridLayoutManager){
isReverseLayout = ((StaggeredGridLayoutManager) layoutManager).getReverseLayout();
}

int targetPosition = 0;
if (isReverseLayout) {
targetPosition = direction < 0 ? adapter.getItemCount() - 1 : 0;
} else {
targetPosition = direction > 0 ? adapter.getItemCount() - 1 : 0;
}

View itemView = layoutManager.findViewByPosition(targetPosition);
if (itemView == null) {
return true;
}
Expand Down Expand Up @@ -370,7 +387,7 @@ static View getScrollChild(View view) {

if (lp instanceof ConsecutiveScrollerLayout.LayoutParams) {
int childId = ((ConsecutiveScrollerLayout.LayoutParams) lp).scrollChild;
if (childId != View.NO_ID){
if (childId != View.NO_ID) {
View child = view.findViewById(childId);
if (child != null) {
return child;
Expand Down

0 comments on commit a3d68e1

Please sign in to comment.