旋转后恢复RecyclerView状态

时间:2017-10-20 07:10:37

标签: android android-recyclerview

我有RecyclerView的用户列表。点击特定行后我显示对话框,用户可以更改单元格背景颜色。但是,当我旋转移动它回到白色背景时,一切正常。如何保存recyclelerView状态并在旋转后恢复?我尝试使用saveInstanceState但无法达到这种效果。

适配器中的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();
        }

这是我的自定义Dialog类:

@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>

请帮忙吗? :)

1 个答案:

答案 0 :(得分:2)

您只需添加此行

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

到具有RecyclerView的活动。