因此,这里的目标是在活动中按“后退”按钮时,在recyclerview中保留切换按钮的状态。
目前这是一个简单的设置:
MainActivity(包含recyclerview)>购物车
这是我尝试过的:
在MainActivity文件中(不是适配器):
[...]
@Override
protected void onSaveInstanceState(Bundle state)
{
super.onSaveInstanceState(state);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// save RecyclerView state
mBundleRecyclerViewState = new Bundle();
Parcelable listState = recyclerView.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
}
@Override
protected void onResume()
{
super.onResume();
Toast toast = Toast.makeText(this,
"OnResume Triggered",
Toast.LENGTH_SHORT);
toast.show();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// restore RecyclerView state
if (mBundleRecyclerViewState != null) {
Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(listState);
}
}
我注意到的奇怪的事情是: -在应用启动时触发onResume -按下时会触发onCreate和onResume。我忽略这是否是正常行为。 -切换按钮的状态从不保存。
整个mainactivity文件:
package com.example.notes;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import android.os.Parcelable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import static android.content.Intent.EXTRA_TEXT;
public class MainActivity extends AppCompatActivity {
public static final String ITEM_EXTRA = "com.example.application.example.EXTRA_TEXT";
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("Notebook");
private final String KEY_RECYCLER_STATE = "recycler_state";
private static Bundle mBundleRecyclerViewState;
private NoteAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteOpenHelper DatabaseHelper = new DBHelper(this);
try {
SQLiteDatabase db = DatabaseHelper.getReadableDatabase();
Toast toast = Toast.makeText(this,
"Database Exists !",
Toast.LENGTH_SHORT);
toast.show();
db.close();
} catch(SQLiteException e) {
Toast toast = Toast.makeText(this,
"Database unavailable",
Toast.LENGTH_SHORT);
toast.show();
}
setUpRecyclerView();
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button_add_note);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCart();
}
});
}
private void setUpRecyclerView() {
Query query = notebookRef.orderBy("id", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(query, Note.class)
.build();
adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(adapter);
}
public void openCart() {
Intent intent = new Intent(this, Cart.class);
intent.putExtra(ITEM_EXTRA, NoteAdapter.getTrolly().toString());
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
protected void onSaveInstanceState(Bundle state)
{
super.onSaveInstanceState(state);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// save RecyclerView state
mBundleRecyclerViewState = new Bundle();
Parcelable listState = recyclerView.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
}
@Override
protected void onResume()
{
super.onResume();
Toast toast = Toast.makeText(this,
"OnResume Triggered",
Toast.LENGTH_SHORT);
toast.show();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// restore RecyclerView state
if (mBundleRecyclerViewState != null) {
Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(listState);
}
}
}
请告知我是否需要其他代码,谢谢