0

I have been trying to implement the functionality of the SwipeRefreshLayout for my Spinner, but nothing is happening. Does anybody have an idea why is this not working? The Spinner is loading the data the first time without a problem, but when I do the dragging gesture, nothing is happening. No colours, no spinning, no System.out.println, nothing :(

Here is my XML and Java code.

XML

        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="0dp"
            android:layout_height="44dp"
            android:id="@+id/swipe_main">
            <Spinner
                android:id="@+id/data_spinner"
                android:layout_width="0dp"
                android:layout_height="44dp"/>
        </android.support.v4.widget.SwipeRefreshLayout>

JAVA

    spinner = (Spinner) findViewById(R.id.data_spinner);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataArrayList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_main);
    swipe.setColorSchemeColors(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,dataArrayList);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            swipe.setRefreshing(false);
            System.out.println("Is swipe refreshing?...");

        }
    });

Thanks a lot in advance!

Marie Amida
  • 556
  • 1
  • 5
  • 14
  • Are you trying a swipe when spinner is opened(as a dropdown)?? I think it won't work. – Sujay Aug 25 '16 at 10:10
  • Thanks @sJy. Yes, I am trying a swipe when the spinner is opened. So this is not the way to do it, huh? :-/ – Marie Amida Aug 25 '16 at 10:17
  • Dear @sJy. I have implemented the answer on this http://stackoverflow.com/questions/23830594/swiperefreshlayout-can-host-only-one-direct-child setting the Spinner inside of a RelativeLayout and I can see what you are saying more clearly. The SwipeRefreshLayout is working but only in the area of the one little element selected. Do you know how to refresh the Spinner when the it is opened by any chance? – Marie Amida Aug 25 '16 at 10:25
  • Please add spinner.clearFocus() to close the spinner first in onRefresh() method of swipe , @MarieAmida. I hope this will help you. – Mahamadali Aug 25 '16 at 10:35

2 Answers2

2

SwipeRefreshLayout works when swiping on its Child Views.

In your case, when Spinner is Clicked, it opens the list as DropdownPopup which is a subclass of ListPopupWindow. It is not the child view of SwipeRefreshLayout but a popup window & hence swipe doesn't work. But it works if you swipe over the Spinner when dropdown is not opened.

Sujay
  • 3,417
  • 1
  • 23
  • 25
  • That is correct, @sJy. I am still figuring out how to perform some actions when performing draging gesture on the opened Spinner which clearly it is not through SwipeRefreshLayout. – Marie Amida Aug 25 '16 at 10:48
0

Please add below code to your onCreate method after initialization of the swipe ofcourse

swipe.post(new Runnable() {
@Override
public void run() {
    swipe.setRefreshing(true);
}
});

I hope this will help you , Feel free to comment.

Mahamadali
  • 319
  • 3
  • 11
  • Thanks @Mahamadali. I can see the little circle now, but it does not go away. And also, as sJy has pointed out, this might not even be the way to implement the swipe to update the list inside the Spinner. – Marie Amida Aug 25 '16 at 10:21
  • Actually I answered on the bases of the need that you just want that circle indicator to show @MarieAmida. – Mahamadali Aug 25 '16 at 10:25