B4A Library AHSwipeToRefresh - Wrapper for Android SwipeToRefresh implementation

With the v19.1 version of the Android support library Google added a SwipeToRefresh view to the android system. This is usable with API version 4 and up but it needs the android-support-v4.jar library.

Google uses two different implementations of the SwipeToRefresh usepattern in their own apps. This one is similar to the "Google Now" implementation. Nearly all other Google apps use another (in my opinion slightly nicer) solution but the Google Now version is, what made it into the official Android SDK.

Usage:
Add the AHSwipeToRefresh Object to the activity. This can be done manually or with the designer as a custom view.
Then you have to add a view to the AHSwipeToRefresh object which then can be pulled to start the refresh process. You can only add one single view to the AHSwipeToRefresh object. This should be something like a ListView or ScrollView.
When the user starts a refresh process the "Refresh" event is fired where you can start your refresh process (like loading some data over the net). When the refresh process is complete just set the Refresh property to false to stop the animation.

Installation:
Copy the AHSwipeToRefresh.jar and AHSwipeToRefresh.xml files to your custom libraries folder.

Additionally you will need the android-support-v4.jar. Check with the Android SDK Manager if you have the latest Android Support Library (You will need at least v22) installed. You can find the library in the <SDK_HOME>\extras\android\support\v4 folder. Copy the library to your custom libraries folder.

History:
V1.00
- Initial version

V1.10
- Requires Support Library v22 or up
- Fixed problems with UltimateListView (hopefully, simple example provided)
- Added ability to change background color of the progress indicator
- Added ability to set any number of colors for the progress indicator
- Supports a larger progress indicator
- New event (STR_CanChildScrollUp) to determine when a swipe event should be triggered or not.
- Support for multiple views (See example)
- Added method to set the offset (position) of the progress indicator
 

Attachments

  • AHSwipeToRefreshExample.zip
    13.2 KB · Views: 743
  • AHSwipeToRefreshULVExample.zip
    11.8 KB · Views: 611
  • AHSwipeToRefreshMultiExample.zip
    12.2 KB · Views: 639
  • AHSwipeToRefresh1_10.zip
    13.8 KB · Views: 876
  • ViewPagerSwipeToRefreshExample.zip
    12.4 KB · Views: 652
Last edited:

Scunkaneli

New Member
Licensed User
Longtime User
I tested it with your example and and my app and custom colors

Auf Deutsch: egal ob ich meine App probiere oder dein Beispiel von oben, die Farben ändern sich nicht, soweit ich das gesehen habe, haben sich ein paar Methoden in der "SwipeRefreshLayout" Class geändert, mit der "alten" Support Library hat alles funktioniert

Android 5.1 and CyanogenMod
 

corwin42

Expert
Licensed User
Longtime User
Sorry that my answer took so long. You are right. Unfortunately the old version of the SwipeRefreshLayout in the support library did not have methods to set the colors directly but only with colors defined in resources. So I had to use reflection to implement a "hack" that it is possible with B4A to change the colors. Now Google has fixed this problem and provided new methods for setting colors directly.

I just worked on an update and I think I have a new version ready for release. This version will have some nice improvements and should be compatible with ULV (at least with simple lists. Don't know if the advanced swipe gestures of ULV still work but they should). Needs some additional testing but looks quite good at the moment.
 

Reids

Member
Licensed User
Longtime User
Hey something went wrong with

STR.SetColorScheme()
STR.SetColorScheme2()

it say

java.lang.NoSuchMethodError: de.amberhome.strefresh.base.CustomSwipeRefreshLayout.setColorSchemeColors

removing those line will fix it, but the loader doesn't have any color
 

corwin42

Expert
Licensed User
Longtime User
it say

java.lang.NoSuchMethodError: de.amberhome.strefresh.base.CustomSwipeRefreshLayout.setColorSchemeColors

Update your android-support-v4.jar.

See release notes. You need at least v22.
 

Reids

Member
Licensed User
Longtime User
Cool problem solved, It require android-support-v4.jar placed in library folder too not just copy-ing v4 folder to library folder
thank you very much!
 

corwin42

Expert
Licensed User
Longtime User
Added a new example "ViewPagerSwipeToRefreshExample" to the first post.

The example shows the usage of the CanChildScrollUp event. It has a Viewpager (AHViewPager library needed) with four pages and a ListView on each page.
All four ListViews can trigger a single SwipeToRefresh object. The logic, when to trigger the refresh and when not is done in the CanChildScrollUp event.
 

corwin42

Expert
Licensed User
Longtime User
It's can't be used for webview?
You can use it for WebView, if you use the CanChildScrollUp event. See ViewPagerSwipeToRefreshExample how to use it.
 
Last edited:

frenkipi

Member
Licensed User
Longtime User
Hello, I have an infinite scroll with pulltorefresh wich is containing customlistview, but now my method for when I'm on the end of scroll doesn't work.

Any sugestions?

Regards!
 

Attachments

  • CustomListView.zip
    95.4 KB · Views: 216

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello corwin42 I trying your library and get this error:

B4A line: 35
Activity.AddView(STR, 0, 0, 100%x, 100%y)
javac 1.8.0_73
src\de\amberhome\swipetorefreshulv\main.java:337: error: cannot access SwipeRefreshLayout
mostCurrent._activity.AddView((android.view.View)(mostCurrent._str.getObject()),(int) (0),(int) (0),anywheresoftware.b4a.keywords.Common.PerXToCurrent((float) (100),mostCurrent.activityBA),anywheresoftware.b4a.keywords.Common.PerYToCurrent((float) (100),mostCurrent.activityBA));
^
class file for android.support.v4.widget.SwipeRefreshLayout not found
1 error


I compiling with C:\Android\platforms\android-22\android.jar and C:\Program Files\Java\jdk1.8.0_73
 

fbritop

Active Member
Licensed User
Longtime User
Great library, but can this Lib be used with another types of views, for example a Custom List View or a Panel?

When I tried it outputs an error of:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 

peacemaker

Expert
Licensed User
Longtime User
The lib is working with WebView, but
1) not all links
2) and only under old Android versions, 4.4 works, but 5.0, 6.0 - not.

If "STR_CanChildScrollUp As Boolean" exists - only this is fired.
If no - "STR_Refresh" is fired, but under Android 5+ - the webview is blank. But in the default browsser, Chrome browser and just Webview (without adding it to a STR object) the ULR is opened OK.
So, no idea why not all ULRs are visible, but i have to stop using the lib as users complain.

B4X:
Sub STR_Refresh
    'Disable the STR object so we can not start a new refresh process
STR.Refreshing = True
STR.Enabled = False
    wv.StopLoading
    wv.LoadUrl(LastURL)
End Sub
 

eps

Expert
Licensed User
Longtime User
I'm trying to use the ViewPagerSwipeToRefresh example, but it doesn't seem to want to display anything for me... It's carried out all the processing but doesn't display the Page Content. I had to change this line

B4X:
VP.Initialize(PC,"")

to

VP.Initialize("PC")

but apart from that I've left it unchanged.

I've updated the android-support-v4.jar to v22.2.1 version as well...

Any thoughts? I'm using BA 6.80 and Android 7.0 on an HTC 10..

It's probably something I haven't set up correctly.
 

DonManfred

Expert
Licensed User
Longtime User
using android-support-v4.jar in b4a 6.8 is, i think, the wrong solution.

You should remove it and add a reference to the google maven repo

start a new thread if you want to know more.
 

eps

Expert
Licensed User
Longtime User
using android-support-v4.jar in b4a 6.8 is, i think, the wrong solution.

You should remove it and add a reference to the google maven repo

start a new thread if you want to know more.

I thought so as well - but it doesn't work without it either

Basically the example doesn't work and I can't work out why.
 
Top