B4A Library [lib]mListView based on native ListView

icefairy333

Active Member
Licensed User
mListView based on native ListView
differents between ListView(in b4a)
implement :
1.divider(drawable,get or set height)
2.selected drawable (differents between Usually state)
3.scroll event(use to do something like:load next page,load last page,judge with this event)
4.FooterView and HeaderView
how to code:
B4X:
'must like ListView
dim lv as mListView
lv.Initialize("lv")
Activity.AddView(lv,0,0,-1,-1)'-1=matchparent,-2=wrapcontent
   Dim itbg,itbgp As BitmapDrawable
itbg.Initialize(LoadBitmap(File.DirAssets,"itbg.9.png"))
   itbg.Gravity=Gravity.FILL
   itbgp.Initialize(LoadBitmap(File.DirAssets,"itbgp.9.png"))
   itbgp.Gravity=Gravity.FILL
   lv.SingleLineLayout.Background=itbg
   lv.SingleLineLayout.Label.TextSize=20
   lv.SingleLineLayout.Label.TextColor=Colors.ARGB(255,255,71,65)
   lv.SingleLineLayout.SelectedBackground=itbgp
   Dim cd As ColorDrawable
   cd.Initialize(Colors.Red,5)
   lv.Divider=cd
   lv.DividerHeight=1dip
        Dim lbh,lbf As Label
   lbh.Initialize("")
   lbh.Text="Header"
   lbf.Initialize("")
   lbf.Text="Footer"
   lv.AddFooterView(lbf)
   lv.AddHeaderView(lbh)
   lv.SetData'call this after footer and header,must before you add any 
'data in listview
        For i=1 To 200
   lv.AddSingleLine2("项目名称:"&i,"项目名称:"&i)   
Next

b4amListView
Author: IceFairy333
Version: 1.2

  • Fields:
    • Background As Drawable
    • Label As LabelWrapper
    • SecondLabel As LabelWrapper
    • SelectedBackground As Drawable
    Properties:
    • ItemHeight As Int

  • Fields:
    • Background As Drawable
    • ImageView As ImageViewWrapper
    • Label As LabelWrapper
    • SecondLabel As LabelWrapper
    • SelectedBackground As Drawable
    Properties:
    • ItemHeight As Int

  • Fields:
    • Background As Drawable
    • Label As LabelWrapper
    • SelectedBackground As Drawable
    Properties:
    • ItemHeight As Int
  • mListView
    Events:
    • itemclick (Position As Int, Value As Object)
    • itemlongclick (Position As Int, Value As Object)
    • onscroll (view as Object As , firstVisibleItem as int As , visibleItemCount as int As , totalItemCount as int As )
    • onscrollstatechanged (view as Object As , scrollState as int As )
    Fields:
    • Status_scroll_begin As Int
    • Status_scroll_end As Int
    • nCurSel As Int
    Methods:
    • AddFooterView (vf As View)
      call this before SetData
    • AddHeaderView (vh As View)
      call this before SetData
    • AddSingleLine (Text As String)
    • AddSingleLine2 (Text As String, ReturnValue As Object)
    • AddTwoLines (Text1 As String, Text2 As String)
    • AddTwoLines2 (Text1 As String, Text2 As String, ReturnValue As Object)
    • AddTwoLinesAndBitmap (Text1 As String, Text2 As String, Bitmap As Bitmap)
    • AddTwoLinesAndBitmap2 (Text1 As String, Text2 As String, Bitmap As Bitmap, ReturnValue As Object)
    • BringToFront
    • Clear
    • GetItem (Index As Int) As Object
    • GetSelection As Int
    • Initialize (arg1 As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveAt (Index As Int)
      if index=nCurSel then nCurSel=-1
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetData
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetSelection (Position As Int)
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • Divider As Drawable
    • DividerHeight As Int
    • Enabled As Boolean
    • FastScrollEnabled As Boolean
    • Height As Int
    • Left As Int
    • ScrollingBackgroundColor As Int [write only]
    • SingleLineLayout As SingleLineLayout [read only]
    • Size As Int [read only]
    • Tag As Object
    • Top As Int
    • TwoLinesAndBitmap As TwoLinesAndBitmapLayout [read only]
    • TwoLinesLayout As TwoLinesLayout [read only]
    • Visible As Boolean
    • Width As Int
 

Attachments

Last edited:

padvou

Active Member
Licensed User
Hello,
I have implemented simplelistview and it's awsome.
However,
how do you unselect an item from code?
 

padvou

Active Member
Licensed User
Hi again,
how could i search for a specific value in the list and set it as selection by code?
 

padvou

Active Member
Licensed User
Hi again,
how could i search for a specific value in the list and set it as selection by code?
OK,
here's a rather strange but serious issue...
I three mlv's: lv, lv2, lv3
I have 4 buttons with the following code:
B4X:
Sub Button4_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(2)
lv3.SetSelection(3)
End Sub
Sub Button3_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(2)
lv3.SetSelection(1)
End Sub
Sub Button2_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(0)
lv3.SetSelection(2)
End Sub
Sub Button1_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(0)
lv2.SetSelection(0)
lv3.SetSelection(1)
End Sub
The lists are already filled by values, so you might as well overlook the initialize commands..
I I run the code pressing F8, it runs fine. If I execute it normally however, the wrong items are selected...
PLease! Any ideas?
Thank you
 

icefairy333

Active Member
Licensed User
can you post your complete code as an zip?
OK,
here's a rather strange but serious issue...
I three mlv's: lv, lv2, lv3
I have 4 buttons with the following code:
B4X:
Sub Button4_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(2)
lv3.SetSelection(3)
End Sub
Sub Button3_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(2)
lv3.SetSelection(1)
End Sub
Sub Button2_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(1)
lv2.SetSelection(0)
lv3.SetSelection(2)
End Sub
Sub Button1_Click
If lv3.IsInitialized=False Then   lv3.Initialize("lv3")
   lv3.SetSelection(-1)
If lv.IsInitialized=False Then lv.Initialize("lv")
   lv.SetSelection(-1)
If lv2.IsInitialized=False Then   lv2.Initialize("lv2")
   lv2.SetSelection(-1)
lv.SetSelection(0)
lv2.SetSelection(0)
lv3.SetSelection(1)
End Sub
The lists are already filled by values, so you might as well overlook the initialize commands..
I I run the code pressing F8, it runs fine. If I execute it normally however, the wrong items are selected...
PLease! Any ideas?
Thank you
 

NeverGiveUp2

Member
Licensed User
Problem with TwoLineandBmp

Hi, your work is realy great.
I tried to use your library for twolineandbmp and twolineslayout, but the selectBackground doesen't work by me.

Is it a bug or make i a mistake.

SingleLineLayout work really fine.

Best regards from Germany:sign0085:
 

mjas

Member
Licensed User
Hi, great job.

For me too, neither the background, or the selectedbackground works.

All the rest works fine.

Many thanks.:icon_clap:
 

mjas

Member
Licensed User
background issue

I wrote:
For me too, neither the background, or the selectedbackground works.

All the rest works fine.
But i worked it out:
AS "SingleLineLayout" extends "TwoLinesLayout" put the background on "SingleLineLayout" like this:
B4X:
SingleLineLayout.Background=something
or
SingleLineLayout.SelectedBackground=something
All the rest works fine, except the event listener "ClickItem" that has a bug when you have a header row and you click on it:
Application terminates with an "Out of bounds" exception.

Regards :)
 

tipallandgo

Member
Licensed User
Hi, this library is really nice.

I'm having a bit of a problem though. I have created multiple mListViews, populated them, and added them into the same activity.
The problem is this: for example, my mlistviews are named lv1 and lv2. When I try to use lv1.SetSelection(0), lv2 also gets updated and also selects the item in index 0.

B4X:
'Activity_Create code (Try this in a new project)

lv.Initialize("lv1")
lv2.Initialize("lv2")
Dim cd As ColorDrawable
cd.Initialize(Colors.Blue, 0)
lv.SingleLineLayout.SelectedBackground = cd
lv2.SingleLineLayout.SelectedBackground = cd
lv.SetData
lv2.SetData
For i=0 To 3
    lv.AddSingleLine("test" & i)
Next
For i=0 To 3
    lv2.AddSingleLine("tset" & i)
Next
Activity.AddView(lv, 0, 0, 100%x, 50%y)
Activity.AddView(lv2, 0, 50%y, 100%x, 50%y)
lv.SetSelection(0)
 

padvou

Active Member
Licensed User
For some unknown reason, if you Dim two different objects, bitmaps or drawables for selected backgroung, it works as expected.. for me it did.


Hi, this library is really nice.

I'm having a bit of a problem though. I have created multiple mListViews, populated them, and added them into the same activity.
The problem is this: for example, my mlistviews are named lv1 and lv2. When I try to use lv1.SetSelection(0), lv2 also gets updated and also selects the item in index 0.

B4X:
'Activity_Create code (Try this in a new project)

lv.Initialize("lv1")
lv2.Initialize("lv2")
Dim cd As ColorDrawable
cd.Initialize(Colors.Blue, 0)
lv.SingleLineLayout.SelectedBackground = cd
lv2.SingleLineLayout.SelectedBackground = cd
lv.SetData
lv2.SetData
For i=0 To 3
    lv.AddSingleLine("test" & i)
Next
For i=0 To 3
    lv2.AddSingleLine("tset" & i)
Next
Activity.AddView(lv, 0, 0, 100%x, 50%y)
Activity.AddView(lv2, 0, 50%y, 100%x, 50%y)
lv.SetSelection(0)
 

tipallandgo

Member
Licensed User
For some unknown reason, if you Dim two different objects, bitmaps or drawables for selected backgroung, it works as expected.. for me it did.
So I tried this:

B4X:
lv.Initialize("lv1")
lv2.Initialize("lv2")
Dim cd, cd2 As ColorDrawable
cd.Initialize(Colors.Blue, 0)
cd2.Initialize(Colors.Green, 0)
lv.SingleLineLayout.SelectedBackground = cd
lv2.SingleLineLayout.SelectedBackground = cd2
lv.SetData
lv2.SetData
For i=0 To 3
    lv.AddSingleLine("test" & i)
Next
For i=0 To 3
    lv2.AddSingleLine("tset" & i)
Next
Activity.AddView(lv, 0, 0, 100%x, 50%y)
Activity.AddView(lv2, 0, 50%y, 100%x, 50%y)
lv.SetSelection(1)
They still both select the item in index 1. :(
 

Ionut Indigo

Member
Licensed User
Hi, first of all this library is amazing! Great job icefairy333.
Second, I'm having a hard time figuring out how to achieve something similar to the YouTube drawer listview with Headers/Footers (picture here).
How can i have a header then some list items, a header, list items and a footer?
 

edgeryder1000

Member
Licensed User
Hi, first of all this library is amazing! Great job icefairy333.
Second, I'm having a hard time figuring out how to achieve something similar to the YouTube drawer listview with Headers/Footers (picture here).
How can i have a header then some list items, a header, list items and a footer?
You can use multiple listviews inside a scrollview to achieve this.

@icefairy333: is there a way to change the highlight color of the list?
 

Inman

Well-Known Member
Licensed User
This is a nice library. The SelectedBackground feature itself is worth switching to this.

But currently, when I longpress an item, the background color is Holo Blue. Is it possible to change it?
 
Top