我怎么能保存在viewpager观点之间的状态,按钮preSS的价值? [英] How can I save the state and value of button press between views in a viewpager?

查看:200
本文介绍了我怎么能保存在viewpager观点之间的状态,按钮preSS的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个适配器类,它扩展PagerAdapter。里面我有我的InstantiateItem()方法。

Right now I have an Adapter class, which extends PagerAdapter. Inside I have my InstantiateItem() method.

在这里,我有实例化视图几个按钮:

Here I have instantiate several buttons on the view:

            @Override
            public Object instantiateItem(ViewGroup parent, final int position) {

                final int delPosition = position;
                //Get the inflater
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                //inflate the root layout
                View view = inflater.inflate(R.layout.collection, null);

                //Grab instance of image
                ImageView imageView = (ImageView) view.findViewById(R.id.region);

                //Sets a drawable as the content of this ImageView.
                imageView.setImageResource(regionImages.get(position));

                //scores toggle buttons
                zero = (ToggleButton) view.findViewById(R.id.number_zero);
                one = (ToggleButton) view.findViewById(R.id.number_one);
                two = (ToggleButton) view.findViewById(R.id.number_two);
                three = (ToggleButton) view.findViewById(R.id.number_three);
                four = (ToggleButton) view.findViewById(R.id.number_four);
                five = (ToggleButton) view.findViewById(R.id.number_five);
                six = (ToggleButton) view.findViewById(R.id.number_six);
                seven = (ToggleButton) view.findViewById(R.id.number_seven);
                eight = (ToggleButton) view.findViewById(R.id.number_eight);
                nine = (ToggleButton) view.findViewById(R.id.number_nine);
                ten = (ToggleButton) view.findViewById(R.id.number_ten);

                one.setOnClickListener(createClickListener(1));
                two.setOnClickListener(createClickListener(2));
                three.setOnClickListener(createClickListener(3));
                four.setOnClickListener(createClickListener(4));
                five.setOnClickListener(createClickListener(5));
                six.setOnClickListener(createClickListener(6));
                seven.setOnClickListener(createClickListener(7));
                eight.setOnClickListener(createClickListener(8));
                nine.setOnClickListener(createClickListener(9));
                ten.setOnClickListener(createClickListener(10));

                //Back Arrow Button
                final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow);

                back_button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        //it doesn't matter if you're already in the first item
                        viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                    }
                });


                //forward button 
                final ImageView forward_button = (ImageView) view.findViewById(R.id.forward_nav_arrow);

                forward_button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        //it doesn't matter if you're already in the last item
                        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                    }
                });

                parent.addView(view, 0);
                return view;
            }

下面是为buttosn onClick的动作:

Here's the onClick action for the buttosn:

//onClickListener method that returns an interface
    private View.OnClickListener createClickListener(final int value) {
        return new View.OnClickListener()  {

            @Override
            public void onClick(View view) {

                buttonValue = value;

                ToggleButton clickedButton = (ToggleButton) view;
                RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();

                for (int i = 0; i < radioGroup.getChildCount(); ++i) {
                    View nextChild = radioGroup.getChildAt(i);
                    if (!(nextChild instanceof ToggleButton)) {
                        continue;
                    }
                    if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
                        ToggleButton tb2 = (ToggleButton) nextChild;
                        tb2.setChecked(false);
                    }
                }
            }

        };
    }

我希望能够保存按钮preSS的状态(现在它就像一个单选按钮,当它在用户点击该按钮状态是pressed,直到他们移动到另一个按钮)而在viewpager的观点之间的buttonValue。

I want to be able to save the state of the button press (right now it's like a radiobutton and when the user clicks on it, the button state is pressed down until they move to another button) and the buttonValue between the views in the viewpager.

最后,我想在我的viewpager月底在那里我可以列出所有的得分为每个视图,以及有用户返回其他意见,仍然有自己的按钮状态pressed这样一个观点,他们可以确认它,而视图之间滑动。

Ultimately, I want a view at the end of my viewpager where I can list all the scores for each view as well as have the user go back to the other views and still have their button state pressed so that they can confirm it while swiping between views.

我现在面临两个问题:

1)只有最后buttonValue对所有的次保存,而不是所有的buttonValues​​。我怎样才能达到节省所有视图的所有分数,​​而不是仅在过去认为的最后得分?

1) Only the last buttonValue is saved and not all the buttonValues for all the views. How can I achieve saving all the scores in all the views and not just the last score in the last view?

2)从一个视图去到另一个和背部,按钮的状态依然pressed。但是,如果我去从最后一个点第三个观点,按钮状态pressed不再是真实的。我该如何解决这个问题?

2) Going from one view to another and back, the button state is still pressed. However, if I go to a third view from the last point, the button state pressed is no longer true. How can I fix this?

推荐答案

我觉得现在是时候用的片段,虽然你可以实现你想要没有它什么,但使用片段给出了很多优惠,如节省你的价值时,你的活动被破坏(旋转)或使用的生命周期回调方法或重用你的未来的项目。反正:

I think it is time to use fragment, although you can achieve what you want without it but using fragment gives a lot of benefits like saving your value when your activity is destroyed (rotation) or using the life cycle callback methods or reusing it to your future projects. anyway:

1)只有最后buttonValue被保存,而不是所有的buttonValues​​为
  所有的意见。我怎样才能达到节省所有的得分在所有的
  意见及不只是在最后一个视图的最后得分?

1) Only the last buttonValue is saved and not all the buttonValues for all the views. How can I achieve saving all the scores in all the views and not just the last score in the last view?

保存按钮的状态,反正你喜欢,例如在共享preference,数据库或作为活动的成员或适配器类似下面的自定义对象的列表:

save the state of your button in anyway you like for example in sharedpreference, database or a list of custom object as a member of your activity or adapter something like below:

class MyToggleButton{
   boolean mState; // false = unpressed, true = pressed

}

和使用列表&LT; MyToggleButton方式&gt; 来存储它们

2)展望从一个视图到另一个和背部,按钮的状态依然
  pressed。但是,如果我去第三种观点从最后一点,
  按钮状态pressed不再是真实的。我该如何解决这个问题?

2) Going from one view to another and back, the button state is still pressed. However, if I go to a third view from the last point, the button state pressed is no longer true. How can I fix this?

您可以使用列表&LT; MyToggleButton&GT; instantiateItem 来确定哪些按钮必须具有状态,这样你可以设置它正确。当用户更改任何按钮的状态,你必须更新 MyToggleButton 状态,以便当过你要看看列表&LT; MyToggleButton&GT; 找到正确的。

you can use List<MyToggleButton> at instantiateItem to determine which button must have which state so you can set it correctly. when user change the state of any button you must update MyToggleButton state so that when ever you are going to look at List<MyToggleButton> find the correct one.

这篇关于我怎么能保存在viewpager观点之间的状态,按钮preSS的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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