gpt4 book ai didi

android - RecyclerView 在更改屏幕方向后被覆盖

转载 作者:行者123 更新时间:2023-11-29 15:42:31 26 4
gpt4 key购买 nike

当我将屏幕方向从垂直更改为水平然后再次返回时,RecyclerView 被重绘并覆盖旧的(您可以在卡片 View 之间看到它):

enter image description here

问题:如何抑制该行为,以便 Android 始终重用在开始时创建的 RecyclerView?

fragment XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:lib="http://schemas.android.com/tools"
android:id="@+id/maschine_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/maschine_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

主要 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/maschineCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

<include
android:id="@+id/toolbar_main"
layout="@layout/toolbar"></include>

</android.support.design.widget.AppBarLayout>

<FrameLayout
android:id="@+id/machinelistcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_cloud_sync_maschinen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:backgroundTint="@color/colorPrimary"
android:src="@drawable/ic_cloud_sync_white_48dp"
app:borderWidth="0dp"
app:elevation="@dimen/fab_elevation"
app:rippleColor="#00ffff"/>

</android.support.design.widget.CoordinatorLayout>

最佳答案

在您的 Activity 的 onCreate() 方法中,检查 Activity 是新启动的还是正在被系统重新创建。仅在不重新创建 fragment 时才将 fragment 添加到其中。

@Override
protected void onCreate(Bundle savedInstanceState) {

.....

// If savedInstanceState is null, it means that the activity is freshly created
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.machinelistcontainer, new MyFragment())
.commit();
}
}

关于android - RecyclerView 在更改屏幕方向后被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com