Android Question ScrollView not scrolling

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to Everybody
I have a strange situation, using a ScrollView. The following code worked in the past. Now I am trying to re-use it and the scrollview that I used, doesn't scroll automatically. The ScrollView is named "Lista" and is defined in the designer (with internal height 500, the default). The sub assumes a global iRow counter that is sequentially incremented by other operations.

B4X:
Sub AddLineToList
    
    Dim Row As Panel
    Dim pos As Int
    Dim RowHeight,nRowPage As Int
   
    RowHeight=26dip

    nRowPage=Lista.Height/RowHeight

    Row.Initialize("")
    '.....
    ' Row is filled by other Views that I omit to avoid confusion
    ' then Row is added to the Lista.Panel at iRow-1 position

    Lista.Panel.AddView(Row,1,2dip+(iRow-1)*(RowHeight) ,Lista.Width-6,RowHeight)
    
    pos=RowHeight*(iRow-nRowPage+1)

    If pos>0 Then
        Lista.ScrollPosition=pos ' doesn't work ... nothing happens .
    End If
End Sub
In my case, I have 23 nRow. Initially pos is negative, but also when it becomes positive (adding rows, incrementing iRow), the ScrollView doesn't scroll up...BUT, as I said, such code worked in other Apps and I don't figure out what is now happening or what I am missing..
Thanks in advance for any hint.
 
Solution
The attached project works as you expect it to work with both anchor types.
The problem in your code with my solution is that you changed a few things:
- the value of iLettura begins with 1 but iRow begins with 0.
- nRowPage = ScrollView1.Height/RowHeight in my code becomes
nLPagMisure=Lista.Panel.Height/LineHeight in your code, why ?
I use the Scrollview.Height but you use the Scrollview.Panel.Height
You did not take this in account.

I added this line
Lista.Panel.Height = Lista.Height
to avoid problems depending on the Lista.Height you define in the Designer and the value of the internal panel height in the Designer.

Sagenut

Expert
Licensed User
Longtime User
Try adding
B4X:
Lista.Panel.Height = pos
after
B4X:
pos=RowHeight*(iRow-nRowPage+1)
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Try adding
B4X:
Lista.Panel.Height = pos
after
B4X:
pos=RowHeight*(iRow-nRowPage+1)
Thanks, but It doesn't solve anything, unluckily. Moreover, as I wrote, until that iRow reaches the value of nRowPage, pos is negative, so putting Lista.Panel.Height to a negative value seems not good, if I don't miss something..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
BTW I have created a test project with same code, and it works. Therefore it should be something related to my particular project. Forget about. Thanks anyway.
 

Attachments

  • TestScroll.zip
    9.4 KB · Views: 47
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I found the reason, which is very subtle. If the ScrollView is anchored both to top and to bottom, my code doesn't scroll. This explain why in other Apps I never saw this problem: in other Apps, the ScrollView was anchored only to the Top. In the TestScroll App attached to previous post, ScrollView1 is anchored to the top. Anchor it to Bottom too, and it will not scroll. Thanks for your attention..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
BTW ... I would like to anchor the view to both, Top and Bottom, so the question is still open, and maybe not so trivial ..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Attached your project modified.
Thanks. Actually the code works as expected. But I didn't get out of the problem. As a matter of fact, I prepared a simplified version of my actual code, to make the test project. Migrating your indications to the actual App, gives no result. At the end, I see the problem: while in the TestApp the number of rows per page is correct (nRowPage=PanelHeight/RowHeight), the same instruction gives a wrong number in my app. I thought that in the designer, I had "autoscaleAll" in the Test Project, while in the actual App AutoScaleAll was commented. Nevertheless, despite setting AutoscaleAll also in the layout of the App, the number remains wrong. The thing is rather misterious (for me), because the nRowPage is simply a division. I empirically resolved forcing the correct nRowPage value. Until I don't have a correct value for the number of lines per page, no chance .. Anyway thanks a lot.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks. Actually the code works as expected. But I didn't get out of the problem. As a matter of fact, I prepared a simplified version of my actual code, to make the test project. Migrating your indications to the actual App, gives no result. At the end, I see the problem: while in the TestApp the number of rows per page is correct (nRowPage=PanelHeight/RowHeight), the same instruction gives a wrong number in my app. I thought that in the designer, I had "autoscaleAll" in the Test Project, while in the actual App AutoScaleAll was commented. Nevertheless, despite setting AutoscaleAll also in the layout of the App, the number remains wrong. The thing is rather misterious (for me), because the nRowPage is simply a division. I empirically resolved forcing the correct nRowPage value. Until I don't have a correct value for the number of lines per page, no chance .. Anyway thanks a lot.
nRowPage is the number of rows per "page".
In the source (in Klaus's but I suppose also in yours) the row height is fixed (26dip). If you do not set the ScrollView to a fixed height (but anchor it) and do not disable AutoScaleAll, obviously nRowPage will vary based on the device display.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
nRowPage is the number of rows per "page".
In the source (in Klaus's but I suppose also in yours) the row height is fixed (26dip). If you do not set the ScrollView to a fixed height (but anchor it) and do not disable AutoScaleAll, obviously nRowPage will vary based on the device display.
I always disable AutoscaleAll. I was suspicious on this parameter and, in the project, I tried either disabling it or not. Things do not change: the number of rows per page remains the same. Anyway, just a precisation: either using autoscaleall or not, I calculate the number of rows per page "dynamically", as the ratio between the panel height and the fixed value 26dip. Therefore It should change either docking the ScrollView only to the top, or both on Top and Bottom. Anyway, I will post not my entire project, but the layout and the actual sub, as Klaus suggested and probably it will be clear where I did the mistake .. Thanks for now.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. I have prepared a TestScroll2 project, using exactly my code and the involved part of my Layout. I have put two conditional compilation variables. One is KLAUS and activates Klaus solution. The other is PIG (which is unfortunately my confidential nickname, derived from my name and title, sorry). I am using a Samsung Galaxy A12. WHat happens is (correct me if I did some mess, always possible):
1) Klaus solution: works only docking the ScrollView to both Top and Bottom. The value of nLinePage (the former nRowPage) is wrong. That's why it doesn't work docking on Top only.
2) Moreover it shortly shows the line on bottom position, before scrolling., and puts a blank row at bottom. Simply a little defect.
3) PIG solution. Works only forcing the number of lines per page and docking the ScrollView to top (didn't try docking only to Bottom), but performs exactly how I would like.
I remark that it is very weird that the number of Lines per page, calculated with Lista.Panel.height / 26 is wrong.. Maybe I miss something ..
That's all. Thanks for your patience.
 

Attachments

  • TestScroll2.zip
    10.2 KB · Views: 44
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Bisogna fare chiarezza.

Innanzitutto, suppongo che tu voglia vedere solo "linee" intere, non che queste siano un numero fisso deciso da te, a prescindere dal dispositivo; sbaglio?

L'errore principale, ma non l'unico, è il calcolo del numero di linee, che è basato sull'altezza del pannello interno. Tu non imposti questa altezza. Sul mio dispositivo, nel tuo esempio, l'altezza di default del pannello interno è 1500 dip, mentre quella della ScrollView (Lista) è 593 dip. Nota che nel suo esempio hai AutoScaleAll su True e ancoraggio solo in alto. In questa modalità, a seconda del dispositivo l'altezza della Lista sarà diversa.

Ancoraggio e AutoScaleAll influenzano "l'esterno" della ScrollView, non quella del suo pannello interno, ecco perché ripeti che non cambia alcunché, nelle varie prove.
Chiarito questo, non dovresti avere ulteriori problemi.

Ripeto che sarebbe parecchio meglio se usassi CustomListView. Inoltre, potresti semplicemente aggiungere un panel come "linea" (diciamo Item) e anziché creare il suo contenuto da codice (le 4 label), caricarlo da un layout.

------------------

Let's clarify.

First of all, I assume that you want to see only whole "lines", not that these are a fixed number regardless of the device; am I wrong?

The main error, but not the only one, is the calculation of the number of lines, which is based on the height of the internal panel. You do not set this height. On my device, in your example project, the default height of the internal panel is 1500 dip, while that of the ScrollView (List) is 593 dip. Note that in your example you have AutoScaleAll on True and anchor only at the top. In this mode, depending on the device the height of the List will be different.

Anchor and AutoScaleAll affect "the outside" of the ScrollView, not on its internal panel, that's why you repeat that nothing changes, in the various tests.

I repeat that it would be much better if you used CustomListView. Also, you could simply add a panel as a "line" (let's say Item) and instead of creating its content by code (the 4 labels), load it from a layout.

-----

[Infine, sempre meglio creare progetti di tipo B4XPages; hanno solo vantaggi]
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes. I will follow your advice, and your code. Only i would avoid the funny appearance of the text... Thanks a lot.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The attached project works as you expect it to work with both anchor types.
The problem in your code with my solution is that you changed a few things:
- the value of iLettura begins with 1 but iRow begins with 0.
- nRowPage = ScrollView1.Height/RowHeight in my code becomes
nLPagMisure=Lista.Panel.Height/LineHeight in your code, why ?
I use the Scrollview.Height but you use the Scrollview.Panel.Height
You did not take this in account.

I added this line
Lista.Panel.Height = Lista.Height
to avoid problems depending on the Lista.Height you define in the Designer and the value of the internal panel height in the Designer.
 

Attachments

  • TestScroll3.zip
    10.1 KB · Views: 53
Last edited:
Upvote 0
Solution

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
The attached project works as you expect it to work with both anchor types.
The problem in your code with my solution is that you changed a few things:
- the value of iLettura begins with 1 but iRow begins with 0.
- nRowPage = ScrollView1.Height/RowHeight in my code becomes
nLPagMisure=Lista.Panel.Height/LineHeight in your code, why ?
I use the Scrollview.Height but you use the Scrollview.Panel.Height
You did not take this in account.

I added this line
Lista.Panel.Height = Lista.Height
to avoid problems depending on the Lista.Height you define in the Designer and the value of the internal panel height in the Designer.
Thanks a lot Klaus. I was suspicious about my use of Lista.Panel.Height, to be sincere, but i didn't try it.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
BTW : in ancient times, you published a lot of examples of tables. Sometimes they may be useful, for simple cases, not involving DB or editing of cells, like the present case. I didn't find them in this site, when I did a search, exactly thinking to apply one of them now. Are they still available or totally obsolete?
 
Upvote 0
Top