gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:09:27 30 4
gpt4 key购买 nike

当我旋转设备时,我的 Activity 实例被销毁,从而启动新的 onCreate。我不知道如何在屏幕方向后重新填充我的 RecyclerView。我尝试了一些涉及 AndroidManifest: android:configChanges="keyboardHidden|orientation" 以及 onSaveInstanceState 的解决方案,但无法使其正常工作。我已在我的问题中添加了 onSaveInstanceStateonRestoreInstanceState` 基本代码,目前它不执行任何操作。

谢谢

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. 如果您想关闭重新创建 Activity 类,configChanges 还应该包括 screenSize。所以正确的版本:

    android:configChanges="keyboardHidden|orientation|screenSize"

  2. 为了保存回收器 View 内容而不禁用重新创建,您必须将内容存储在不受 Activity 生命周期约束的某个位置,或者将其传递到 onSaveInstanceState 中的 bundle ,然后在 onRestoreInstanceState 或 onCreate 中获取它。请看一下https://developer.android.com/guide/components/activities/activity-lifecycle.html

关于java - android旋转后如何保留我的RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655967/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com