i am using SwipeRefreshLayout
in my below layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/homePageBackground"
android:orientation="vertical" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout_listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="@+id/announcementHomefragment"
android:name="in.test.app.AnnouncementFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/homePageBackground" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@color/homePageBackground" >
<TextView
android:id="@+id/newsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/new_list"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textStyle="bold" />
<fragment
android:id="@+id/newshomefragment"
android:name="in.test.app.NewsFragment"
android:layout_width="wrap_content"
android:layout_height="190dp"
android:layout_below="@id/newsTitle"
android:layout_marginTop="-15dp" />
<TextView
android:id="@+id/productTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/newshomefragment"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/product_in_home"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textStyle="bold" />
<fragment
android:id="@+id/proCategoryhomefragment"
android:name="in.test.app.CategoryFragment"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_below="@id/productTitle"
android:layout_marginTop="-15dp" />
<TextView
android:id="@+id/trainingTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/proCategoryhomefragment"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="@string/trainings_in_home"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textStyle="bold" />
<fragment
android:id="@+id/trainingfragment"
android:name="in.test.app.TrainingFragment"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_below="@id/trainingTitle"
android:layout_marginBottom="10dp"
android:layout_marginTop="-15dp" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
When I pull down my SwipeRefreshLayout
it is working, but as you can see in the above code I have a scroll view inside that. So when I am pulling down my scroll view, it goes down and half the images are not showing because it came down. When I am trying to pull up again my scroll view is not going up. Instead, SwipeRefreshLayout
is getting call. What should i do?
Please help me out.
As others have already stated, if you don't have your scrollable view (ie listview) as the direct child of the SwipeRefreshLayout, the stock canChildScrollUp will not work.
Has to do with the simple logic SwipeRefreshLayout uses in checking the ability of the child view to scroll.
I was using a ListView inside an ActionbarActivity, and wanted to include an empty view whenever my listview was empty. This caused problems, since the SwipeRefreshLayout class can only have a single child. Note it also checks this child's ability to scrollUp to determine if a pull down causes the child to scrollUp, or if it causes the childs content to refresh.
So if you want to use the same logic as SwipeRefreshLayout, just extend the class, and create a method to allow you to pass in the handle to your scrollable view. Note the stock implementation uses canScrollVertically() which does exactly what we want, but only appears in SDK >= 14.
Also don't forget to include the constructor that contains the param "AttributeSet", when you extend the class, otherwise you will have problems using the class in your layout files.
So, in the onCreate method of your Activity (in my case it was an ActionBarActivity) that includes the list view, just call setMyScrollableView passing in your ListView or whatever view you use that scrolls.
Easier solution is to use
onScrollListener
and check if user can see firstElement.Where method isViewAtTop() is some other method, that checks this View is scrolled to the top
Just create a class which extends
SwipeRefreshLayout
and override the methodcanChildScrollUp()
. Return true when you want scroll down for your control.For example for scrollview you may try this,
Ok I have got it working. If the
SwipeRefreshLayout
is the root of the layout and theScrollView
resides deep into the hierarchy (I had put theScrollView
inside aRelativeLayout
) and not the direct child of theSwipeRefreshLayout
, it won’t detect a swipe up on theScrollView
properly.You should create a custom class that extends
SwipeRefreshLayout
and overridecanChildScrollUp()
method inSwipRefreshLayout
Here is a example :
The solution from @User22791 works perfectly and, based on that, I created a library available on github that you can use (and modify) for make the usage of swipeRefreshLayout easier for developers. It's here: https://github.com/dagova/referencedSwipeRefreshLayout
Basically you just have to reference in your layout the view to be checked in the method canChildScrollUp. I hope it will be useful.
I also found the other answers didn't quite work.
Took me a while of head scratching to figure out that they are using the method
getScrollY()
which as this answer explains, is aView
method describing how far it's been scroll within a container, not a method to describe how much your Scroll container has been scrolled.If you use the same technique as in the other answers (overriding the
canChildScrollUp()
method) you can easily check if the Scrollable is at it's highest point:(As you can see, I'm using a
GridView
, but you can use aListView
too)