ViewPager的PageAdapter得到复位方向改变 [英] ViewPager's PageAdapter get reset on orientation change

查看:118
本文介绍了ViewPager的PageAdapter得到复位方向改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ViewPager 的选择题考试的应用程序,它选择了随机30问题出更大的一套。我这样做是在 PageAdapter 即提供网页的 ViewPager

的问题是,当改变方向时发生时,不仅该寻呼机而且适配器被重新加载 - 我知道如何保存当前寻呼机位置,但,当适配器被复位,它也从该组选择了新的问题。什么是处理这个正确的方法是什么?

另外,侧面的问题 - 什么是注册在RadioGroups的选择的最佳方式?直接点击或以不同的方式?

我是相当新的Andr​​oid应用发展论坛。


活动:

 公共类MyActivity扩展SherlockActivity {
    动作条动作条;
    ViewPager寻呼机;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        寻呼机=新ViewPager(本);
        的setContentView(寻呼机);
        QuestionsAdapter适配器=新QuestionsAdapter(本);
        pager.setAdapter(适配器);        INT位置= 0;
        如果(savedInstanceState!= NULL){
            位置= savedInstanceState.getInt(Q_NUMBER);
        }
        pager.setCurrentItem(位置);
    }    @覆盖
    公共无效的onSaveInstanceState(捆绑savedInstanceState){
        INT位置= pager.getCurrentItem();
        savedInstanceState.putInt(Q_NUMBER位置);
    }
}

适配器:

 类QuestionsAdapter扩展PagerAdapter {
    上下文语境;
    QuestionsHelper dbQuestions;
    布尔考试;
    清单<&HashMap的LT;弦乐,对象>> examQuestions;    公共QuestionsAdapter(上下文的背景下,布尔考试){
        this.context =背景;
        this.examQuestions = GetQuestionsFromDB(30);
    }    公共对象instantiateItem(查看收集,INT位置){
        LayoutInflater吹气=(LayoutInflater)collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看视图。
        HashMap的<弦乐,对象> q;        鉴于= inflater.inflate(R.layout.exam_question_layout,NULL);
        Q = getQuestion(位置+ 1);        ((的TextView)view.findViewById(R.id.q_number))的setText(Integer.toString(位置+ 1)+。);
        ((的TextView)view.findViewById(R.id.q_question))的setText(q.get(问题)的toString());
        ((单选按钮)view.findViewById(R.id.q_answer_a))的setText(q.get(answer_a)的toString());
        ((单选按钮)view.findViewById(R.id.q_answer_b))的setText(q.get(answer_b)的toString());
        ((单选按钮)view.findViewById(R.id.q_answer_c))的setText(q.get(answer_c)的toString());        ((ViewPager)集合).addView(查看,0);
        返回视图。
    }
}


解决方案

  

我知道如何保存当前寻呼机位置,但是当适配器
  被复位,也选择从一系列新的问题。什么是
  来处理这个正确的方法是什么?


您问题应真有一个 ID 来唯一地识别它们。我不知道你如何从数据库中获取他们,但那个时候会发生,你就需要存储它们的ID。另外:


  • 您适配器应该有一个阵列(或整数)持股30值重新presenting问题当前所选批次的ID

  • 您需要实现在适配器以下逻辑:如果从previous点的多头排列是则认为它是一个干净启动,并得到一套新的30个问题。
    如果多头排列为非null那么,我们正面临着一个配置更改恢复,你需要使用这些ID来从数据库中获取,而不是一个随机的一批合适的问题

  • 活动你会保存在的onSaveInstanceState()方法适配器的多头排列( savedInstanceState.putLongArray

  • 活动的onCreate 方法,当你创建适配器,你会检查 savedInstanceState 捆绑来查看它是否非空,它有多头排列,并设置适配器上(所以它会知道得到哪些问题)


  

什么是注册在RadioGroups的选择的最佳方式?
  直接点击或以不同的方式?


您可以用上面的方法,或者创建拥有 Parcelable 自定义类像它已经被推荐给你的意见。

I'm using a ViewPager for a multiple-choice exam app, which chooses out randomly thirty questions out of a bigger set. I do this in the PageAdapter that is supplying the pages to the ViewPager.

The problem is that when an orientation change occurs, not only the pager but also the adapter gets reloaded - I know how to save the current pager position but when the adapter gets reset, it also chooses new questions from the set. What would be the proper way to handle this?

Also, side question - what would be the best way to register the choices on the RadioGroups? Directly by click or in a different way?

I'm fairly new to the Android app developement.


Activity:

public class MyActivity extends SherlockActivity {
    ActionBar actionBar;
    ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pager = new ViewPager(this);
        setContentView(pager);
        QuestionsAdapter adapter = new QuestionsAdapter(this);
        pager.setAdapter(adapter);

        int position = 0;
        if (savedInstanceState != null) {
            position = savedInstanceState.getInt("Q_NUMBER");
        }         
        pager.setCurrentItem(position);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        int position = pager.getCurrentItem();
        savedInstanceState.putInt("Q_NUMBER", position);
    }
}

Adapter:

class QuestionsAdapter extends PagerAdapter {
    Context context;
    QuestionsHelper dbQuestions;
    boolean exam;
    List<HashMap<String,Object>> examQuestions;

    public QuestionsAdapter(Context context, boolean exam) {
        this.context = context;
        this.examQuestions = GetQuestionsFromDB(30);
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        HashMap<String,Object> q;

        view = inflater.inflate(R.layout.exam_question_layout, null);
        q = getQuestion(position+1);

        ((TextView)view.findViewById(R.id.q_number)).setText(Integer.toString(position+1)+".");
        ((TextView)view.findViewById(R.id.q_question)).setText(q.get("question").toString());
        ((RadioButton)view.findViewById(R.id.q_answer_a)).setText(q.get("answer_a").toString());
        ((RadioButton)view.findViewById(R.id.q_answer_b)).setText(q.get("answer_b").toString());
        ((RadioButton)view.findViewById(R.id.q_answer_c)).setText(q.get("answer_c").toString());

        ((ViewPager)collection).addView(view, 0);
        return view;
    }
}

解决方案

I know how to save the current pager position but when the adapter gets reset, it also chooses new questions from the set. What would be the proper way to handle this?

Your questions should really have an id to uniquely identify them. I'm not sure how you get them from the database but when that would happen you would need to store their ids. Also:

  • Your adapter should have a long array(or integer) holding 30 values representing the ids of the current selected batch of questions
  • You'll need to implement the following logic in the adapter: if the long array from the previous point is null then assume it's a clean start and get a new set of 30 questions. If the long array is non null then we are facing a restore from a configuration change and you'll need to use those ids to get the proper questions from the database instead of a random batch
  • In the Activity you'll save the long array of the adapter in the onSaveInstanceState() method(savedInstanceState.putLongArray)
  • In the onCreate method of the Activity, when you create the adapter, you'll check the savedInstanceState Bundle to see if it is non-null and it has the long array and set that on the adapter(so it will know which questions to get)

what would be the best way to register the choices on the RadioGroups? Directly by click or in a different way?

You could use the above method, or create a custom class with Parcelable like it has already been recommended to you in the comments.

这篇关于ViewPager的PageAdapter得到复位方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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