これは私のコードで、gridviewのスクロール状態を保存します。ただし、復元インスタンスでは取得されません。私のプロジェクトに合格するには、このステップだけが必要です。SavedInstance gridViewのスクロール位置が機能していません
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
rootView = inflater.inflate(R.layout.fragment_movies, null);
if (savedInstance != null) {
MovieTask.mProgressDialog.onRestoreInstanceState(savedInstance);
gridView.smoothScrollToPosition(savedInstance.getInt("scroll"));
}
setHasOptionsMenu(true);
return rootView;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
MovieTask.mProgressDialog.onSaveInstanceState();
savedInstanceState.putInt("scroll",gridView.getFirstVisiblePosition());
super.onSaveInstanceState(savedInstanceState);
}
ここにはAsyncTaskコードがあります。どこで私はグリッドビューを初期化しています。
@Override
protected void onPreExecute(){
mProgressDialog = new ProgressDialog(mContext);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String categoryType = preferences.getString(mContext.getString(R.string.pref_key_category) ,mContext.getString(R.string.pref_category_default_value));
if(categoryType.equals(Constants.TOP_RATED_KEY)){
Category = Constants.TOP_RATED_KEY;
mProgressDialog.setTitle("Top Rated Movies");
} else if (categoryType.equals(Constants.POPULARITY_KEY)){
Category = Constants.POPULARITY_KEY;
mProgressDialog.setTitle("Popular Movies");
}
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected String[] doInBackground(String... strings) {
try {
URL url = new URL(Constants.MOVIE_URL + Category + Constants.API_KEY);
Log.d(LOG_TAG, String.valueOf(url));
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
movieJsonStr = buffer.toString();
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
try {
return getMovieDataFromJson(movieJsonStr);
} catch (JSONException e) {
Log.d(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String[] result){
if(result != null) {
mProgressDialog.dismiss();
if(NetworkUtility.isOnline(mContext)){
List<Model> favoriteMovie = Model.listAll(Model.class);
MovieAdapter movieAdapter = new MovieAdapter(mContext, favoriteMovie, new MovieAdapter.Callback() {
@Override
public void onItemSelected(String title, String poster, String plot, double ratings, String release, long id) {
if (MainActivity.mTwoPane) {
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction()
.replace(R.id.details_fragment_container, DetailsFragment.newInstance(title, poster, plot, ratings, release, id))
.commit();
} else {
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.putExtra("title", title);
intent.putExtra("poster", poster);
intent.putExtra("plot",plot);
intent.putExtra("ratings", ratings);
intent.putExtra("release", release);
intent.putExtra("id", id);
mContext.startActivity(intent);
}
}
});
MoviesFragment.gridView = (GridView) MoviesFragment.rootView.findViewById(R.id.grid_view);
MoviesFragment.gridView.setAdapter(movieAdapter);
int index = MoviesFragment.gridView.getFirstVisiblePosition();
MoviesFragment.gridView.smoothScrollToPosition(index);
} else {
Toast.makeText(mContext,"Please Enable Internet Services",Toast.LENGTH_SHORT).show();
}
}
}
「/ gridView」の宣言/初期化はどこですか? – 0X0nosugar
@ 0X0nosugar私はPostExecuteメソッドで私のAsyncTaskのgridViewをintialisingしています。 – Se7en
問題が解決しない場合は、コードを追加することをお勧めします(AsyncTask/AsyncTaskのコードを開始するコードなど)。 – 0X0nosugar