gpt4 book ai didi

android - 在旋转时保存 DialogFragment 的变量

转载 作者:行者123 更新时间:2023-11-29 20:05:22 25 4
gpt4 key购买 nike

我有一个 DialogFragment,它包含一个 RecyclerView。
为了填充 recyclerview,我想给它一个 HashSet,并在 RecyclerView 中填充项目。

问题是,由于没有 onSaveInstanceState -> onRestoreInstanceState,我无法定期保存它。

我看过关于 setRetainInstance(true) 的帖子,但它不会保存我的 RecyclerView 状态。

这是我的代码:

package giorag.dailytimer;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Created by GioraPC on 01/03/2016.
*/
public class TeamNamesEditDialog extends DialogFragment {

private EditText personName;
private RecyclerView namesList;
private ImageButton addName;
private TeamNamesAdapter adapter;

public TeamNamesEditDialog() {
}

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

setRetainInstance(true);
}

@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);
super.onDestroyView();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_team_names, container);
personName = (EditText) view.findViewById(R.id.team_names_new_name);
namesList = (RecyclerView) view.findViewById(R.id.team_names_list);
namesList.setLayoutManager(new LinearLayoutManager(getContext()));
addName = (ImageButton) view.findViewById(R.id.team_names_add_name);

getDialog().setTitle("Update team");

adapter = new TeamNamesAdapter(null);

namesList.setAdapter(adapter);
namesList.addItemDecoration(new DividerItemDecoration(getActivity(), null));
addName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addName(personName.getText().toString());
personName.setText("");
}
});

ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(namesList);

return view;
}

class TeamNamesAdapter extends RecyclerView.Adapter<TeamNamesAdapter.ViewHolder> implements ItemTouchHelperAdapter {

private List<String> names;

public List<String> getNames() {
return names;
}

public TeamNamesAdapter(List<String> names) {
this.names = names == null ? new ArrayList<String>() : names;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_names_list_item, null);

ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = new ArrayList<>(names).get(position);
holder.name.setText(name);
}

@Override
public int getItemCount() {
return names.size();
}

public void addName(String name) {
if(names.contains(name) || name.isEmpty())
return;

names.add(name);
notifyDataSetChanged();
}

@Override
public void onItemDismiss(int position) {
names.remove(position);
notifyItemRemoved(position);
}

@Override
public void onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(names, i, i + 1);
}
}
else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(names, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
}

class ViewHolder extends RecyclerView.ViewHolder {

CheckedTextView name;

public ViewHolder(View itemView) {
super(itemView);

name = (CheckedTextView)itemView.findViewById(R.id.name_in_list);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((CheckedTextView) v).toggle();
}
});
}
}
}
}

我已经尝试在构造函数、onCreateViewonCreate 中调用 setRetainInstance,但我仍然丢失了 RecyclerView 项目轮换。

有什么方法可以保存这些项目并在轮换时恢复它们吗?

最佳答案

我也有一个 CustomDialogFragment 并且 onSaveInstanceState 工作正常。

您的代码存在一些问题:例如,在 onCreateView 期间,您总是使用空列表创建适配器。这就是您的元素消失的原因。

因此,您必须添加更多代码以在 (onSaveInstanceState) 期间保存名称并在 onCreateonCreateView 期间恢复它们。

类似于:

public class TeamNamesEditDialog extends DialogFragment {
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList("oldNames", (ArrayList<String>) adapter.getNames());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(savedInstanceState != null) {
ArrayList<String> previousNameList = savedInstanceState.getStringArrayList("oldNames");
adapter = new TeamNamesAdapter(previousNameList);
} else {
adapter = new TeamNamesAdapter(null);
}
...
return view;
}
}

关于android - 在旋转时保存 DialogFragment 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755712/

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