我想在旋转手机时保持固定的滚动位置。这是我的代码。
public class MainActivityFragment extends Fragment {
private static final String TAG = MainActivityFragment.class.getSimpleName();
private static final String ARRAY_LIST = "list_view";
private static final String LAYOUT_STATE = "MainActivityFragment.recycler.layout";
ArrayList<Movie> movies;
RecyclerView recyclerView;
private MovieAdapter movieAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
public MainActivityFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main_activity, container, false);
if(savedInstanceState == null) {
MovieDataService movieDataService = RetrofitInstance.getService();
Call<MovieDBResponse> call = movieDataService.getPopularMovies(BuildConfig.MOVIESDB_API_KEY);
call.enqueue(new Callback<MovieDBResponse>() {
@Override
public void onResponse(Call<MovieDBResponse> call, Response<MovieDBResponse> response) {
MovieDBResponse movieDBResponse = response.body();
Log.i(TAG, "inside retrofit");
if (movieDBResponse != null) {
movies = (ArrayList<Movie>) movieDBResponse.getResults();
recyclerView = rootView.findViewById(R.id.rvMovies);
movieAdapter = new MovieAdapter(getActivity(), movies);
swipeRefreshLayout = rootView.findViewById(R.id.swipe_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
//swipeRefreshLayout.setOnRefreshListener(()-> getPopularMovies());
if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
}
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(movieAdapter);
movieAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<MovieDBResponse> call, Throwable t) {
}
});
}else{
movies = savedInstanceState.getParcelableArrayList(ARRAY_LIST);
recyclerView = rootView.findViewById(R.id.rvMovies);
movieAdapter = new MovieAdapter(getActivity(), movies);
swipeRefreshLayout = rootView.findViewById(R.id.swipe_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
//swipeRefreshLayout.setOnRefreshListener(()-> getPopularMovies());
if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
}
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(movieAdapter);
movieAdapter.notifyDataSetChanged();
}
return rootView;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(ARRAY_LIST,movies);
outState.putParcelable(LAYOUT_STATE, recyclerView.getLayoutManager().onSaveInstanceState());
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if(savedInstanceState != null)
{
Parcelable savedRecyclerLayoutState = savedInstanceState.getParcelable(LAYOUT_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(savedRecyclerLayoutState);
}
}
}
我正在使用onSaveInstanceState和onViewStateRestored方法,但是当我旋转手机时,固定的滚动位置不起作用。如何解决?
谢谢
Theo。