如何在listview中为每行添加按钮

如何在listview中为每行添加按钮,第1张

你是的意思是在listView的每一项里都加一个固定的按钮或图片吗,那就将它写到一个xml中,然后inflate到listView中,这样listView的每一项的布局就都是xml中的布局了。

<LinearLayout xmlns:android="

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

/>

<ListView

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout>

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.listview)

String[] data = {"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"}

ListView listView = (ListView) findViewById(R.id.listView)

listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, data))

在ListView的Item上显示CheckBox与Button

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

type

PItemCtrl = ^TItemCtrl

PItemCtrl = record

CheckBox: TCheckBox

Button: TButton

end

end

procedure TfrmMain.ListView1CustomDrawSubItem(Sender: TCustomListView

Item: TListItemSubItem: IntegerState: TCustomDrawState

var DefaultDraw: Boolean)

var

Rect: TRect

P: PItemCtrl

begin

{ 第2个子项目上显示CheckBox,第5个子项目上显示Button }

if SubItem in [2, 5] then

begin

DefaultDraw:= False// 不显示默认的文本.

Rect:= Item.DisplayRect(drBounds)// 获取Item显示的区域.

if Item.Data = nil then // 如果为空则创建CheckBox及Button.

begin

new(P)// 创建一个指针用于存储CheckBox及Button.

{ 创建并显示CheckBox }

P.CheckBox:= TCheckBox.Create(ListView1)

P.CheckBox.Parent:= ListView1

P.CheckBox.Caption:= ' '

P.CheckBox.Width:= 20

P.CheckBox.Height:= 20

P.CheckBox.Left:= Rect.Right - ListView1.Columns[3].Width

- ListView1.Columns[4].Width - ListView1.Columns[5].Width

- ((ListView1.Columns[2].Width + P.CheckBox.Width) div 2)

P.CheckBox.Top:= Rect.Top

P.CheckBox.Visible:= True

{ SubItems[2 -1].Caption为0和1,直接转换为Boolean型并给CheckBox赋值. }

P.CheckBox.Checked:= StrToBool(Item.SubItems[SubItem -1])

{ 创建并显示Button }

P.Button:= TSpeedButton.Create(ListView1)

P.Button.Parent:= ListView1

P.Button.Caption:= '... '

P.Button.Width:= 20

P.Button.Height:= 20

P.Button.Left:= Rect.Right - ((ListView1.Columns[5].Width

+ P.Button.Width) div 2)

P.Button.Top:= Rect.Top

P.Button.Visible:= True

Item.Data:= P// 将CheckBox及Button的结构指针保存于Item.Data属性.

end

end

end

在布局器中将按钮都加进去,然后再代码中根据情况来设置按钮是否可见: setVisibility(View.INVISIBLE) 给按钮添加一个listener,就可以得到点击事件。 希望能帮助你!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11668385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存