我从这里学到了如何防止方向改变时重启
How to avoid restarting activity when orientation changes on Android
在我的清单中我确保旋转不会触发重启
android:configChanges="keyboardHidden|orientation|screenSize"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_of_items"/>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
在我的主要活动中
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView itemsView = findViewById(R.id.list_of_items);
itemsView.setLayoutManager(new LinearLayoutManager(this));
itemsView.setNestedScrollingEnabled(false);
ItemsAdapter items = new ItemsAdapter(this);
itemsView.setAdapter(items);
}
每次旋转我都会检查里面
onConfigurationChanged
项目计数、可见性状态和日志打印回收器视图可见并且包含x个项目,但不知何故在旋转时它从不显示这些项目。我错过了什么?我的ItemsAdapter是一个非常简单的适配器,里面没有什么特别的东西。