Skip to content

Commit

Permalink
Fixed orientation changed issues.
Browse files Browse the repository at this point in the history
Added abstract class implementation
Changed example project + readme

Signed-off-by: unknown <[email protected]>
  • Loading branch information
kanytu committed Aug 13, 2015
1 parent e373825 commit 120a8fb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 101 deletions.
47 changes: 20 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,26 @@ android-parallax-recycleview

```java
List<String> myContent = new ArrayList<String>(); // or another object list
ParallaxRecyclerAdapter myAdapter = new ParallaxRecyclerAdapter(myContent); // pass the list to the constructor
```

- Implement `ParallaxRecyclerAdapter.RecyclerAdapterMethods`.

```java
myAdapter.implementRecyclerAdapterMethods(new ParallaxRecyclerAdapter.RecyclerAdapterMethods() {
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
// If you're using your custom handler (as you should of course)
// you need to cast viewHolder to it.
((MyCustomViewHolder) viewHolder).textView.setText(myContent.get(i)); // your bind holder routine.
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// Here is where you inflate your row and pass it to the constructor of your ViewHolder
return new MyCustomViewHolder(LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.myRow, viewGroup, false));
}

@Override
public int getItemCount() {
// return the content of your array
return myContent.size();
}
});
ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) {
@Override
public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) {
// If you're using your custom handler (as you should of course)
// you need to cast viewHolder to it.
((MyCustomViewHolder) viewHolder).textView.setText(myContent.get(i)); // your bind holder routine.
}

@Override
public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) {
// Here is where you inflate your row and pass it to the constructor of your ViewHolder
return new MyCustomViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.myRow, viewGroup, false));
}

@Override
public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) {
// return the content of your array
return myContent.size();
}
};
```

- Now we set the parallax header. You need to pass the `RecyclerView` too to implement the scroll listeners.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import java.util.ArrayList;
import java.util.List;

/**
* Created by poliveira on 26/02/2015.
*/
public class MainActivity extends Activity {

private boolean isNormalAdapter = false;
Expand Down Expand Up @@ -56,22 +53,15 @@ private void createCardAdapter(RecyclerView recyclerView) {
for (int i = 0; i < 50; i++) {
content.add("item " + i);
}
final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<>(content);
HeaderLayoutManagerFixed layoutManagerFixed = new HeaderLayoutManagerFixed(this);
recyclerView.setLayoutManager(layoutManagerFixed);
View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
layoutManagerFixed.setHeaderIncrementFixer(header);
adapter.setShouldClipView(false);
adapter.setParallaxHeader(header, recyclerView);
adapter.setData(content);
adapter.implementRecyclerAdapterMethods(new ParallaxRecyclerAdapter.RecyclerAdapterMethods() {

final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) {
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) {
((ViewHolder) viewHolder).textView.setText(adapter.getData().get(i));
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) {
final ViewHolder holder = new ViewHolder(getLayoutInflater().inflate(R.layout.row_recyclerview_cards, viewGroup, false));
//don't set listeners on onBindViewHolder. For more info check http://androidshenanigans.blogspot.pt/2015/02/viewholder-pattern-common-mistakes.html
holder.textView.setOnClickListener(new View.OnClickListener() {
Expand All @@ -84,10 +74,18 @@ public void onClick(View v) {
}

@Override
public int getItemCount() {
public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) {
return content.size();
}
});
};

HeaderLayoutManagerFixed layoutManagerFixed = new HeaderLayoutManagerFixed(this);
recyclerView.setLayoutManager(layoutManagerFixed);
View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
layoutManagerFixed.setHeaderIncrementFixer(header);
adapter.setShouldClipView(false);
adapter.setParallaxHeader(header, recyclerView);
adapter.setData(content);
recyclerView.setAdapter(adapter);
}

Expand All @@ -96,19 +94,15 @@ private void createAdapter(RecyclerView recyclerView) {
for (int i = 0; i < 50; i++) {
content.add("item " + i);
}
final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<>(content);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
adapter.setParallaxHeader(header, recyclerView);
adapter.setData(content);
adapter.implementRecyclerAdapterMethods(new ParallaxRecyclerAdapter.RecyclerAdapterMethods() {

final ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) {
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) {
((ViewHolder) viewHolder).textView.setText(adapter.getData().get(i));
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) {
final ViewHolder holder = new ViewHolder(getLayoutInflater().inflate(R.layout.row_recyclerview, viewGroup, false));
//don't set listeners on onBindViewHolder. For more info check http://androidshenanigans.blogspot.pt/2015/02/viewholder-pattern-common-mistakes.html
holder.textView.setOnClickListener(new View.OnClickListener() {
Expand All @@ -121,10 +115,15 @@ public void onClick(View v) {
}

@Override
public int getItemCount() {
public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) {
return content.size();
}
});
};

recyclerView.setLayoutManager(new LinearLayoutManager(this));
View header = getLayoutInflater().inflate(R.layout.header, recyclerView, false);
adapter.setParallaxHeader(header, recyclerView);
adapter.setData(content);
recyclerView.setAdapter(adapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@
import android.graphics.Rect;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

import java.util.List;


/**
* Created by poliveira on 03/11/2014.
*/
public class ParallaxRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final float SCROLL_MULTIPLIER = 0.5f;
public abstract class ParallaxRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final float SCROLL_MULTIPLIER = 0.5f;

public static class VIEW_TYPES {
public static final int NORMAL = 1;
public static final int HEADER = 2;
public static final int FIRST_VIEW = 3;
}

public interface RecyclerAdapterMethods {
void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i);

RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i);
public abstract void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder,ParallaxRecyclerAdapter<T> adapter, int i);

int getItemCount();
}
public abstract RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup,ParallaxRecyclerAdapter<T> adapter, int i);

public abstract int getItemCountImpl(ParallaxRecyclerAdapter<T> adapter);

public interface OnClickEvent {
/**
Expand All @@ -57,11 +51,9 @@ public interface OnParallaxScroll {

private List<T> mData;
private CustomRelativeWrapper mHeader;
private RecyclerAdapterMethods mRecyclerAdapterMethods;
private OnClickEvent mOnClickEvent;
private OnParallaxScroll mParallaxScroll;
private RecyclerView mRecyclerView;
private int mTotalYScrolled;
private boolean mShouldClipView = true;

/**
Expand Down Expand Up @@ -102,44 +94,39 @@ public void setParallaxHeader(View header, final RecyclerView view) {
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (mHeader != null) {
mTotalYScrolled += dy;
translateHeader(mTotalYScrolled);
translateHeader(mRecyclerView.computeVerticalScrollOffset());
}
}
});
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
if (mRecyclerAdapterMethods == null)
throw new NullPointerException("You must call implementRecyclerAdapterMethods");
if (i != 0 && mHeader != null) {
mRecyclerAdapterMethods.onBindViewHolder(viewHolder, i - 1);
} else if (i != 0)
mRecyclerAdapterMethods.onBindViewHolder(viewHolder, i);
if (mOnClickEvent != null)
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnClickEvent.onClick(v, i - (mHeader == null ? 0 : 1));
}
});
onBindViewHolderImpl(viewHolder,this, i - 1);
} else if (i != 0) {
onBindViewHolderImpl(viewHolder, this, i);
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (mRecyclerAdapterMethods == null)
throw new NullPointerException("You must call implementRecyclerAdapterMethods");
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
if (i == VIEW_TYPES.HEADER && mHeader != null)
return new ViewHolder(mHeader);
if (i == VIEW_TYPES.FIRST_VIEW && mHeader != null && mRecyclerView != null) {
RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForPosition(0);
final RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForPosition(0);
if (holder != null) {
translateHeader(-holder.itemView.getTop());
mTotalYScrolled = -holder.itemView.getTop();
if (mOnClickEvent != null)
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnClickEvent.onClick(v, holder.getAdapterPosition() - (mHeader == null ? 0 : 1));
}
});
}
}
return mRecyclerAdapterMethods.onCreateViewHolder(viewGroup, i);
return onCreateViewHolderImpl(viewGroup,this, i);
}

/**
Expand Down Expand Up @@ -200,29 +187,16 @@ public void removeItem(T item) {


public int getItemCount() {
if (mRecyclerAdapterMethods == null)
throw new NullPointerException("You must call implementRecyclerAdapterMethods");
return mRecyclerAdapterMethods.getItemCount() + (mHeader == null ? 0 : 1);
return getItemCountImpl(this) + (mHeader == null ? 0 : 1);
}

@Override
public int getItemViewType(int position) {
if (mRecyclerAdapterMethods == null)
throw new NullPointerException("You must call implementRecyclerAdapterMethods");
if (position == 1)
return VIEW_TYPES.FIRST_VIEW;
return position == 0 ? VIEW_TYPES.HEADER : VIEW_TYPES.NORMAL;
}

/**
* You must call this method to set your normal adapter methods
*
* @param callbacks
*/
public void implementRecyclerAdapterMethods(RecyclerAdapterMethods callbacks) {
mRecyclerAdapterMethods = callbacks;
}

static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
Expand Down

0 comments on commit 120a8fb

Please sign in to comment.