每当我从DetailsActivity
移动到MainActivity
时,RecyclerView都会重置,并且列表总是从顶部位置显示。
完整的源代码可以在这里看到。该代码在很大程度上仍在进行中。
MainActivityFragment is as follows.
public class MainActivityFragment extends Fragment {
private ArrayList<Movie> mMovieArrayList = new ArrayList<Movie>();
private static final String PAGE = "1";
private RecyclerView mRecyclerView;
private SharedPreferences mSettings;
private SharedPreferences.Editor mEditor;
private static final String LOG = MainActivityFragment.class.getSimpleName();
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_list, container, false);
// Set column size to 2 for default and portrait
// and 3 for landscape orientations
int column = Integer.parseInt(getString(R.string.grid_portrait));
if (getResources().getConfiguration().orientation == 1) {
column = Integer.parseInt(getString(R.string.grid_portrait));
} else if (getResources().getConfiguration().orientation == 2) {
column = Integer.parseInt(getString(R.string.grid_landscape));
}
if (getActivity().findViewById(R.id.movie_detail_container) != null) {
column = Integer.parseInt("2");
}
mSettings = PreferenceManager.getDefaultSharedPreferences(getActivity());
mEditor = mSettings.edit();
mEditor.apply();
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), column));
mRecyclerView.setAdapter(new MovieAdapter(getActivity(), mMovieArrayList));
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onStart() {
super.onStart();
updateMovieList();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main_fragment, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.popularity:
mEditor.putString(getResources().getString(R.string.perf_sort),
getResources().getString(R.string.url_popularity));
mEditor.apply();
updateMovieList();
item.setChecked(true);
Log.d(LOG, "onOptionsItemSelected: popularity");
return true;
case R.id.rating:
mEditor.putString(getResources().getString(R.string.perf_sort),
getResources().getString(R.string.url_top_rated));
mEditor.apply();
updateMovieList();
item.setChecked(true);
Log.d(LOG, "onOptionsItemSelected: rating");
return true;
case R.id.favorite:
mEditor.putString(getResources().getString(R.string.perf_sort),
getResources().getString(R.string.url_favorite));
mEditor.apply();
updateMovieList();
item.setChecked(true);
Log.d(LOG, "onOptionsItemSelected: favorite");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
String sortBy = mSettings.getString(getResources().getString(R.string.perf_sort),
getResources().getString(R.string.url_popularity));
if (sortBy.equals(getResources().getString(R.string.url_popularity))) {
menu.findItem(R.id.popularity).setChecked(true);
} else if (sortBy.equals(getResources().getString(R.string.url_top_rated))) {
menu.findItem(R.id.rating).setChecked(true);
} else if (sortBy.equals(getResources().getString(R.string.url_favorite))) {
menu.findItem(R.id.favorite).setChecked(true);
}
}
private void updateMovieList() {
mMovieArrayList = new ArrayList<>();
String sortBy = mSettings.getString(getResources().getString(R.string.perf_sort),
getResources().getString(R.string.url_popularity));
if (sortBy.equals(getResources().getString(R.string.url_popularity)) ||
sortBy.equals(getResources().getString(R.string.url_top_rated))) {
try {
mMovieArrayList =
new FetchMoviesTask().execute(sortBy, PAGE).get();
} catch (ExecutionException | InterruptedException ei) {
ei.printStackTrace();
}
} else if (sortBy.equals(getResources().getString(R.string.url_favorite))) {
ContentResolver resolver = getActivity().getContentResolver();
Cursor cursor =
resolver.query(MovieContract.MovieEntry.CONTENT_URI,
null,
null,
null,
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.TITLE));
String movie_id = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.MOVIE_ID));
String poster = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.POSTER));
String backdrop = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.BACKDROP));
String overview = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.OVERVIEW));
String vote_average = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.VOTE_AVERAGE));
String release_date = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.DATE));
Movie movie = new Movie(title, release_date, poster,
vote_average, overview, backdrop, movie_id);
mMovieArrayList.add(movie);
} while (cursor.moveToNext());
}
}
if (cursor != null)
cursor.close();
}
mRecyclerView.setAdapter(new MovieAdapter(getActivity(), mMovieArrayList));
mRecyclerView.getAdapter().notifyDataSetChanged();
}
}
MainActivity如下所示。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// if (id == android.R.id.home) {
// onBackPressed();
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
我已经尝试过设置保存RecyclerView的状态,如本文所示。
我还尝试过将mArrayList
保存在onSaveInstanceState
中,并像本文中那样在onActivityCreated
或onViewCreated
中恢复它。
这两个对我都不起作用。我从DetailsActivity
回到MainActivity
,位置就不见了。
谢谢。
- 您正在调用一个方法来更新片段生命周期的onStart回调中的电影列表。这意味着当你的主要活动显示回来时,你正在重置你的列表。你不应该
- 您在更新列表方法的末尾设置了一个适配器的新实例。这会重新绘制整个列表。你应该重新思考你的生命周期。仅在onCreate或onCreateView中调用更新电影列表,并始终检查适配器opject是否为null,然后仅在回收器视图上使用setAdapter方法。若适配器不为null,只需调用notifyDataSetChanged或更新项所需的任何东西
也许这会有所帮助。尽管它每次都会一直滚动到选定的项目。