Android 循环视图在旋转时为空

Android 循环视图在旋转时为空,android,android-recyclerview,android-orientation,recyclerview-layout,Android,Android Recyclerview,Android Orientation,Recyclerview Layout,我从这里学到了如何防止方向改变时重新启动 因此我实施了它 在我的清单中,我确保旋转不会触发重启 android:configChanges="keyboardHidden|orientation|screenSize" 我的布局很简单 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"

我从这里学到了如何防止方向改变时重新启动

因此我实施了它

在我的清单中,我确保旋转不会触发重启

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>

每当发生旋转时,我都会在
onConfigurationChanged
内检查项目计数和可见性状态,并记录显示回收器视图是可见的,并且它包含x个项目,但不知何故,在旋转时它从不显示这些项目。我错过了什么?My ItemsAdapter是一个非常简单的适配器,里面没有什么花哨的东西。

通过在
onConfiguration Changed()中使用
setContentView(R.layout.activity\u main)
基本上每次旋转设备时都会创建新的视图。但是,对于在
onCreate()中找到的视图,只需设置一次适配器。从
onConfigurationChanged()
中删除此行应该可以解决您的问题


顺便说一句,您很可能需要ScrollView,RecyclerView处理自己的滚动。

通过在
OnConfiguration Changed()
中使用
setContentView(R.layout.activity\u main)
基本上每次旋转设备时都会创建新视图。但是,对于在
onCreate()中找到的视图,只需设置一次适配器。从
onConfigurationChanged()
中删除此行应该可以解决您的问题。顺便说一句,你很可能需要ScrollView,RecyclerView处理它自己的滚动。@JanStoltman如果你把它作为一个答案发布,我会接受它作为正确答案。成功了!!!
@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);
}