Knowing these 4 things helped me become good at Nativescript

Knowing these 4 things helped me become good at Nativescript

Understanding Nativescript at a high-level

  • It is Nodejs that makes it possible for a Javascript framework, like Nativescript, to run on IOS or Android platforms. Nodejs is what enables Javascript to be run in an environment other than a browser - the only environment in which it was run before its creation.
  • Nativescript apps are possible in Nodejs thanks to the Nativescript CLI, an NPM package that bundles the app code written by a developer - using a mixture of Nativescript Core Modules, direct API calls, and/or plugins- into Android or IOS files(AAB/IPA) that contain, additionally, a Javascript Virtual Machine(JVM) and a Nativescript runtime.
  • The JVM is responsible for understanding and running Javascript code and the Nativescript runtime is what translates that Javascript code to Java/Kotlin for Android or Objective C/Swift for IOS.
  • Since Nativescript calls the native APIs, when you are developing an app you may get errors that a native IOS/Android developer may get. When that happens a native code from answers(like the Java code below comes from one of them) to questions on Stackoverflow also applies to Nativescript problems. Basically, Nativescript allows developers to "speak" to an Android or IOS operating system in Javascript and gets the message delivered to the system in Swift/Objective C or Java/Kotlin. For example, if you wanted to send the following Java message to the Android system,

    ListView lv = (ListView)findViewById(R.id.myListView);  // your listview inside scrollview
    lv.setOnTouchListener(new ListView.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              int action = event.getAction();
              switch (action) {
              case MotionEvent.ACTION_DOWN:
                  // Disallow ScrollView to intercept touch events.
                  v.getParent().requestDisallowInterceptTouchEvent(true);
                  break;
    
              case MotionEvent.ACTION_UP:
                  // Allow ScrollView to intercept touch events.
                  v.getParent().requestDisallowInterceptTouchEvent(false);
                  break;
              }
    
              // Handle ListView touch events.
              v.onTouchEvent(event);
              return true;
          }
      });
    

    you would express yourself in Javascript like so:

const listView: android.widget.ListView = (args.object as ListView).android;

      listView.setOnTouchListener(
new android.widget.ListView.OnTouchListener({
        onTouch(view: android.view.View,motionEvent: android.view.MotionEvent): boolean{

            const action:number = motionEvent.getAction();

            switch(action){
              case android.view.MotionEvent.ACTION_DOWN:
                view.getParent().getParent().requestDisallowInterceptTouchEvent(true)
                break;

              case  android.view.MotionEvent.ACTION_DOWN:
                                view.getParent().getParent().requestDisallowInterceptTouchEvent(false)

            }
            view.onTouchEvent(motionEvent)
            return true;
        }
      })
)

With the code above, you are able to scroll a ListView nested in a ScrollView. Both the ListView and ScrollView have a scrolling functionality but the ListView's scroll gets intercepted by the parent ScrollView. So the code stops the ScrollView from intercepting the ListView's touch(thus scroll) event.