Disable Spinner drop-down list when focus changes

I have a dropdown counter that displays when I click on the button, it looks like this:

 <Button
    android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:spinnerMode="dropdown"
        android:visibility="invisible" />


    <ListView
        android:id="@+id/lvWall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Here is a snippet showing the popup of a popup:

      findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            spinMenu.performClick();                
        }
    });       

My spinner can display the popup popup correctly. The problem is that there is a list in my layout that receives data from a web service in the background. When the data is fully loaded, all list items will be displayed or updated, and the spinner popup will be canceled (I donโ€™t even touch anything on the screen).
I think the problem in the window changed orientation to a different view. So how can I prevent this?

Update : Here is my list after loading data from the background, it is very simple:

List<Feed> data = result; 
FeedAdapter adapter = new FeedAdapter (this, data);
ListView lvWall = (ListView)findViewById(R.id.lvWall);
lvWall.setAdapter(adapter);

:

 List<String> list = getMenus();
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinMenu.setAdapter(dataAdapter);
+4
2

, Spinner, , Spinner. , , Spinner.java, DropdownPopup.show():

public void show(int textDirection, int textAlignment) {
    ...
    super.show();
    ...

    // Make sure we hide if our anchor goes away.
    // TODO: This might be appropriate to push all the way down to PopupWindow,
    // but it may have other side effects to investigate first. (Text editing handles, etc.)
    final ViewTreeObserver vto = getViewTreeObserver();
    if (vto != null) {
        final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!Spinner.this.isVisibleToUser()) {
                    dismiss();
                } else {
                    computeContentWidth();

                    ...

? , Spinner ViewTreeObserver, , . , Spinner , , . ListView, , , , .

: Spinner , , - , . , . , - isVisibleToUser(), , , @hide, .

, , Spinner , ? , 1px? , , .

, , Spinner PopupMenu. Button, . .

+3

, . .

focuschangeListener onfocuschange, ,

yourView.setOnFocusChangeListener(testListener);

@Override
      public void onFocusChange(View arg0,
              boolean isFocused) 
      {
          if(isFocused)
           {
              //do your work here
            }
           else
           {
           }
       }

.

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" android:focusableInTouchMode="true"
    android:layout_width="0px" android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
     to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>
+1

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


All Articles