简体   繁体   English

Android ViewPager在多个页面上使用单个片段

[英]Android ViewPager using a single Fragment on multiple pages

I'm having a bit of trouble having the ViewPager displaying and running properly with a single fragment class. 我在使用单个片段类显示ViewPager并正常运行时遇到了一些麻烦。

So a bit of background. 有一点背景知识。 The Activity itself is supposed to allow users to answer questions in a survey. 活动本身应允许用户回答调查中的问题。 Each question consists of a question title, an input box, and a submit button. 每个问题由一个问题标题,一个输入框和一个提交按钮组成。 I've made a layout and a corresponding fragment (QuestionFragment) class to hold all of this. 我制作了一个布局和一个对应的片段(QuestionFragment)类来容纳所有这些内容。

My idea is to have the ViewPager hold a bunch of QuestionFragment's and the user will be able to swipe to the questions they would like to answer or edit. 我的想法是让ViewPager容纳一堆QuestionFragment,而用户将能够轻扫他们想回答或编辑的问题。 Basically each page will be using a QuestionFragment, but each will contain a unique question. 基本上每个页面都将使用QuestionFragment,但是每个页面都将包含一个唯一的问题。

However my implementation doesn't look like its working out. 但是,我的实现看起来并不可行。 The first page in the activity will have the UI set up (page 1) but the rest of the pages will have the default layout xml applied. 活动的第一页将设置UI(第1页),但其余页面将应用默认布局xml。

Note: at the moment I'm trying to set up the UI with the index number. 注意:目前,我正在尝试使用索引号设置UI。 You can ignore the Question[] array and any reference to it as the UI doesnt use it at this time. 您可以忽略Question []数组及其任何引用,因为UI目前不使用它。 However I do try to set the typeface, which only works on the first page. 但是我确实尝试设置字体,该字体仅在第一页上有效。 You can take a look at the screenshots at the bottom, "How many moons does Saturn have" is the default xml in the layout. 您可以看一下底部的屏幕截图,“土星有多少颗卫星”是布局中的默认xml。 I've also noticed that the textview displays question 9, instead of 1. 我还注意到textview显示的是问题9,而不是1。

Here is the fragment 这是片段

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);
}
}

Here is the adapter: 这是适配器:

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);
}
}

Pager Activity 传呼机活动

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

        }
    });
}
}

第1页

第2页

第3页

I'm assuming this is the case because I'm using a single fragment to populate all the pages in the viewpager, but I don't know how else to go about this. 我以为是这种情况,因为我使用一个片段来填充viewpager中的所有页面,但我不知道该怎么做。 Is my design bad? 我的设计不好吗? Or is there something im missing? 还是我缺少什么?

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

It looks like the R.id.tv_question_title , R.id.tv_question_title and R.id.tv_question_title views are part of your R.layout.fragment_question layout. 看起来R.id.tv_question_titleR.id.tv_question_titleR.id.tv_question_title视图是R.layout.fragment_question布局的一部分。 You should be referencing those views through your SurveyTakerFragment instance using getView() , not through the Activity via getActivity() . 您应该通过使用getView() SurveyTakerFragment实例而不是通过getActivity()Activity来引用这些视图。 You are also duplicating effort in onActivityCreated() and setup() . 您还将在onActivityCreated()setup()重复工作。

Replace your onActivityCreated() and setup() implementations with this: 将您的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.
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM