How to save the state and value of a button click between views in a view?

Now I have an adapter class that extends the PagerAdapter. Inside, I have an InstantiateItem () method.

Here I create some 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; } 

Here's the onClick action for 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); } } } }; } 

I want to save the state of the button by clicking (right now, like a radio book, and when the user clicks on it, the state of the button is pressed until they switch to another button), and buttonValue between the views in the viewpager.

Ultimately, I want to see at the end of my viewpager where I can list all the points for each view, also return the user to other views and still click on their button so that they can confirm this by swaying between the views.

I ran into two problems right now:

1) Only the last buttonValue is saved, not all the Values โ€‹โ€‹buttons for all views. How can I achieve the preservation of all points in all submissions, and not just the last point in the last submission?

2) The transition from one view to another and vice versa, the state of the button is still pressed. However, if I move to the third view from the last point, pressing the button will no longer be valid. How can i fix this?

0
source share
1 answer

I think itโ€™s time to use the fragment, although you can achieve what you want without it, but using the fragment provides many advantages, such as preserving your value when destroying your activity (rotation) or using lifecycle callback or reuse methods your future projects. anyway:

1) Only the last buttonValue is saved, not all the Values โ€‹โ€‹buttons for all looks. How can I achieve the preservation of all points in all and not just the last point in the last submission?

save the state of your button in any case, for example, in a sharedpreference, database or user object list as a member of your activity or adapter, as shown below:

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

and use List<MyToggleButton> to save them.

2) The transition from one view to another and vice versa, the state of the button is still pressed. However, if I move to the third view from the last point, the button click is no longer valid. How can i fix this?

you can use List<MyToggleButton> in instantiateItem to determine which button should have this state so that you can set it correctly. when the user changes the state of any button, you must update the state of MyToggleButton so that someday you look at List<MyToggleButton> to find the correct one.

+1
source

Source: https://habr.com/ru/post/1203550/


All Articles