0
我有RecyclerView和用戶列表。點擊特定行後,我會顯示對話框,用戶可以更改單元格背景顏色。一切正常,但是,當我旋轉移動它回到白色背景。如何保存recyclerView的狀態並在旋轉後恢復它?我嘗試了saveInstanceState,但無法實現這種效果。旋轉後恢復RecyclerView狀態
MyViewHolder在適配器:
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有userItemClick方法:
@Override
public void userItemClick(final int pos) {
Log.e(TAG, "userItemClick: " + pos);
DetailsDialog dialog = new DetailsDialog(this,mRecyclerView,pos);
dialog.show();
}
而且這裏是我的自定義對話框類:
@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();
}
});
}
這裏是我的清單
<?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>
請幫忙嗎? :)
您不保存回收站視圖的狀態。您可以保存基礎項目的狀態。 – poss
請顯示您的AndroidManifest.xml。 –
你的答案在這裏https://stackoverflow.com/a/32283912/5281666 –