恢复RecyclerView滚动位置

时间:2018-07-21 13:47:17

标签: android android-recyclerview rotation

我有这个代码。它显示两个回收者视图,并希望存储其滚动位置以进行旋转。这是我的代码。

public class DetailActivity extends AppCompatActivity implements DetailViewInteface, TrailersAdapter.TrailersAdapterOnClickHandler {
public static final String IMAGE_URL = "http://image.tmdb.org/t/p/";
public static final String IMAGE_SIZE_185 = "w185";
private static final String TAG = "DetailActivity";

private static final String TRAILERS_LAYOUT = "DetailsActivity.trailer.layout";
private static final String REVIEWS_LAYOUT = "DetailsActivity.review.layout";

private Parcelable listState1;
private Parcelable listState2;


@BindView(R.id.movie_image)
ImageView movieImageTv;

String mImage;

@BindView(R.id.movie_title)
TextView movieTitleTv;

String mTitle;

@BindView(R.id.movie_rating)
TextView movieRatingTv;

double mRating;

@BindView(R.id.movie_date)
TextView movieDateTv;

String mReleaseDate;

@BindView(R.id.movie_overview)
TextView movieOverviewTv;

String mMovieOverview;

@BindView(R.id.recyclerview_trailers)
RecyclerView trailersRecyclerView;

TrailersAdapter trailersAdapter;

@BindView(R.id.recyclerview_reviews)
RecyclerView reviewsRecyclerView;

ReviewAdapter reviewAdapter;

int recyclerViewOrientation = LinearLayoutManager.VERTICAL;

private Parcelable mLayoutManagerSavedState;


DetailPresenter detailPresenter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    ButterKnife.bind(this);

    setMVP();
    setUpViews();
    detailPresenter.getMovieDetails();
    detailPresenter.getMovieTrailers();
    detailPresenter.getMovieReviews();



}

public void setMVP(){
    detailPresenter = new DetailPresenter(this,this);
}

public void setUpViews(){
    trailersRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    trailersRecyclerView.addItemDecoration(new MyDividerItemDecoration(this,recyclerViewOrientation,16));

    reviewsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    reviewsRecyclerView.addItemDecoration(new MyDividerItemDecoration(this,recyclerViewOrientation,16));
}
@Override
public void dipsplayMovieDetails(Movie movie) {
      //movieTitleTv.setText(movieTitle);
      //movieDescriptionTv.setText(movieDesc);

      mImage = movie.getPosterPath();
      Picasso.with(this).load(IMAGE_URL + IMAGE_SIZE_185 + mImage).into(movieImageTv);

      mTitle = movie.getTitle();
      movieTitleTv.setText(mTitle);

      mRating = movie.getVoteAverage();
      movieRatingTv.setText("Rating: " + mRating);

      mReleaseDate = movie.getReleaseDate();
      movieDateTv.setText("Date: " + mReleaseDate);

      mMovieOverview = movie.getOverview();
      movieOverviewTv.setText(mMovieOverview);

}

@Override
public void dipsplayMovieTrailers(TrailersResponse trailersResponse) {
    Log.d(TAG, "Trailers size: " + String.valueOf(trailersResponse.getTrailers().size()));
    trailersAdapter = new TrailersAdapter(trailersResponse.getTrailers(),DetailActivity.this,this);
    trailersRecyclerView.setAdapter(trailersAdapter);
}

@Override
public void displayMovieReviews(ReviewsResponse reviewsResponse) {

    if(reviewsResponse != null) {
        Log.d(TAG, String.valueOf("Reviews size: " + reviewsResponse.getReviews().size()));
        reviewAdapter = new ReviewAdapter(reviewsResponse.getReviews());
        reviewsRecyclerView.setAdapter(reviewAdapter);
    }else{
        Toast.makeText(DetailActivity.this,"No reviews found!",Toast.LENGTH_LONG).show();
    }
}

@Override
public void displayError(String s) {
    showToast(s);
}

@Override
public void showToast(String s) {
    Toast.makeText(DetailActivity.this,s,Toast.LENGTH_LONG).show();
}

@Override
public void onClick(String trailerKey) {
    Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + trailerKey));
    Intent webIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("http://www.youtube.com/watch?v=" + trailerKey));
    try {
        startActivity(appIntent);
    } catch (ActivityNotFoundException ex) {
        startActivity(webIntent);
    }
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable(REVIEWS_LAYOUT,reviewsRecyclerView.getLayoutManager().onSaveInstanceState());
    outState.putParcelable(TRAILERS_LAYOUT,trailersRecyclerView.getLayoutManager().onSaveInstanceState());



  }
}

我使用onSaveInstanceState方法。但是现在我需要恢复recyclerviews的滚动位置。

我正在如下使用onRestoreInstanceState,但是什么也没有发生。旋转使我到达屏幕顶部。

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    savedInstanceState.getParcelable(REVIEWS_LAYOUT);
    savedInstanceState.getParcelable(TRAILERS_LAYOUT);
}

那么我该如何解决该错误?

谢谢

Theo。

1 个答案:

答案 0 :(得分:0)

void addOnScrollListener (RecyclerView.OnScrollListener listener)

这是一个侦听器,将通知回收者视图滚动状态或位置的任何更改。

Refer to this answer for an example implementation.

For more on how to use it correctly.