Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23406] iOS/Android: Add 'scrolling' event to ListView to track scroll direction (up or down)

GitHub Issuen/a
TypeImprovement
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2016-09-06T07:34:39.000+0000
Affected Version/sn/a
Fix Version/sRelease 6.1.0
ComponentsAndroid, iOS
Labelsn/a
ReporterRichard Lustemberg
AssigneeHans Knöchel
Created2016-05-18T16:25:43.000+0000
Updated2016-12-02T19:25:42.000+0000

Description

At the moment is not possible to determine the scroll direction of a ListView natively and as the view is scrolling. Very common effects such as the hiding of the navigation bar when scrolling (like on the Facebook app) become impossible to achieve without cumbersome workarounds. I will send a PR with a modification to the SDK core. Probably some modifications like coding style and naming will be necessary (By no means I am an Objective C dev). Most of the code I took straight from Stackoverflow [link Stackoverflow](http://stackoverflow.com/a/21263997/436785)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        if ([self isLazyLoadingEnabled]) {
            [[ImageLoader sharedLoader] resume];
        }
        [self fireScrollEnd:(UITableView *)scrollView];
    }
    
    if ([self.proxy _hasListeners:@"dragend"]) {
        [self.proxy fireEvent:@"dragend" withObject:nil withSource:self.proxy propagate:NO reportSuccess:NO errorCode:0 message:nil];
    }

    if ([self.proxy _hasListeners:@"pullend"]) {
        if ( (_pullViewProxy != nil) && (pullActive == YES) ) {
            pullActive = NO;
            [self.proxy fireEvent:@"pullend" withObject:nil withSource:self.proxy propagate:NO reportSuccess:NO errorCode:0 message:nil];
        }
    }
}

Comments

  1. Richard Lustemberg 2016-05-18

    Sorry, I put the wrong snippett ;) And the file modified is iphone/Classes/TIUIListView.m
       -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
           
           if ([self.proxy _hasListeners:@"scrolling"]) {
               NSMutableDictionary *eventArgs = [NSMutableDictionary dictionary];
               NSString* swipeString = @"none";
               if (velocity.y > 0){
                   swipeString = @"up";
               }
               if (velocity.y < 0){
                   swipeString = @"down";
               }
               if(swipeString !=(id)[NSNull null])
               {
                   [eventArgs setValue:swipeString forKey:@"direction"];
                   [self.proxy fireEvent:@"scrolling" withObject:eventArgs withSource:self.proxy propagate:NO reportSuccess:NO errorCode:0 message:nil];
               }
           }
       }
       
  2. Nuno Costa 2016-05-19

    This will solve lot of our problems :)
  3. Nuno Costa 2016-05-19

    @Richard Lustemberg you actually tested the code, and if yes that worked?
  4. Richard Lustemberg 2016-05-19

    Yes, I am using it [It's here ](https://github.com/appcelerator/titanium_mobile/pull/8005) I guess that to make it production ready some extra thoughts should be given to the event name, at the very least ;)
  5. Nuno Costa 2016-05-19

    I know is there :) you js code is ?:
       $.listView.addEventListener('scrolling'), function scrollingList(e) {
       	
        	log.warn(JSON.stringify(e, null, 2));
       };
       
       
  6. Richard Lustemberg 2016-05-19

    yes, exactly that.
  7. Nuno Costa 2016-05-28

    Can we do the same for Android?
  8. Hans Knöchel 2016-05-28

    http://stackoverflow.com/a/17017477/5537752 looks promising. Although it seems like we cannot get the velocity and targetContentOffset, we get at least say if its scrolling up or down. Anyone interesting in implementing that?
  9. Hans Knöchel 2016-05-28

    Btw, iOS PR (for linking): https://github.com/appcelerator/titanium_mobile/pull/8005
  10. Richard Lustemberg 2016-05-28

    I'll try to do that. The project I'm working on needs it :)
  11. Michael Gangolf 2016-05-28

    Done: https://github.com/appcelerator/titanium_mobile/pull/8031 ;) *Edit:* have to change it, at the moment it fires the whole time while scrolling (so it will detect chaging the direction during scrolling). In the yml file it says it will only fire when lifting the finger
  12. Michael Gangolf 2016-05-28

    I've changed the doc to: {quote}On iOS no event is fired when the finger is not released.{quote} since it will fire when the finger is down on Android, too.
  13. Hans Knöchel 2016-05-30

    [~msamah] or [~hpham] to review the Android-side? Looks good to me.
  14. Hieu Pham 2016-05-31

    Reviewed code, left some comments.
  15. Ashraf Abu 2016-07-26

    Waiting for m1ga aka [~michael] to provide updates for this. For the moment, I believe he doesn't have time to look throughly at it.
  16. Michael Gangolf 2016-07-26

    Sorry, at the moment I can't compile the source since my normal work environment is under linux. I'll try to setup a windows machine to get things running though.
  17. Ashraf Abu 2016-07-27

    No worries.
  18. Michael Gangolf 2016-08-13

    Updated the code and made a new PR: https://github.com/appcelerator/titanium_mobile/pull/8216
  19. Ashraf Abu 2016-09-06

    Android PR https://github.com/appcelerator/titanium_mobile/pull/8216 merged.
  20. Lokesh Choudhary 2016-12-02

    Verified the implementation. scrolling event is fired successfully & it gives the direction as up or down. Closing. Appc Studio : 4.8.1.201611291132 SDK Version : 6.1.0.v20161202104721 Mac OS Version : 10.12 Xcode Version : Xcode 8.1 Build version 8B62 Appc CLI AND Appc NPM : {"NPM":"4.2.9-1","CLI":"6.1.0-302"} Ti CLI : 5.0.11 Alloy : 1.9.4 Node : v4.6.0 Device: running 7.1Pixel IOS simulator : Iphone 7 plus running IOS 10

JSON Source