2014-12-27 4 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 (view.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; 
 
    }

관련 문제