How to move up scrollview.top position?

moster67

Expert
Licensed User
Longtime User
I am struggling with a problem which in this moment I really cannot understand how to resolve. It is probably something stupid and very simple which I am overlooking.

In an activity I have a listview which is populated dynamically. Below the listview I have a scrollview with a panel and a label. The idea is that when a user clicks on an item in the listview some information is shown below in the label. I need a scrollview since the contents which needs to be written in the label can be very long. So far so good.

It looks graphically OK when the listview contains many items but when there is only one item in the listview, I get some empty space between the listview and the below scrollview. So when this happens, I want to move the scrollview upwards just below the listview to cover this empty space. No matter what I cannot get this to work. :BangHead:

I have posted a small project to simulate what I would like to do. Just run it and then click on the button. When the button is clicked I want the the scrollview to move up. Read my comments in the button-click event.

Anyone who can give me a helping hand? Maybe with some code-example if possible. Thanks.
 

Attachments

  • testscrollview.zip
    7.5 KB · Views: 276

klaus

Expert
Licensed User
Longtime User
Modify your routine to this:
B4X:
Sub Button1_Click
    ListView1.Clear
    ListView1.AddTwoLines2("Item 1", "Description of Item 1", "My value is 1")
    
    'My problem!!
    'In this case when Listview1 only has 1 item, I want to move up the scrollview just below the bottom of list listview
    ' and to stretch and make the scrollview higher to cover the empty space just above button1
    
    'Msgbox(ListView1.TwoLinesLayout.ItemHeight,"")
    'ListView1.Height=160dip 'ListView1.TwoLinesLayout.ItemHeight
    ListView1.Height = ListView1.Top + ListView1.TwoLinesLayout.ItemHeight +1dip
    ScrollView1.Top = ListView1.Top + ListView1.Height + 2dip
    ScrollView1.Height = Button1.Top - ScrollView1.Top
End Sub
You must remove * 100dip/100

Best regards.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
So the problem was "* 100dip/100". I read somewhere it is always better to assign dip to properties such as Height, Top and so on and therefore I used it. Apparently in this case I shouldn't do that :BangHead:

Thanks Klaus!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you define views in the Designer the Left, Top, Width and Height properties are considered as dip values.
When you define these properties in the code you should use dip values.
B4X:
ScrollView1.Top = 100dip
But when you use
B4X:
ScrollView1.Top = ListView1.Top + ListView1.Height + 2dip
ListView1.Top and ListView1.Height have already 'dip' values. So you must not multiply them by the density value.

Best regards.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Thanks for the explanation. Reading your posts I always learn something new.

Inviato dal mio GT-I9300 con Tapatalk 2
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…