我在使用单个片段类正确显示和运行ViewPager时遇到了一些麻烦。
所以有点背景知识。活动本身应该允许用户回答调查中的问题。每个问题都包含一个问题标题,一个输入框和一个提交按钮。我已经制作了一个布局和一个相应的片段(QuestionFragment)类来保存所有这些。
我的想法是让ViewPager拥有一堆QuestionFragment,并且用户可以刷到他们想要回答或编辑的问题。基本上每个页面都使用一个QuestionFragment,但每个页面都包含一个独特的问题。
然而,我的实现看起来并不像它的工作。活动的第一页将设置UI(第1页),但其余页面将应用默认布局xml。
注意:目前我正在尝试使用索引号设置UI。您可以忽略Question []数组以及对它的任何引用,因为此时UI不会使用它。但是我确实尝试设置字体,它只适用于第一页。你可以看看底部的截图,“土星有多少个卫星”是布局中的默认xml。我还注意到textview显示问题9,而不是1。
这是片段
public class SurveyTakerFragment extends Fragment {
private Question question;
private int index;
private TextView tv_question_title;
private EditText et_sms_response;
private Button btn_submit_response;
private SharedPreferences sharedPrefs;
private Typeface typeface;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_question, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
Activity activity = getActivity();
tv_question_title = (TextView) activity.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) activity.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) activity.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}
public void setQuestion(Question question, int index){
this.question = question;
this.index = index;
}
public void setSharedPrefs(SharedPreferences sharedPrefs){
this.sharedPrefs = sharedPrefs;
}
public void setTypeface(Typeface typeface){
this.typeface = typeface;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
这是适配器:
public class SurveyTakerAdapter extends FragmentPagerAdapter{
private Question[] questions;
private SharedPreferences sharedPrefs;
private Typeface typeface;
public SurveyTakerAdapter(FragmentManager fm, Question[] questions,
SharedPreferences sharedPrefs, Typeface typeface) {
super(fm);
this.questions = questions;
this.sharedPrefs = sharedPrefs;
this.typeface = typeface;
}
@Override
public Fragment getItem(int index) {
SurveyTakerFragment surveyTakerFragment = new SurveyTakerFragment();
surveyTakerFragment.setQuestion(questions[index], index);
surveyTakerFragment.setSharedPrefs(sharedPrefs);
surveyTakerFragment.setTypeface(typeface);
return surveyTakerFragment;
}
@Override
public int getCount() {
return questions.length;
}
@Override
public CharSequence getPageTitle(int position) {
return "Question: " + (position + 1);
}
}
寻呼机活动
public class SurveyTaker extends FragmentActivity{
private final String APP_DATA = "appData";
private SurveyTakerAdapter surveyTakerAdapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
Question[] questions = new Question[10];
FragmentManager fragmentManager = getSupportFragmentManager();
SharedPreferences sharedPrefs = getSharedPreferences(APP_DATA, MODE_PRIVATE);
Typeface robot = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
surveyTakerAdapter = new SurveyTakerAdapter(fragmentManager, questions, sharedPrefs, robot);
viewPager = (ViewPager) findViewById(R.id.vp_survey_taker);
viewPager.setOffscreenPageLimit(10);
viewPager.setAdapter(surveyTakerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
我假设情况就是这样,因为我使用单个片段来填充viewpager中的所有页面,但我不知道怎么回事。我的设计不好吗?或者有什么东西我不见了?
任何帮助将不胜感激! 谢谢!
答案 0 :(得分:3)
看起来R.id.tv_question_title
,R.id.tv_question_title
和R.id.tv_question_title
视图是R.layout.fragment_question
布局的一部分。您应该通过SurveyTakerFragment
实例使用getView()
,而不是通过Activity
通过getActivity()
引用这些视图。您还在onActivityCreated()
和setup()
重复工作。
将onActivityCreated()
和setup()
实施替换为:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
View view = getView();
tv_question_title = (TextView) view.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) view.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) view.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}