我使用的是viewpager,但是片段有问题。 我的片段onCreateView被调用两次。 这是我的代码 适配器:
public class QuestionAdapter extends FragmentStatePagerAdapter {
/**
* @param fm
* Fragment manager
*/
public QuestionAdapter(FragmentManager fm) {
super(fm);
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentStatePagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
if (position == 4) {
return InfoFragment.getInstance();
}
return QuestionFragment.getInstance(position);
}
/*
* (non-Javadoc)
*
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return 5;
}
/*
* (non-Javadoc)
*
* @see
* android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
*/
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
我的片段代码:
public class QuestionFragment extends Fragment {
private int mPos;
@Bind(R.id.fragment_question_group_layout)
RadioGroup questionGroup;
public static QuestionFragment getInstance(int pos) {
QuestionFragment fragment = new QuestionFragment();
fragment.setPos(pos);
return fragment;
}
public void setPos(int pos) {
this.mPos = pos;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
LogUtil.e("QuestionFragment", "Pos : " + mPos);
View view = inflater.inflate(R.layout.fragment_question, container, false);
ButterKnife.bind(this, view);
switch (mPos) {
case 0 :
displayQuestion(getResources().getStringArray(R.array.question1_content));
break;
case 1 :
displayQuestion(getResources().getStringArray(R.array.question2_content));
break;
case 2 :
displayQuestion(getResources().getStringArray(R.array.question3_content));
break;
default :
displayQuestion(getResources().getStringArray(R.array.question4_content));
break;
}
return view;
}
这是log:
编辑: 我已经设置了:viewPager.setOffscreenPageLimit(4);在活动上 当我打开活动时,上面的日志得到了,没有刷卡动作。只需打开活动并获取日志
答案 0 :(得分:0)
我解决了我的问题,它是:
当我运行我的应用程序时,由于配置更改,片段将重新创建。我推了
android:configChanges="keyboardHidden|screenSize|orientation"
在清单上然后一切正常