Quantcast
Channel: Sarina DuPont, Product Manager RAD Studio
Viewing all articles
Browse latest Browse all 132

Using Pull To Refresh with TListview in RAD Studio XE7

$
0
0

In RAD Studio XE7, we added built-in support for pull to refresh for TListView. On iOS, this shows the spinning indicator as you are refreshing your list, and on Android, it shows the glow effect.

I built a quick demo that populates my listview with data from the cloud. New data is added via the BaaS web interface. After new data has been added via my BaaS backend, pulling the list down results in new data being added to the list and the toolbar label listing the number of new items that have been added.

This demo consists of the following components:

User Interface:

  • TListView, aligned to the client
    • populated with data using LiveBindings Designer
  • TToolbar, aligned to the top
    • TLabel, parented to TToolbar, aligned to contents; TextSettings->HorzAlign: Center
  • TToolbar, aligned to the bottom
    • TLabel, parented to TToolbar, aligned to contents; TextSettings->HorzAlign: Center; TextSettings->FontColor: Dodgerblue;
BaaS:
  • TKinveyProvider
    • AppKey, AppSecret and MasterSecret have been set; you can also use TApp42Provider and TParseProvider
  • TBackendQuery for querying existing data that lives in the cloud and was added via the BaaS web interface
    • connected to KinveyProvider; BackendService = Storage; BackendClassName = Recipes (this was defined inside my Kinvey account))
  • TRESTResponseDataSetAdapter
    • DataSet = FDMemTable1
    • ResponseJSON = BackendQuery1
    • Active = True
  • TFDMemTable
Other:
  • TTimer
    • Enabled = True
    • Interval = 1000
  • TActionlist
    • Right-click to open ActionList Editor
    • Add a New Action
    • Create OnUpdate event
Object Pascal Code:
unit Unit3;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IPPeerClient,
  REST.OpenSSL, REST.Backend.ServiceTypes, REST.Backend.MetaTypes, System.JSON,
  REST.Backend.KinveyServices, FMX.ListView.Types, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  FireDAC.Phys.Intf, FireDAC.DApt.Intf, System.Rtti, System.Bindings.Outputs,
  Fmx.Bind.Editors, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Data.Bind.Components,
  Data.Bind.DBScope, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
  REST.Response.Adapter, FMX.ListView, Data.Bind.ObjectScope,
  REST.Backend.BindSource, REST.Backend.ServiceComponents,
  REST.Backend.KinveyProvider, System.Threading, FMX.StdCtrls, System.Actions,
  FMX.ActnList;

type
  TForm3 = class(TForm)
    KinveyProvider1: TKinveyProvider;
    BackendQuery1: TBackendQuery;
    ListView1: TListView;
    RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
    FDMemTable1: TFDMemTable;
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    LinkFillControlToField1: TLinkFillControlToField;
    Timer1: TTimer;
    BottomToolbar: TToolBar;
    Label1: TLabel;
    ActionList1: TActionList;
    Action1: TAction;
    TopToolbar: TToolBar;
    Label2: TLabel;
    Action2: TAction;
    procedure FormCreate(Sender: TObject);
    procedure ListView1PullRefresh(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Action1Update(Sender: TObject);
  private
    { Private declarations }
        FCount : Integer;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.fmx}
{$R *.iPhone4in.fmx IOS}

//execute Backend Query and populate Listview with Data on Application Launch
procedure TForm3.FormCreate(Sender: TObject);
begin
  BackendQuery1.Execute;
end;

//Custom Action for updating the Toolbar Label with Record Count Data
procedure TForm3.Action1Update(Sender: TObject);
begin
  if FCount > 0 then
      TAction(Sender).Text := Format ('Total Items: %d; New Items: %d', [
      (BindSourceDB1.DataSet.RecordCount), FCount])
  else
    TAction(Sender).Text := Format ('Number of Recipes: %d', [(BindSourceDB1.DataSet.RecordCount)]);
end;

//Enables Timer when PullRefresh Event Handler is called
procedure TForm3.ListView1PullRefresh(Sender: TObject);
begin
 Timer1.Enabled := true;

end;

//OnTimer Event for getting the BaaS Data Collection Record Count and executing the Backend Query
procedure TForm3.Timer1Timer(Sender: TObject);
begin
  FCount := BindSourceDB1.DataSet.RecordCount;
  BackendQuery1.Execute;
  FCount := BindSourceDB1.DataSet.RecordCount - FCount;
  Timer1.Enabled := False;
end;
end.

29 records in Recipes data collection. 29 Recipes shown in TListView

30 Records in Recipes Data Collection (1 newly added one). TListView shows new recipe; Toolbar Label lists total number of recipes and number of newly added recipes.


Viewing all articles
Browse latest Browse all 132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>