線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1011
推到 Plurk!
推到 Facebook!

用ListView显示动态的数据

 
pcplayer99
尊榮會員


發表:142
回覆:738
積分:591
註冊:2003-01-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2016-04-29 16:48:24 IP:120.237.xxx.xxx 訂閱
比如我在下载几个File,下载进度要动态显示。使用ListView来显示这几个正在下载的File,并动态显示下载进度。

unit Unit2;
{--------------------------------------------------------------------------
测试如何自己创建 ListView 的 ListItem
--------------------------------------------------------------------------}
interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, FMX.ListView.Types, FMX.ListView.Appearances,
FMX.ListView.Adapters.Base, FMX.ListView, FMX.Objects;

type
TForm2 = class(TForm)
ToolBar1: TToolBar;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
ListView1: TListView;
Image1: TImage;
Image2: TImage;
SpeedButton3: TSpeedButton;
Timer1: TTimer;
procedure ListView1UpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
procedure FormCreate(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
private
{ Private declarations }
FMyIndex: Integer;

FCurrentItem: TListItemText;

procedure ShowNextPage;
public
{ Public declarations }
end;


//通过继承里面的一个条目的元素,来达到动态改变其显示值的效果。测试OK
TMyListItemText = class(TListItemText)
private
public
procedure ShowMyText(const S: string);
end;
var
Form2: TForm2;

implementation

const
MAX_PAGE_COUNT = 30;
MY_CAPTION = 'MyCaption';
My_Text = 'MyText';
Thumb_Nail_Name = 'ThumbNailName';

{$R *.fmx}

{ TForm2 }

procedure TForm2.FormCreate(Sender: TObject);
begin
Image1.Visible := False;
Image2.Visible := False;
end;

procedure TForm2.ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
var
C: TListItemText;
begin
C := AItem.View.FindDrawable(MY_CAPTION) as TListItemText;
if not Assigned(C) then Exit;

if C is TMyListItemText then
begin
FCurrentItem := C; //绑定这个到时钟。
end;

end;

procedure TForm2.ListView1UpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
var
LImage: TListItemImage;
LCaption, MyCaption: TListItemText;
begin
//每个条目在被创建的时候会触发这个事件。在这个事件里,为条目增加元素。
LImage := AItem.View.FindDrawable(Thumb_Nail_Name) as TListItemImage;
if LImage = nil then
begin
LImage := TListItemImage.Create(AItem);
LImage.Name := Thumb_Nail_Name;
LImage.Align := TListItemAlign.Trailing;
LImage.PlaceOffset.Y := 5;
LImage.PlaceOffset.X := -30;
LImage.Width := 20;
LImage.Height := 20;
end;

LCaption := AItem.View.FindDrawable(My_Text) as TListItemText;
if LCaption = nil then
begin
LCaption := TListItemText.Create(AItem);
LCaption.Name := My_Text;
LCaption.Align := TListItemAlign.Trailing;
LCaption.VertAlign := TListItemAlign.Trailing;
LCaption.PlaceOffset.X := -10;
LCaption.TextAlign := TTextAlign.Center;
LCaption.Trimming := TTextTrimming.Character;
LCaption.IsDetailText := True;
LCaption.Width := 60;
LCaption.Height := 18;
end;

//以下这个元素是我自己定义的。果然能在 ListView 的 Item 里面出现。
MyCaption := AItem.View.FindDrawable(MY_CAPTION) as TListItemText;
if MyCaption = nil then
begin
MyCaption := TMyListItemText.Create(AItem);
MyCaption.Name := MY_CAPTION; //MY_CAPTION 是一个字符串常量
MyCaption.Align := TListItemAlign.Center;
MyCaption.VertAlign := TListItemAlign.Center;// TListItemAlign.Trailing;
MyCaption.PlaceOffset.X := 10;
MyCaption.TextAlign := TTextAlign.Center;
MyCaption.Trimming := TTextTrimming.Character;
MyCaption.IsDetailText := True;
MyCaption.Width := 60;
MyCaption.Height := 18;
end;
end;

procedure TForm2.ShowNextPage;
var
i: Integer;
LItem: TListViewItem;
begin
ListView1.Items.Clear;

for i := FMyIndex to FMyIndex MAX_PAGE_COUNT -1 do
begin
LItem := ListView1.Items.Add;
LItem.Text := Format('Text %d', [I]);
LItem.Detail := Format('Detail %d', [I]);
LItem.BitmapRef := Image1.Bitmap; //要显示这个图,设计期选择 ListView1 的属性 ItemAppearance.ItemAppearance 为 ImageListItem
LItem.Data[MY_CAPTION] := '哈哈';
LItem.Data[My_Text] := Format('%d thumb', [I]);

// LItem.Height := 80;

// LItem.Data[sThumbNailName] := Format('Caption %d', [I]);
// Do the following instead of above line. Above line will copy the image
(LItem.Objects.FindDrawable(Thumb_Nail_Name) as TListItemImage).OwnsBitmap := False;
(LItem.Objects.FindDrawable(Thumb_Nail_Name) as TListItemImage).Bitmap := Image2.Bitmap;
end;

FMyIndex := i;
end;

procedure TForm2.SpeedButton2Click(Sender: TObject);
begin
Self.ShowNextPage;
end;

procedure TForm2.SpeedButton3Click(Sender: TObject);
begin
ListView1.EditMode := not ListView1.EditMode;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
var
T: TDateTime;
begin
if Assigned(FCurrentItem) then
begin
T := Now;
TMyListItemText(FCurrentItem).ShowMyText(TimeToStr(T));
end;
end;



{ TMyListItemText }

procedure TMyListItemText.ShowMyText(const S: string);
begin
Self.Text := S;
end;

end.
編輯記錄
pcplayer99 重新編輯於 2016-04-29 16:50:57, 註解 無‧
pcplayer99 重新編輯於 2016-04-29 16:51:23, 註解 無‧
pcplayer99
尊榮會員


發表:142
回覆:738
積分:591
註冊:2003-01-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2016-04-29 16:49:50 IP:120.237.xxx.xxx 訂閱
这里怎么排版啊?
系統時間:2017-10-24 4:41:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!