Multiple ListViews inside a ScrollView

后端 未结 6 1210
旧巷少年郎
旧巷少年郎 2020-12-28 23:04

I have a complex xml file and I really need a ScrollView. Please Don\'t mind the Ids as I have changed them. The ScrollView here doesn\'t work.



        
6条回答
  •  情歌与酒
    2020-12-28 23:23

    You can set it by engulfing ListView in a parent layout like an example below -

    
    
    
        
    
    
            
    
                
    
                    
    
                    
    
                
    
            
    
            
    
            
    
                
    
                    
    
                    
    
                
    
            
    
            
    
        
    
    
    

    And, after the listview is populated -

    eachitemSize = 180;
    
    cardBrand.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * brandsAdapter.getCount()));
    
    cardRating.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * ratingAdapter.getCount()));
    

    What we did above is, we set the ScrollView height to match_parent. And after the ListView is populated we defined each parent layout of ListView to its ListView item count. And, remember to change the value of eachitemSize according to your each cell size.

    Another thing to care about is after the LayoutParams is assigned its margin becomes invalid due to the new LayoutParams. So, use instead of margin like the example above.

    Hope it helps!!

提交回复
热议问题