Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
 - Changed Add/Remove OnSelectionChangedListener to setOnSelectionChangedListener
 - Fixed minor bug where duplicate updates were showing up in the listener
 - Update README
  • Loading branch information
whyisitworking committed Jan 16, 2019
1 parent 02c56fe commit f492916
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ The item selected, in the middle underlying the query `EditText`, can be found t
int position = ReelSearchView.getSelection()
```

#### Add/Remove OnSelectionChangedListener(s)
#### Set OnSelectionChangedListener

```java
ReelSearchView.addOnSelectionChangedListener(listener);
ReelSearchView.removeOnSelectionChangedListener(listener);
ReelSearchView.setOnSelectionChangedListener(listener);
```

#### Child transformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mBinding.txtQuery.setFilters(new InputFilter[]{
(source, start, end, dest, dstart, dend) -> source.toString().toLowerCase().trim()
});
mBinding.reelSearch.addOnSelectionChangedListener((prevSelection, newSelection) -> {
mBinding.reelSearch.setOnSelectionChangedListener((prevSelection, newSelection) -> {
Log.e("Selection", "Changed to " + newSelection + " from " + prevSelection);
});
}
Expand Down
59 changes: 27 additions & 32 deletions library/src/main/java/com/suhel/library/CenteredLayoutManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
import android.view.View;
import android.view.ViewGroup;

import java.util.HashSet;
import java.util.Set;

/**
* A custom {@link android.support.v7.widget.RecyclerView.LayoutManager} which
* applies top and bottom offsets to achieve a reel effect
Expand All @@ -45,7 +42,7 @@ public class CenteredLayoutManager extends RecyclerView.LayoutManager {
* Stores the references of {@link OnSelectionChangedListener} delegates
* to inform about change in selection
*/
private final Set<OnSelectionChangedListener> mOnSelectionChangedListeners;
private OnSelectionChangedListener mOnSelectionChangedListener;

/**
* Stores the absolute scroll value
Expand All @@ -58,6 +55,11 @@ public class CenteredLayoutManager extends RecyclerView.LayoutManager {
*/
private int mMaxScrollY;

/**
* Stores the number of total items in the adapter
*/
private int mChildCount;

/**
* Stores the height of each child assuming
* the children are homogeneous
Expand Down Expand Up @@ -95,7 +97,6 @@ public class CenteredLayoutManager extends RecyclerView.LayoutManager {
* Constructor to initialize required members
*/
public CenteredLayoutManager() {
mOnSelectionChangedListeners = new HashSet<>();
mPreviousSelection = -1;
}

Expand Down Expand Up @@ -155,9 +156,11 @@ public RecyclerView.LayoutParams generateDefaultLayoutParams() {
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
detachAllViews(recycler);

if (state.getItemCount() != 0) {
mChildCount = state.getItemCount();

if (mChildCount != 0) {
calculateDimensions(recycler, state);
render(recycler, state);
render(recycler);
recycle(recycler);
}
}
Expand All @@ -169,15 +172,17 @@ public boolean canScrollVertically() {

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
if(state.getItemCount() < 2) {
mChildCount = state.getItemCount();

if (mChildCount < 2) {
return 0;
}

final int lastScrollY = mScrollY;
mScrollY = Math.min(Math.max(mScrollY + dy, 0), mMaxScrollY);

detachAllViews(recycler);
render(recycler, state);
render(recycler);
recycle(recycler);

return mScrollY - lastScrollY;
Expand All @@ -195,8 +200,9 @@ public void onScrollStateChanged(int state) {

if (newSelection != mPreviousSelection) {
// Inform all listeners about the event
for (final OnSelectionChangedListener listener : mOnSelectionChangedListeners) {
listener.onSelectionChanged(mPreviousSelection, newSelection);

if (mOnSelectionChangedListener != null) {
mOnSelectionChangedListener.onSelectionChanged(mPreviousSelection, newSelection);
}

// For the next time
Expand All @@ -210,22 +216,12 @@ public void onScrollStateChanged(int state) {
}

/**
* Adds a {@link OnSelectionChangedListener} to the set of listeners.
* This method ensures duplicates are not added
* Sets the {@link OnSelectionChangedListener}
*
* @param listener The {@link OnSelectionChangedListener} to be added
* @param listener The {@link OnSelectionChangedListener} to be set
*/
public void addOnSelectionChangedListener(@NonNull OnSelectionChangedListener listener) {
mOnSelectionChangedListeners.add(listener);
}

/**
* Removes a {@link OnSelectionChangedListener} from the set of listeners
*
* @param listener The {@link OnSelectionChangedListener} to be removed
*/
public void removeOnSelectionChangedListener(@NonNull OnSelectionChangedListener listener) {
mOnSelectionChangedListeners.remove(listener);
public void setOnSelectionChangedListener(@Nullable OnSelectionChangedListener listener) {
mOnSelectionChangedListener = listener;
}

/**
Expand All @@ -243,24 +239,22 @@ public int getSelection() {
*
* @param recycler The {@link android.support.v7.widget.RecyclerView.Recycler}
* passed for getting inflated and data bound children
* @param state The {@link android.support.v7.widget.RecyclerView.State}
* passed for getting item count
*/
private void render(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (state.getItemCount() == 0) {
private void render(RecyclerView.Recycler recycler) {
if (mChildCount == 0) {
return;
}

final int firstIndex = (mScrollY >= mTopOffset) ?
Math.min((mScrollY - mTopOffset) / mChildHeight, state.getItemCount() - 1) :
Math.min((mScrollY - mTopOffset) / mChildHeight, mChildCount - 1) :
0;

final int firstTop = (mScrollY >= mTopOffset) ?
-((mScrollY - mTopOffset) % mChildHeight) :
mTopOffset - mScrollY;

for (int i = firstIndex, top = firstTop, bottom;
i < state.getItemCount() && top < getParentBottom();
i < mChildCount && top < getParentBottom();
i++, top = bottom) {

View v = recycler.getViewForPosition(i);
Expand Down Expand Up @@ -380,7 +374,8 @@ public interface OnSelectionChangedListener {
* Called with the new selection value once the view
* has settled at a particular position from scroll
*
* @param selection The new selection
* @param previousSelection The previous selection
* @param newSelection The new selection
*/
void onSelectionChanged(int previousSelection, int newSelection);

Expand Down
20 changes: 5 additions & 15 deletions library/src/main/java/com/suhel/library/ReelSearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
Expand Down Expand Up @@ -131,24 +132,13 @@ public CenteredLayoutManager getLayoutManager() {
}

/**
* Adds a {@link CenteredLayoutManager.OnSelectionChangedListener} to the set of listeners
* Sets the {@link CenteredLayoutManager.OnSelectionChangedListener}
* of the associated {@link CenteredLayoutManager}.
* This method ensures duplicates are not added
*
* @param listener The {@link CenteredLayoutManager.OnSelectionChangedListener} to be added
* @param listener The {@link CenteredLayoutManager.OnSelectionChangedListener} to be set
*/
public void addOnSelectionChangedListener(@NonNull CenteredLayoutManager.OnSelectionChangedListener listener) {
getLayoutManager().addOnSelectionChangedListener(listener);
}

/**
* Removes a {@link CenteredLayoutManager.OnSelectionChangedListener} from the set of listeners
* of the associated {@link CenteredLayoutManager}
*
* @param listener The {@link CenteredLayoutManager.OnSelectionChangedListener} to be removed
*/
public void removeOnSelectionChangedListener(@NonNull CenteredLayoutManager.OnSelectionChangedListener listener) {
getLayoutManager().removeOnSelectionChangedListener(listener);
public void setOnSelectionChangedListener(@Nullable CenteredLayoutManager.OnSelectionChangedListener listener) {
getLayoutManager().setOnSelectionChangedListener(listener);
}

/**
Expand Down

0 comments on commit f492916

Please sign in to comment.