我的主要布局有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())更新。而是在第一次运行时起作用的方法。
我了解片段中的错误。它在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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句