"Synchronization" of two spinners

I have two spinners that I want to "bind" to each other in a mutually exclusive sense: if you select an element in one, this element text will turn red and appear at the top, and the other will return to display the initial ("title") selection (if earlier another item was selected) and its text becomes white.

All this is done through onItemSelected listeners:

sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) { issueSpin.setSelection(0); ((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF")); ((TextView) arg1).setTextColor(Color.parseColor("#E3170D")); .... 

and vice versa for the "spinner" question. My problem is that if I switch from one counter to another and I select the top item, onItemSelectedListener is not registered, because the selected item is already selected.

I was told that this is impossible. Or rather, I was told that it is not possible for the onItemSelected listener to fire an element that is already selected. Although I understand that this is technically true, this problem seems relatively simple, and I'm sure there should be a workaround to get the desired effect.

I have a few questions regarding some that I'm thinking about:

  • Is there a way to set all the elements in the counter as unselected, while showing one of them?
  • Is it possible to use a different type of event (e.g. setOnTouchListener, setOnClickListener, etc.), presumably on the top element, in combination with onItemSelectedListener ?
  • If I use a different type of event on my own, perhaps in Views that are inflated in the counters themselves, without onItemSelectedListener ?
  • Can you help me find a better strategy than the ones mentioned in the paragraphs above?
+6
source share
3 answers

Instead of using Spinner use a Button that visually resembles an inactive Spinner . When the button is pressed, show the custom Dialog containing the ListView . You can use the setSelection method and / or position in the getView to show which item is currently selected. ListView OnItemClickListener will receive a notification that something was clicked, regardless of whether the item was selected or not.

When you find which item has been pressed, hide Dialog and report the button containing the Activity so that it can update both Buttons , if necessary.

I implemented something really similar to this with DialogFragments and DialogFragmentParent and it works great.

+1
source

Do you find something like below

 sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) { updateView("sectionSpin"); } } 

And for another

  issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) { updateView("issueSpin"); } } 

and now

 private void updateView(String spinner){ if (spinner.equals(sectionSpin)){ //update spinner per requirement } else{ //update spinner per requirement } } 

You can also consider some additional variables or parameters to track if the item is selected for the first time or not.

+1
source

So, you can make the first element in spinners invalid choices, for example, "[Select]" or "[Select heading]", forcing the listener to shoot every time.

This will have the added benefit that you know that the user has done something before the next step.

Of course, this will depend on the desired application flow.

0
source

All Articles