Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uListViewSwitchTest;
- interface
- uses
- System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
- FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
- FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
- FMX.ListView, FMX.Controls.Presentation, FMX.StdCtrls;
- type
- TListItemSwitch = class(TListItemSimpleControl)
- private type
- TNeededToDo = set of (SetChecked, CallClick);
- private
- FIsChecked: Boolean;
- FOnSwitch: TNotifyEvent;
- procedure SetIsChecked(const AValue: Boolean);
- protected
- function MouseDown(const Button: TMouseButton; const Shift: TShiftState; const MousePos: TPointF): Boolean;
- override;
- procedure DoSwitch; virtual;
- public
- constructor Create(const AOwner: TListItem); override;
- destructor Destroy; override;
- procedure Render(const Canvas: TCanvas; const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
- const SubPassNo: Integer = 0); override;
- public
- property IsChecked: Boolean read FIsChecked write SetIsChecked;
- property OnSwitch: TNotifyEvent read FOnSwitch write FOnSwitch;
- end;
- TForm1 = class(TForm)
- ListView1: TListView;
- Switch1: TSwitch;
- procedure ListView1UpdateObjects(const Sender: TObject;
- const AItem: TListViewItem);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.fmx}
- { TListItemSwitch }
- constructor TListItemSwitch.Create(const AOwner: TListItem);
- begin
- inherited;
- end;
- destructor TListItemSwitch.Destroy;
- begin
- inherited;
- end;
- procedure TListItemSwitch.DoSwitch;
- begin
- FIsChecked:= not FIsChecked;
- if Assigned(OnSwitch) then
- OnSwitch(Self);
- end;
- function TListItemSwitch.MouseDown(const Button: TMouseButton;
- const Shift: TShiftState; const MousePos: TPointF): Boolean;
- begin
- if (Button = TMouseButton.mbLeft) and Enabled then begin
- DoSwitch;
- end;
- inherited;
- end;
- procedure TListItemSwitch.Render(const Canvas: TCanvas;
- const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
- const SubPassNo: Integer);
- var
- R, R2: TRectF;
- begin
- inherited;
- R:= Self.LocalRect;
- R2:= R;
- Canvas.BeginScene;
- try
- Canvas.Stroke.Kind:= TBrushKind.None;
- Canvas.Fill.Kind:= TBrushKind.Solid;
- Canvas.Fill.Color:= TAlphaColorRec.Skyblue;
- Canvas.FillRect(R, 8, 8,
- [TCorner.TopLeft, TCorner.TopRight, TCorner.BottomLeft, TCorner.BottomRight],
- 1.0, TCornerType.Round);
- if IsChecked then begin
- R2.Left:= R.Right - 20;
- R2.Width:= 20;
- end else begin
- R2.Left:= R.Left;
- R2.Width:= 20;
- end;
- Canvas.Fill.Color:= TAlphaColorRec.Black;
- Canvas.FillRect(R2, 8, 8,
- [TCorner.TopLeft, TCorner.TopRight, TCorner.BottomLeft, TCorner.BottomRight],
- 1.0, TCornerType.Round);
- finally
- Canvas.EndScene;
- end;
- end;
- procedure TListItemSwitch.SetIsChecked(const AValue: Boolean);
- begin
- FIsChecked:= AValue;
- end;
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject);
- var
- I: TListViewItem;
- begin
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- I:= ListView1.Items.Add;
- end;
- procedure TForm1.ListView1UpdateObjects(const Sender: TObject;
- const AItem: TListViewItem);
- var
- S: TListItemSwitch;
- begin
- S:= AItem.Objects.FindObject('Switch') as TListItemSwitch;
- if S = nil then begin
- S:= TListItemSwitch.Create(AItem);
- S.Name:= 'Switch';
- S.Align:= TListItemAlign.Trailing;
- S.VertAlign:= TListItemAlign.Center;
- S.Width:= 50;
- S.Height:= 20;
- S.IsChecked:= False;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment