I implemented SwipeRefreshLayout in my app. I need to change the height which has to be scrolled down to call onRefresh() method. Which method should I use to add a custom height ?
Implementation of SwipeRefreshLayout
private SwipeRefreshLayout swipeLayout;
In oncreate
swipeLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_dark,
android.R.color.holo_green_dark,
android.R.color.holo_purple,
android.R.color.holo_orange_dark);
swipeLayout.setSoundEffectsEnabled(true);
Implementing an AsyncTask in onrefresh method
@Override
public void onRefresh()
{
// Asynctask to perform some task
}
The reversion 21 of v4 support library has provided a API to set the distance.
Check the document here.
One more thing, the rev. 21 changed the behaviour of progress bar, it's now a circle image. Have no idea how to get the old way back.
If you're using API 21 of the support library, refer to and please upvote Han He's answer here. A method exists to set the trigger distance called
setDistanceToTriggerSync
.Prior to API 21, as adneal mentioned, there are no public or internal methods to modify the trigger distance.
However, if you don't want to keep a copy of the classes to modify the constants, you can use reflection to manually set a trigger distance.
If you're using the height of the layout to determine the distance, you may want to use something like a
GlobalLayoutListener
.mDistanceToTriggerSync
In
SwipeRefreshLayout
, there is an internal variable calledmDistanceToTriggerSync
. This is used to determine at what distance to trigger the refresh.In the source code, it is set by the code below in
SwipeRefreshLayout
:The above uses the parent view height and some constants to calculate the trigger distance.
MAX_SWIPE_DISTANCE_FACTOR
(0.6) andREFRESH_TRIGGER_DISTANCE
(120) are private constants in the class that you cannot modify.You can use the above to calculate your trigger distance and use your own constants for the swipe distance factors.
GlobalLayoutListener
Setting of the
mDistanceToTriggerSync
can be done inside a global layout listener so that the height of the layout can be retrieved properly for calculating the trigger distance. CallinggetHeight
on a view inonCreate
will always return 0 because it has not been drawn yet. I got the code from here. You may or may not need to do this depending on your requirements.The setting of the variable only needs to be done once if I understand the underlying code properly as it only sets
mDistanceToTriggerSync
if it is equal to-1
. Therefore, it should be safe to do it in the global layout listener once.SwipeRefreshLayout
If you're interested in the source code of
SwipeRefreshLayout
you can find it here.Currently there isn't a public method, or even an internal one for that matter, that can be used to adjust the trigger distance. But you can copy over the three classes from the support library into your project, then modify
SwipeRefreshLayout
to your liking.Here are the classes you'll need to copy over:
The tigger distance is calculated using the constants:
In your layout change
<android.support.v4.widget.SwipeRefreshLayout.../>
to<your_path_to_SwipeRefreshLayout.../>
and make sure you change your imports, if you need to.