Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SnapHelpers that display multiple items after a snap event #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CircleIndicator.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="CircleIndicator" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.id="CircleIndicator" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -15,8 +15,7 @@
*/
public class CircleIndicator2 extends BaseCircleIndicator {

private RecyclerView mRecyclerView;
private SnapHelper mSnapHelper;
private SnapIndexController mSnapIndexController;

public CircleIndicator2(Context context) {
super(context);
Expand All @@ -37,35 +36,24 @@ public CircleIndicator2(Context context, AttributeSet attrs, int defStyleAttr,
}

public void attachToRecyclerView(@NonNull RecyclerView recyclerView,
@NonNull SnapHelper snapHelper) {
mRecyclerView = recyclerView;
mSnapHelper = snapHelper;
@NonNull SnapIndexController snapIndexController) {
mSnapIndexController = snapIndexController;
mLastPosition = -1;
createIndicators();
recyclerView.removeOnScrollListener(mInternalOnScrollListener);
recyclerView.addOnScrollListener(mInternalOnScrollListener);
}

private void createIndicators() {
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
int count;
if (adapter == null) {
count = 0;
} else {
count = adapter.getItemCount();
}
createIndicators(count, getSnapPosition(mRecyclerView.getLayoutManager()));
public void attachToRecyclerView(@NonNull RecyclerView recyclerView, @NonNull SnapHelper snapHelper) {
mSnapIndexController = new SimpleSnapIndexController(recyclerView, snapHelper);
mLastPosition = -1;
createIndicators();
recyclerView.removeOnScrollListener(mInternalOnScrollListener);
recyclerView.addOnScrollListener(mInternalOnScrollListener);
}

public int getSnapPosition(@Nullable RecyclerView.LayoutManager layoutManager) {
if (layoutManager == null) {
return RecyclerView.NO_POSITION;
}
View snapView = mSnapHelper.findSnapView(layoutManager);
if (snapView == null) {
return RecyclerView.NO_POSITION;
}
return layoutManager.getPosition(snapView);
private void createIndicators() {
createIndicators(mSnapIndexController.getTotalIndicatorCount(), mSnapIndexController.getSnappedIndex());
}

private final RecyclerView.OnScrollListener mInternalOnScrollListener =
Expand All @@ -74,7 +62,7 @@ public int getSnapPosition(@Nullable RecyclerView.LayoutManager layoutManager) {
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

int position = getSnapPosition(recyclerView.getLayoutManager());
int position = mSnapIndexController.getSnappedIndex();
if (position == RecyclerView.NO_POSITION) {
return;
}
Expand All @@ -86,17 +74,13 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
new RecyclerView.AdapterDataObserver() {
@Override public void onChanged() {
super.onChanged();
if (mRecyclerView == null) {
return;
}
RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
int newCount = adapter != null ? adapter.getItemCount() : 0;
int newCount = mSnapIndexController.getTotalIndicatorCount();
int currentCount = getChildCount();
if (newCount == currentCount) {
// No change
return;
} else if (mLastPosition < newCount) {
mLastPosition = getSnapPosition(mRecyclerView.getLayoutManager());
mLastPosition = mSnapIndexController.getSnappedIndex();
} else {
mLastPosition = RecyclerView.NO_POSITION;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.relex.circleindicator;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

class SimpleSnapIndexController implements SnapIndexController {

private final RecyclerView mRecyclerView;
private final SnapHelper mSnapHelper;

public SimpleSnapIndexController(RecyclerView recyclerView, SnapHelper snapHelper) {
mRecyclerView = recyclerView;
mSnapHelper = snapHelper;
}

@Override
public int getSnappedIndex() {
RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
if (layoutManager == null) return RecyclerView.NO_POSITION;

final View snapView = mSnapHelper.findSnapView(layoutManager);
if (snapView == null) return RecyclerView.NO_POSITION;

return layoutManager.getPosition(snapView);
}

@Override
public int getTotalIndicatorCount() {
final RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
if (adapter == null) return 0;
return adapter.getItemCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.relex.circleindicator;

public interface SnapIndexController {
int getSnappedIndex();
int getTotalIndicatorCount();
}