Android ViewPager在多个页面上使用单个片段 [英] Android ViewPager using a single Fragment on multiple pages

查看:87
本文介绍了Android ViewPager在多个页面上使用单个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用单个片段类显示ViewPager并正常运行时,我有些麻烦.

I'm having a bit of trouble having the ViewPager displaying and running properly with a single fragment class.

那么一点背景.该活动本身应该允许用户回答调查中的问题.每个问题都包含一个问题标题,一个输入框和一个提交按钮.我已经制作了一个布局和一个对应的片段(QuestionFragment)类来容纳所有这些内容.

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.

我的想法是让ViewPager容纳一堆QuestionFragment,而用户将能够轻扫他们想回答或编辑的问题.基本上每个页面都将使用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. Basically each page will be using a QuestionFragment, but each will contain a unique question.

但是我的实现看起来并不可行.活动的第一页将设置UI(第1页),但其余页面将应用默认布局xml.

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.您可以忽略Question []数组及其任何引用,因为UI目前不使用它.但是,我确实尝试设置字体,该字体仅在第一页上有效.您可以看一下底部的屏幕截图,土星有多少颗卫星"是布局中的默认xml.我还注意到textview显示的是问题9,而不是1.

Note: at the moment I'm trying to set up the UI with the index number. You can ignore the Question[] array and any reference to it as the UI doesnt use it at this time. 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. I've also noticed that the textview displays question 9, instead of 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);
}
}

Pager活动

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中的所有页面,但是我不知道该怎么做.我的设计不好吗?还是我缺少什么?

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. Is my design bad? Or is there something im missing?

任何帮助将不胜感激! 谢谢!

Any help would be greatly appreciated! Thanks!

推荐答案

看起来R.id.tv_question_titleR.id.tv_question_titleR.id.tv_question_title视图是R.layout.fragment_question布局的一部分.您应该使用getView()通过SurveyTakerFragment实例引用这些视图,而通过getActivity()通过Activity引用不是.您还在onActivityCreated()setup()中重复工作.

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. You should be referencing those views through your SurveyTakerFragment instance using getView(), not through the Activity via getActivity(). You are also duplicating effort in onActivityCreated() and 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.
        }
    });
}

这篇关于Android ViewPager在多个页面上使用单个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆