2016-07-07 79 views
1

後疊加,然後再返回的RecyclerView重繪,並覆蓋舊的(你可以看到它的cardviews之間):RecyclerView被改變屏幕方向

enter image description here

問題:如何禁止該行爲,以便Android始終重用在開始時創建的RecyclerView?

片段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> 
+0

你在使用片段嗎? –

+0

@ 4k3R:是的,我正在使用包含RecyclerView的片段。 RecyclerView被放置在machinelistcontainer – jublikon

+1

那麼問題出在哪裏,你的片段再次膨脹超過前一​​個片段,我會建議做一些檢查,看看片段是否已經膨脹,如果是的話不要膨脹它。 –

回答

4

在你活動的onCreate()方法,檢查活動是新開工或正在被系統重新創建。只有當它不被重新創建時,纔會將它添加到它。

@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(); 
    } 
} 
+0

感謝Rohan !! –

0

在AndroidManifest.xml中添加了android:configChanges ATTRS

<activity android:name=".MyActivity" 
      android:configChanges="orientation" 
      android:label="@string/app_name"> 

,然後你可以處理活動的Java代碼中onConfigurationChanged()的更改,如

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
} 

看到它在android:configChangesHandling Runtime Changes