2016-03-02 59 views
1

我有一個DialogFragment,它包含一個RecyclerView。
要填充recyclerview,我想給它一個HashSet,並在RecyclerView中填充項目。保存旋轉的DialogFragment的變量

問題是,由於沒有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(); 
        } 
       }); 
      } 
     } 
    } 
} 

我試圖把呼叫setRetainInstance在構造函數中,onCreateViewonCreate,但我還是失去旋轉我RecyclerView項目。

有沒有什麼方法可以保存這些項目並在輪換時恢復它們?

回答

2

我有一個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; 
    } 
} 
+0

它總是空創建,因爲我不知道如何保存狀態呢。當我知道時,我會設置適配器。這是故意的。所以基本上你說的是,保存在'onSaveInstanceState'中的包在'onCreateView'中被恢復? –

+0

@GioraGuttsait我更新了答案。我測試過了。我可以保存舊的List名稱並在onCreateView期間恢復它們。是的:在onCreate()和onCreateView()期間,android會傳遞和onSaveInstanceState一樣的saveInstanceState – W0rmH0le

+0

非常棒的人,非常感謝。我會在幾個小時內查看它,看看它是否有效 –