2017-10-20 31 views
0

J'ai RecyclerView avec la liste des utilisateurs. Après avoir cliqué sur une ligne spécifique, j'ai affiché la boîte de dialogue et l'utilisateur peut changer la couleur de fond de la cellule. Tout fonctionne, cependant, quand je fais tourner le mobile, il revient à fond blanc. Comment puis-je enregistrer l'état de recyclerView et le restaurer après la rotation? J'ai essayé avec saveInstanceState mais n'ai pas pu archiver cet effet.Restaurer l'état RecyclerView après la rotation

MyViewHolder Adaptateur:

public MyViewHolder(View view) { 
       super(view); 
       user_name = (TextView) view.findViewById(R.id.user_name); 
       user_surname = (TextView) view.findViewById(R.id.user_surname); 
       User_description = (TextView) view.findViewById(R.id.user_description); 
       user_dob = (TextView) view.findViewById(R.id.user_dob); 
       user_avatar = (AvatarImageView) view.findViewById(R.id.user_avatar); 
       mTrash = (ImageView) view.findViewById(R.id.trash_img); 


       item_ll = (LinearLayout)view.findViewById(R.id.item_layout); 
       item_ll.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View view) { 
         ((MainActivity) ctx).userItemClick(getAdapterPosition()); 

        } 
       }); 
      } 

MainActivity a méthode userItemClick:

@Override 
     public void userItemClick(final int pos) { 
      Log.e(TAG, "userItemClick: " + pos); 
      DetailsDialog dialog = new DetailsDialog(this,mRecyclerView,pos); 
      dialog.show(); 
     } 

Et voici ma classe personnalisée de dialogue:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog_details); 
    mYelloewLayout = (LinearLayout) findViewById(R.id.yellow_layout); 
    mGreenLayout = (LinearLayout) findViewById(R.id.green_layout); 
    mRedLayout = (LinearLayout) findViewById(R.id.red_layout); 
    mYelloewLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.yellow)); 
      dismiss(); 
     } 
    }); 
    mRedLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.red)); 
      dismiss(); 
     } 
    }); 
    mGreenLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.green)); 
      dismiss(); 
     } 
    }); 


} 

voici mon Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
package="com.example.bartoszszlapa.api_request"> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 



<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" 
    tools:replace="android:icon" 
    android:name="com.orm.SugarApp" 
    android:configChanges="orientation|screenSize" 
    > 
    <meta-data android:name="DATABASE" android:value="sugar_example.db" /> 
    <meta-data android:name="VERSION" android:value="2" /> 
    <meta-data android:name="QUERY_LOG" android:value="true" /> 
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" /> 
    <activity android:name=".views.MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

Toute aide, s'il vous plaît? :)

+0

vous ne sauvegardez pas l'état de la vue recycleur. Vous enregistrez l'état des éléments sous-jacents. – poss

+0

montrez votre AndroidManifest.xml s'il vous plaît. –

+2

votre réponse est ici https://stackoverflow.com/a/32283912/5281666 –

Répondre

2

vous suffit d'ajouter cette ligne

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/> 

à l'activité qui a le RecyclerView.