我是Android開發的初學者。我試圖在LayoutManager上使用onSaveInstanceState()和onRestoreInstanceState()來保存RecyclerView的LayoutManager狀態(在經歷了很多StackOverflow問題之後),但我無法這樣做。我希望恢復RecyclerView旋轉時的滾動位置的設備。我也嘗試保存在佈局中使用的NestedScrollView的位置,但無法讓它工作。無法滾動到使用onSaveInstanceState()和onRestoreInstanceState()旋轉設備上的RecyclerView位置
片段類
public class RecipeDetailFragment extends Fragment {
private static final String BUNDLE_RECIPE_ID = "Recipe";
private static final String BUNDLE_LAYOUT_MANAGER_KEY = "layout";
public StepAdapter mStepAdapter;
public IngredientAdapter mIngredientAdapter;
private Recipe selectedRecipe;
private StepAdapter.StepAdapterOnClickHandler mClickHandler;
@BindView(R.id.rv_recipe_detail_steps)
RecyclerView mStepRecyclerView;
@BindView(R.id.rv_recipe_detail_ingredients)
RecyclerView mIngredientRecyclerView;
public RecipeDetailFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recipe_detail, container, false);
ButterKnife.bind(this, rootView);
mIngredientAdapter = new IngredientAdapter();
mIngredientRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mIngredientRecyclerView.setHasFixedSize(true);
mIngredientRecyclerView.setNestedScrollingEnabled(false);
mStepAdapter = new StepAdapter(getContext(), mClickHandler);
mStepRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mStepRecyclerView.setHasFixedSize(true);
mStepRecyclerView.setNestedScrollingEnabled(false);
if (savedInstanceState != null) {
selectedRecipe = savedInstanceState.getParcelable(BUNDLE_RECIPE_ID);
}
mIngredientAdapter.setIngredientData(selectedRecipe.getIngredients());
mStepAdapter.setStepData(selectedRecipe.getSteps());
mIngredientRecyclerView.setAdapter(mIngredientAdapter);
mStepRecyclerView.setAdapter(mStepAdapter);
return rootView;
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
Parcelable state = savedInstanceState.getParcelable(BUNDLE_LAYOUT_MANAGER_KEY);
mStepRecyclerView.getLayoutManager().onRestoreInstanceState(state);
}
}
public void setRecipe(Recipe recipe) {
selectedRecipe = recipe;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mClickHandler = (StepAdapter.StepAdapterOnClickHandler) context;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(BUNDLE_RECIPE_ID, selectedRecipe);
outState.putParcelable(BUNDLE_LAYOUT_MANAGER_KEY, mStepRecyclerView.getLayoutManager()
.onSaveInstanceState());
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recipe_detail_nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_recipe_detail_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="@+id/ingredients_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
cardView:cardCornerRadius="4dp"
cardView:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_recipe_detail_ingredients_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:text="@string/label_ingredients"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Widget.ActionMode.Title"
android:textSize="28sp" />
<android.support.v7.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rv_recipe_detail_ingredients"
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent"
android:paddingBottom="8dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="@+id/steps_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
cardView:cardCornerRadius="4dp"
cardView:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_recipe_detail_steps_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:paddingBottom="16dp"
android:text="@string/label_steps"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Widget.ActionMode.Title"
android:textSize="28sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recipe_detail_steps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:paddingBottom="16dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
P.S.這是我第一次在這裏發佈問題,所以如果我沒有正確發佈問題,請讓我知道!
上保存實例的狀態,你嘗試過'mStepRecyclerView.getAdapterPosition( )'? –
是的。不知何故工作。當我嘗試使用onCreateView()中的適配器位置檢索視圖時,它返回了一個空視圖 –