Skip to content

Commit

Permalink
Don't depend on LinearLayoutManager
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Aug 17, 2019
1 parent 9468615 commit d9f001d
Showing 1 changed file with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
Expand All @@ -33,7 +32,6 @@ public class OrientationAwareRecyclerView extends RecyclerView {

private float lastX = 0.0f;
private float lastY = 0.0f;
private int orientation = RecyclerView.VERTICAL;
private boolean scrolling = false;

public OrientationAwareRecyclerView(@NonNull Context context) {
Expand All @@ -57,15 +55,12 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat
}

@Override
public void setLayoutManager(@Nullable LayoutManager layout) {
super.setLayoutManager(layout);
if (layout instanceof LinearLayoutManager) {
orientation = ((LinearLayoutManager) layout).getOrientation();
public boolean onInterceptTouchEvent(MotionEvent e) {
final LayoutManager lm = getLayoutManager();
if (lm == null) {
return super.onInterceptTouchEvent(e);
}
}

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
boolean allowScroll = true;

switch (e.getActionMasked()) {
Expand All @@ -87,8 +82,7 @@ public boolean onInterceptTouchEvent(MotionEvent e) {
float currentY = e.getY();
float dx = Math.abs(currentX - lastX);
float dy = Math.abs(currentY - lastY);
allowScroll = dy > dx ? orientation == RecyclerView.VERTICAL
: orientation == RecyclerView.HORIZONTAL;
allowScroll = dy > dx ? lm.canScrollVertically() : lm.canScrollHorizontally();
break;
}
}
Expand Down

0 comments on commit d9f001d

Please sign in to comment.