Skip to content

Commit

Permalink
Add scrollTo convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Nov 11, 2018
1 parent fb3c8da commit a11abc2
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ android {
dependencies {
implementation project(':gravitysnaphelper')
implementation 'com.google.android.material:material:1.1.0-alpha01'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.github.rubensousa.gravitysnaphelper.GravityPagerSnapHelper;
import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;
import com.google.android.material.button.MaterialButton;

import java.util.ArrayList;

Expand All @@ -22,19 +23,19 @@ public class SnapAdapter extends RecyclerView.Adapter<SnapAdapter.ViewHolder> im
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;

private ArrayList<Snap> mSnaps;
private ArrayList<Snap> snaps;

public SnapAdapter() {
mSnaps = new ArrayList<>();
snaps = new ArrayList<>();
}

public void addSnap(Snap snap) {
mSnaps.add(snap);
snaps.add(snap);
}

@Override
public int getItemViewType(int position) {
Snap snap = mSnaps.get(position);
Snap snap = snaps.get(position);
switch (snap.getGravity()) {
case Gravity.CENTER_VERTICAL:
return VERTICAL;
Expand Down Expand Up @@ -64,8 +65,8 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Snap snap = mSnaps.get(position);
holder.snapTextView.setText(snap.getText());
holder.bind(snaps.get(position));
Snap snap = snaps.get(position);
int padding = holder.recyclerView.getResources().getDimensionPixelOffset(R.dimen.extra_padding);
if (snap.getPadding()) {
if (snap.getGravity() == Gravity.START) {
Expand Down Expand Up @@ -106,25 +107,56 @@ public void onBindViewHolder(ViewHolder holder, int position) {

@Override
public int getItemCount() {
return mSnaps.size();
return snaps.size();
}

@Override
public void onSnap(int position) {
Log.d("Snapped: ", position + "");
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView snapTextView;
public RecyclerView recyclerView;
public MaterialButton scrollButton;
private Snap snap;

public ViewHolder(View itemView) {
super(itemView);
snapTextView = itemView.findViewById(R.id.snapTextView);
recyclerView = itemView.findViewById(R.id.recyclerView);
scrollButton = itemView.findViewById(R.id.scrollButton);
scrollButton.setOnClickListener(this);
}

public void bind(Snap snap) {
this.snap = snap;
if ((snap.getGravity() == Gravity.START || snap.getGravity() == Gravity.END)
&& !snap.getPadding()) {
scrollButton.setVisibility(View.VISIBLE);
} else {
scrollButton.setVisibility(View.INVISIBLE);
}
snapTextView.setText(snap.getText());
}

@Override
public void onClick(View v) {
RecyclerView.OnFlingListener listener = recyclerView.getOnFlingListener();
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (listener instanceof GravitySnapHelper
&& layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager lm = (LinearLayoutManager) layoutManager;
int firstVisiblePosition = snap.getGravity() == Gravity.START ?
lm.findFirstCompletelyVisibleItemPosition()
: lm.findLastCompletelyVisibleItemPosition();
if (firstVisiblePosition != RecyclerView.NO_POSITION) {
((GravitySnapHelper) listener).smoothScrollToPosition(
firstVisiblePosition + 1);
}
}
}
}
}

32 changes: 26 additions & 6 deletions app/src/main/res/layout/adapter_snap.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="wrap_content">

<TextView
android:id="@+id/snapTextView"
Expand All @@ -14,13 +14,33 @@
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="Snap center"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/scrollButton"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="Scroll +1"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintBottom_toBottomOf="@id/snapTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/snapTextView" />


<com.github.rubensousa.gravitysnaphelper.OrientationAwareRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:clipToPadding="false"
android:padding="4dp" />
</LinearLayout>
android:padding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/scrollButton" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ public void attachToRecyclerView(@Nullable RecyclerView recyclerView) {
}
}

public void smoothScrollToPosition(int position) {
scrollTo(position, true);
}

public void scrollToPosition(int position) {
scrollTo(position, false);
}

private void scrollTo(int position, boolean smooth) {
if (recyclerView.getLayoutManager() != null) {
RecyclerView.ViewHolder viewHolder
= recyclerView.findViewHolderForAdapterPosition(position);
if (viewHolder != null) {
int[] distances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
viewHolder.itemView);
if (smooth) {
recyclerView.smoothScrollBy(distances[0], distances[1]);
} else {
recyclerView.scrollBy(distances[0], distances[1]);
}
} else {
if (smooth) {
recyclerView.smoothScrollToPosition(position);
} else {
recyclerView.scrollToPosition(position);
}
}
}
}

@NonNull
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
@NonNull View targetView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,13 @@ public View findSnapView(RecyclerView.LayoutManager layoutManager) {
public void enableLastItemSnap(boolean snap) {
delegate.enableLastItemSnap(snap);
}

public void smoothScrollToPosition(int position) {
delegate.smoothScrollToPosition(position);
}

public void scrollToPosition(int position) {
delegate.scrollToPosition(position);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

package com.github.rubensousa.gravitysnaphelper;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;


public class GravitySnapHelper extends LinearSnapHelper {

@NonNull private final GravityDelegate delegate;
@NonNull
private final GravityDelegate delegate;

public GravitySnapHelper(int gravity) {
this(gravity, false, null);
Expand Down Expand Up @@ -69,6 +71,14 @@ public void enableLastItemSnap(boolean snap) {
delegate.enableLastItemSnap(snap);
}

public void smoothScrollToPosition(int position) {
delegate.smoothScrollToPosition(position);
}

public void scrollToPosition(int position) {
delegate.scrollToPosition(position);
}

public interface SnapListener {
void onSnap(int position);
}
Expand Down

0 comments on commit a11abc2

Please sign in to comment.