2014-12-27 23 views
0

我有一個主要的佈局與1片段。片段java類綁定了一些視圖,當我第一次開始從onActivityCreate事件正確填充所有視圖時。 當我旋轉手機,我都保留在片段中的數據,但我不能更新onActivityCreate事件或其他事件的任何意見... 是否有任何示例旋轉包含視圖的片段?旋轉後無法修改視圖片段

按照要求我添加代碼,我添加的片段

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:weightSum="10"> 
 
    <fragment 
 
     android:id="@+id/questionListFragment" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="50dp" 
 
     android:layout_weight="1" 
 
     class="com.sp1d3r.itil.itiltester.QuestionListFragment" /> 
 

 
    <fragment 
 
     android:id="@+id/testFragment" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_weight="9" 
 
     class="com.sp1d3r.itil.itiltester.Test_Fragment" /> 
 
</LinearLayout>

在這種情況下,我有兩個片段,但問題是,我無法更新後的任何Test_Fragment意見設備旋轉

這裏片段類的代碼

public class Test_Fragment extends Fragment { 
 

 
    private final static String TAG = "ITIL_TESTER"; 
 

 
    private final static int SIXTY_MINUTES = 1000 * 60 * 60; 
 
    private final static int ONE_SECOND = 1000; 
 
    private final static int MIN_FLIP_DISTANCE = 100; 
 
    private RadioGroup radioGroup; 
 
    private TextView question_TV; 
 
    private Context mContext; 
 
    private DBHelper itildb; 
 
    private TextView answer1_RB, answer2_RB, answer3_RB, answer4_RB; 
 
    private LinearLayout ll; 
 
    private ScrollView scrollViewQA; 
 
    private ImageButton next_IB, prev_IB; 
 
    private ITIL_Test test; 
 
    private int idButtonPrev; 
 
    private FileToDB fdb; 
 
    private Test_Fragment tFragment; 
 
    private int maxQuestionNo; 
 
    private SwipeGestureListener gestureListener; 
 
    private TextView timerView; 
 
    private long mills; 
 
    private boolean loadFlag = false; 
 

 
    private CountDownTimer countDownTimer; 
 

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

 

 
     mLog("Pass from onCreate"); 
 
    } 
 

 

 
    @Nullable 
 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 
    
 

 

 
     return inflater.inflate(R.layout.test_fragment, container, false); 
 
    } 
 

 
    @Override 
 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
 
     super.onViewCreated(view, savedInstanceState); 
 
    } 
 

 
    @Override 
 
    public void onActivityCreated(Bundle savedState) { 
 
     super.onActivityCreated(savedState); 
 
     mLog("Pass from onActivityCreated"); 
 

 

 
     if (!loadFlag) { 
 

 
      initLayout(); 
 
      itildb = new DBHelper(mContext); 
 

 

 
      maxQuestionNo = 40; 
 
      //Create a new test with 40 questions and 60 minutes 
 
      test = new ITIL_Test(this, maxQuestionNo, 60, itildb); 
 

 
      test.asynchLoadfromDB(); 
 

 
      test.setCurrentQuestionNo(1); 
 
      loadFlag = true; 
 

 
     } else { 
 

 
      mLog("set the question!!!"); 
 
      showQuestion(test.getCurrentQuestionNo()); 
 
      mLog("Question no." + test.getCurrentQuestionNo()); 
 
     } 
 
    
 
     mLog("FRAGMENT ID:"+String.valueOf(getId())); 
 

 
    } 
 

 

 
    @Override 
 
    public void onAttach(Activity activity) { 
 
     super.onAttach(activity); 
 
    } 
 

 
    @Override 
 
    public void onDestroy() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onDestroy()"); 
 
     super.onDestroy(); 
 
    } 
 

 
    @Override 
 
    public void onDestroyView() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onDestroyView()"); 
 
     super.onDestroyView(); 
 
    } 
 

 
    @Override 
 
    public void onDetach() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onDetach()"); 
 
     super.onDetach(); 
 
    } 
 

 
    @Override 
 
    public void onPause() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onPause()"); 
 

 
     super.onPause(); 
 
     countDownTimer.cancel(); 
 

 
    } 
 

 
    @Override 
 
    public void onResume() { 
 
     super.onResume(); 
 
    
 
     Log.i(TAG, getClass().getSimpleName() + ":onResume()"); 
 

 

 
    } 
 

 
    @Override 
 
    public void onStart() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onStart()"); 
 
    
 

 
     super.onStart(); 
 
    } 
 

 
    @Override 
 
    public void onStop() { 
 
     Log.i(TAG, getClass().getSimpleName() + ":onStop()"); 
 
     super.onStop(); 
 
    } 
 

 
    
 

 
private void showQuestion(int no) { 
 
    //set question sentences 
 
    question_TV.setText(test.getQuestionSentenceByQNo(no)); 
 

 
    //set answer sentences 
 
    answer1_RB.setText(test.getAnswerListByQNo(no).get(0).getASentence()); 
 
    answer2_RB.setText(test.getAnswerListByQNo(no).get(1).getASentence()); 
 
    answer3_RB.setText(test.getAnswerListByQNo(no).get(2).getASentence()); 
 
    answer4_RB.setText(test.getAnswerListByQNo(no).get(3).getASentence()); 
 

 
.. 
 
.... 
 

 
}

當我旋轉設備後,我可以看到日誌「設置問題!!!」和「問題編號」。正確,但視圖無法通過showQuestion(test.getCurrentQuestionNo())更新;方法,而不是在第一次運行。

+0

請添加代碼顯示在您添加片段到活動。 – stkent

回答

0

我明白在我的片段中犯的錯誤。 它在onCreateView中。當片段中有相同的視圖時,首先需要創建一個視圖,並使用對應的佈局。 因此,這是錯誤的方式:

@Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 
    
 

 

 
     return inflater.inflate(R.layout.test_fragment, container, false); 
 
    }

,這是正確的做法:

@Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 
     View v = inflater.inflate(R.layout.test_fragment, container, false); 
 

 
     // initialize here some view 
 
     return v; 
 
    }