如何在android旋转后保留我的RecyclerView?

时间:2017-10-09 22:34:46

标签: java android android-recyclerview orientation onsaveinstancestate

当我旋转设备时,我的活动实例将被销毁,从而启动一个新的onCreate。我无法弄清楚如何在屏幕方向后重新填充我的RecyclerView。我已经尝试了一些涉及AndroidManifest的解决方案:android:configChanges="keyboardHidden|orientation"以及onSaveInstanceState, but could not get it to work. I have added onSaveInstanceState and onRestoreInstanceState`基本代码到我的问题,目前没有做任何事情。

由于

MainActivity.java

public class MainActivity extends AppCompatActivity implements RepositoryAdapter.OnItemClickListener {

    private static final String LOG_TAG = "MainActivity";

    @BindView(R.id.fab_search_github_user)
    FloatingActionButton fabSearchGitHubUser;

    @BindView(R.id.et_search_user)
    EditText etSearchUser;

    @BindView(R.id.btn_search_user)
    Button btnSearchUser;

    @BindView(R.id.github_user)
    TextView tvOwner;

    @BindView(R.id.github_repository_recyclerview)
    RecyclerView mRecyclerView;

    private RepositoryAdapter mAdapter;

    private Api mApi;

    private Owner mOwner;

    boolean isSearchToggled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ButterKnife.bind(this);

        mApi = ApiUtils.getApi();

        // RecyclerView
        RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setHasFixedSize(true);

        // Floating action button to toggle user search field.
        FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab_search_github_user);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleSearch();
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @OnClick(R.id.btn_search_user)
    public void onSearchBtnClicked(View view) {
        if (!TextUtils.isEmpty(etSearchUser.getText())) {
            String searchedUser = etSearchUser.getText().toString();

            // Search will be done offline when no internet, otherwise online.
            if (!isNetworkAvailable()) {
                Toast.makeText(this, "No internet, searching offline", Toast.LENGTH_SHORT).show();
                mOwner = Owner.getByUsername(searchedUser);
                if (mOwner != null) {
                    searchByUsername(searchedUser);
                    tvOwner.setText(searchedUser);
                } else {
                    if (mAdapter != null) {
                        mAdapter.clearRecyclerView();
                    }
                    tvOwner.setText(getString(R.string.user_not_found, searchedUser));
                }
            } else {
                loadRepository(searchedUser);
                tvOwner.setText(searchedUser);
                Toast.makeText(this, "Searching online", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.i(LOG_TAG, "Search field is empty");
        }

        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

        toggleSearch();
    }

    /**
     * Search local user by username.
     *
     * @param username The local user that is searched for.
     */
    public void searchByUsername(String username) {
        mAdapter = new RepositoryAdapter(Repository.getList(username), MainActivity.this);
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * Search online on GitHub to retrieve all user repositories.
     *
     * @param username The GitHub user that is searched for.
     */
    public void loadRepository(String username) {
        mApi.listRepository(username).enqueue(new Callback<List<Repository>>() {
            @Override
            public void onResponse(Call<List<Repository>> call, Response<List<Repository>> response) {
                mAdapter = new RepositoryAdapter(response.body(), MainActivity.this);
                mRecyclerView.setAdapter(mAdapter);
            }

            @Override
            public void onFailure(Call<List<Repository>> call, Throwable t) {
                Log.e("MainActivity", "error loading from API");
            }
        });
    }

    @Override
    public void onItemClick(Repository repository) {
        if (isNetworkAvailable()) {
            mOwner = Owner.getByUsername(repository.owner.getLogin());
            Repository mRepository = Repository.getByRepositoryName(repository.getName());

            if (mOwner != null) {
                if (mRepository != null) {
                    // Repository is already in Database.
                } else {
                    // Selected repository is added to Database
                    mRepository = new Repository(mOwner);
                    mRepository.setName(repository.getName());
                    mRepository.setLogin(repository.owner.getLogin());
                    mRepository.setDescription(repository.getDescription());
                    mRepository.setStargazersCount(repository.getStargazersCount());
                    mRepository.setForksCount(repository.getForksCount());
                    mRepository.save();
                }
            } else {
                // selected repository and its owner are added to Database
                mOwner = new Owner();
                mOwner.setLogin(repository.owner.getLogin());
                mOwner.setAvatarUrl(repository.owner.getAvatarUrl());
                mOwner.setReposUrl(repository.owner.getReposUrl());
                mOwner.save();

                mRepository = new Repository(mOwner);
                mRepository.setName(repository.getName());
                mRepository.setLogin(repository.owner.getLogin());
                mRepository.setDescription(repository.getDescription());
                mRepository.setStargazersCount(repository.getStargazersCount());
                mRepository.setForksCount(repository.getForksCount());
                mRepository.save();
            }
        }

        // Send intent with repository name to repository detail activity.
        Intent intent = new Intent(MainActivity.this, RepositoryDetailActivity.class);
        intent.putExtra(INTENT_KEY_REPOSITORY, repository.getName());
        startActivity(intent);
    }

    /**
     * Toggle search field according to its state.
     */
    private void toggleSearch() {
        if (!isSearchToggled) {
            etSearchUser.setVisibility(View.VISIBLE);
            btnSearchUser.setVisibility(View.VISIBLE);
            isSearchToggled = true;
        } else {
            etSearchUser.setVisibility(View.GONE);
            btnSearchUser.setVisibility(View.GONE);
            isSearchToggled = false;
        }
    }

    /**
     * Checks if network is available on the device.
     *
     * @return Return when networkInfo is not null and networkInfo is connected.
     */
    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 如果要关闭重新创建活动类,configChanges也应该包含screenSize。所以正确的版本:

    机器人:configChanges =&#34; keyboardHidden |取向|屏幕尺寸&#34;

  2. 为了在不禁用重新创建的情况下保存回收站视图内容,您必须将内容存储在未绑定到活动生命周期的某个位置,或者将其传递给onSaveInstanceState中的bundle并稍后在onRestoreInstanceState或onCreate中获取。请查看https://developer.android.com/guide/components/activities/activity-lifecycle.html