Skip to content

Commit

Permalink
trying to get rid of parcelable
Browse files Browse the repository at this point in the history
  • Loading branch information
vivchar committed Mar 22, 2018
1 parent 1ecd00f commit 7951ae3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class GithubFragment extends BaseScreenFragment {
private GridLayoutManager mLayoutManager;
private SwipeRefreshLayout mSwipeToRefresh;
private GithubPresenter mGithubPresenter;
@Nullable
private Bundle mSavedInstanceState;

@Nullable
@Override
Expand Down Expand Up @@ -123,7 +125,14 @@ public void onLoadMore(final int page, final int totalItemsCount) {
@Override
public void onViewStateRestored(@Nullable final Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mRecyclerViewAdapter.onRestoreInstanceState(savedInstanceState);
mSavedInstanceState = savedInstanceState;
}

@Override
public void onResume() {
super.onResume();
mRecyclerViewAdapter.onRestoreInstanceState(mSavedInstanceState);
mSavedInstanceState = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@
import com.github.vivchar.rendererrecyclerviewadapter.CompositeViewState;
import com.github.vivchar.rendererrecyclerviewadapter.ViewState;

import java.io.Serializable;
import java.util.HashMap;

/**
* Created by Vivchar Vitaly on 21.10.17.
*/

public class RecyclerViewState extends CompositeViewState<RecyclerViewHolder> {

@NonNull
private final HashMap<Integer, ViewState> mViewStates;
public class RecyclerViewState extends CompositeViewState<RecyclerViewHolder> implements Serializable {

public RecyclerViewState(@NonNull final CompositeViewHolder holder) {
super(holder);
mViewStates = holder.getAdapter().getStates();
}

@Override
public void restore(@NonNull final RecyclerViewHolder holder) {
super.restore(holder);
holder.getAdapter().setStates(mViewStates);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import com.github.vivchar.rendererrecyclerviewadapter.ViewState;

import java.io.Serializable;

/**
* Created by Vivchar Vitaly on 21.10.17.
*/

public class StargazerViewState implements ViewState<StargazerViewHolder> {
public class StargazerViewState implements ViewState<StargazerViewHolder>, Serializable {

private static final String TAG = StargazerViewState.class.getSimpleName();
private final int mVisibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class ViewStateFragment extends BaseScreenFragment {

private final YourDataProvider mYourDataProvider = new YourDataProvider();
private RendererRecyclerViewAdapter mRecyclerViewAdapter;
@Nullable
private Bundle mSavedInstanceState;

@Nullable
@Override
Expand Down Expand Up @@ -77,7 +79,14 @@ public int createViewStateID(@NonNull final StateViewModel model) {
@Override
public void onViewStateRestored(@Nullable final Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mRecyclerViewAdapter.onRestoreInstanceState(savedInstanceState);
mSavedInstanceState = savedInstanceState;

This comment has been minimized.

Copy link
@Zhuinden

Zhuinden Apr 10, 2018

Can't you just use the savedInstanceState bundle in onCreateView()?

This comment has been minimized.

Copy link
@vivchar

vivchar Apr 11, 2018

Author Owner

probably yes

}

@Override
public void onResume() {
super.onResume();
mRecyclerViewAdapter.onRestoreInstanceState(mSavedInstanceState);
mSavedInstanceState = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
package com.github.vivchar.rendererrecyclerviewadapter;

import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.io.Serializable;
import java.util.HashMap;

/**
* Created by Vivchar Vitaly on 20.10.17.
*
*/
public class CompositeViewState <VH extends CompositeViewHolder> implements ViewState<VH> {
public class CompositeViewState <VH extends CompositeViewHolder> implements ViewState<VH>, Serializable {

@NonNull
protected final Parcelable mLayoutManagerState;
protected HashMap<Integer, ViewState> mViewStates;
protected int mPosition;
protected int mTopOffset;
protected int mLeftOffset;

public <VH extends CompositeViewHolder> CompositeViewState(@NonNull final VH holder) {
mLayoutManagerState = holder.getRecyclerView().getLayoutManager().onSaveInstanceState();
mViewStates = holder.getAdapter().getStates();
final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
/* To get rid of Parcelable, https://stackoverflow.com/a/35287828/4894238 */
mPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
final View item = holder.getRecyclerView().getChildAt(0);
mTopOffset = (item == null) ? 0 : (item.getTop() - holder.getRecyclerView().getPaddingTop());
mLeftOffset = (item == null) ? 0 : (item.getLeft() - holder.getRecyclerView().getPaddingLeft());
Log.d("###", "save mPosition: " + mPosition + " mTopOffset: " + mTopOffset);
} else {
mPosition = 0;
mTopOffset = 0;
}
}

@Override
public void restore(@NonNull final VH holder) {
holder.getRecyclerView().getLayoutManager().onRestoreInstanceState(mLayoutManagerState);
holder.getAdapter().setStates(mViewStates);

final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager();
if (mPosition != -1 && layoutManager instanceof LinearLayoutManager) {
Log.d("###", "restore mPosition: " + mPosition + " mTopOffset: " + mTopOffset);
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(mPosition, Math.max(mTopOffset, mLeftOffset));
}
}
}

0 comments on commit 7951ae3

Please sign in to comment.