Android Question CustomListView - How to change ItemHeight?

jtare

Active Member
Licensed User
Longtime User
which is the right way to change the item height?
I tried this:
change from original 100dip item height to 200dip itemheight when the user click the item

B4X:
Sub clv2_ItemClick(Index As Int, Value As Object)
Dim pnlnew AsPanel
Dim newvalue As Object
 newvalue = Value
 pnlnew= clv2.GetPanel(Index)
 clv2.RemoveAt(Index)
 clv2.InsertAt(Index,pnlnew,200dip,newvalue)
Log(Index & " = " & Value)
End Sub

But I get this error:
B4X:
LogCat connected to: 5718712f
--------- beginning of system--------- beginning of main~i:** Activity (main) Create, isFirst = true **~i:** Activity (main) Resume **
customlistview_insertat (java line: 242)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4212)
    at android.view.ViewGroup.addView(ViewGroup.java:4065)
    at android.view.ViewGroup.addView(ViewGroup.java:4041)
    at anywheresoftware.b4a.objects.PanelWrapper.AddView(PanelWrapper.java:65)
    at anywheresoftware.b4a.samples.customlistview.customlistview._insertat(customlistview.java:242)
    at anywheresoftware.b4a.samples.customlistview.main._clv2_itemclick(main.java:389)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:847)
    at anywheresoftware.b4a.samples.customlistview.customlistview._panel_click(customlistview.java:388)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:5197)
    at android.view.View$PerformClick.run(View.java:20926)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

then I tried this and it worked:

B4X:
Sub clv2_ItemClick(Index As Int, Value As Object)
Dim newvalue As Object
 newvalue = Value
 pnlnew= clv2.GetPanel(Index)
 clv2.RemoveAt(Index)
 clv2.InsertAt(Index,CreateListItem($"Item #${Index+1}"$, clv2.AsView.Width, 100dip),200dip,newvalue)
Log(Index & " = " & Value)
End Sub

Finally I tried this code and it does work fine, but it implies to REMOVE the panel and then insert the same panel again ( I dont like this method)

B4X:
Sub clv2_ItemClick(Index As Int, Value As Object)
    If clv2.GetPanel(Index).Height = 100dip Then
        clv2.GetPanel(Index).Height = 200dip
    SetItemHeight(200dip,clv2,Index,Value,clv2.GetPanel(Index))   
    Else
        clv2.GetPanel(Index).Height = 100dip
    SetItemHeight(100dip,clv2,Index,Value,clv2.GetPanel(Index))       
    End If
   
    'SetItemHeight(200dip,clv2,Index,Value,clv2.GetPanel(Index))
   
    Log(Index & " = " & Value)
End Sub


Sub SetItemHeight ( ItemHeight As Int, cv As CustomListView, index As Int, NewValue As Object, pnlnew As Panel) As Int
    Dim spnlnew As Panel
    Dim snewvalue As Object   
    snewvalue = NewValue
    spnlnew = pnlnew
    spnlnew.Height =ItemHeight
    cv.RemoveAt(index)
    spnlnew.RemoveView
    spnlnew.LoadLayout("CellItem")
    'spnlnew.Height = ItemHeight
    cv.InsertAt(index,spnlnew,ItemHeight,snewvalue)
    Return ItemHeight
End Sub

Is there a way to only change the item height without deleting and adding the same item?
Because while the program is deleting the old panel and adding the new one, it flashes the background color and does not look good for a good user experience.
Thanks.
 

mangojack

Well-Known Member
Licensed User
Longtime User
Is there a way to only change the item height without deleting and adding the same item?
You can just change the item height simply like this ..
B4X:
   Dim pnlNew As Panel
   pnlNew= clv1.GetPanel(Index)
   pnlNew.Height = 50dip
But ... Then you must reposition all the following items to allow for height change ... so in a way the answer is No, unless you rewrite the InsertAt sub.

B4X:
Sub clv2_ItemClick(Index As Int, Value As Object)
   Log(Index & " = " & Value)
   Dim newHeight As Int = 200dip
   SetItemHeight(newHeight,Index,Value,clv2.GetPanel(Index))
End Sub

Sub SetItemHeight ( ItemHeight As Int, index As Int, Value As Object, pnlOld As Panel)
  Dim pnlNew As Panel
  pnlNew = pnlOld
  clv2.RemoveAt(index)
  pnlNew.RemoveView
  pnlNew.LoadLayout("CellItem")
  clv2.InsertAt(index,pnlNew,ItemHeight,Value)
End Sub
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
As a workaround you could change the click color ..
In the CLV class
B4X:
Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map)
   ReplaceBasePanelWithView(base, sv)   
   sv.Color = props.Get("DividerColor")
   dividerHeight = DipToCurrent(props.Get("DividerHeight")) 'need to scale the value
   
   PressedDrawable.Initialize(Colors.Black,2dip)  '(props.Get("PressedColor"), 2dip)
I have tried to play with the ' PressedColor ' #Designer property .. but it did have an effect (I have not used this before so unsure)
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
you could change the click color ..
It seems to solve the problem changing the click color with a low contrast one, that can "blend" with the panel color, in this case black.
Visually it seems that does not flash.
Thanks for the solution.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Thanks for the solution.

As a follow up .. If you were unaware , the colors for the custom view can be changed directly in designer.

Then
B4X:
PressedDrawable.Initialize(props.Get("PressedColor"), 2dip)
As it was originally ... Still we learn.;)
 
Upvote 0
Top