Search code examples
androidhorizontalscrollviewhorizontallist

How to refresh horizontalview in android


I have 10 section like a tab, but not a tab, it is button.

enter image description here

if i click Europe, then display data on horizontal view.Data not fixed. eg 10 , if i clicked africa then 25 data will be populated in horizontal view.

How could i do this.

Note: I display data succesfully in first time , when click europe 10 data in to the horizontal view but in second, other buttons then it cannot be display, something went crash. and got this error log.

01-13 11:30:18.090: E/AndroidRuntime(3493): java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
01-13 11:30:18.090: E/AndroidRuntime(3493):     at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:213)

My sample code, where getting error.

I just solve adding this line of code.

                linlayout.addView(innerYello);

                if(horizontalScrollview.getChildCount()>0){
                    horizontalScrollview.removeAllViews();
                    horizontalScrollview.addView(linlayout);
                }else {
                    horizontalScrollview.addView(linlayout);
                }

Solution

  • IllegalStateException: HorizontalScrollView can host only one direct child

    means you are trying to add more then one view in HorizontalScrollView .

    In HorizontalScrollView we only add one view as a child of HorizontalScrollView. so if you want to add more view then use any container view like RelativeLayout,LinearLayout,FrameLayout,... as child of HorizontalScrollView then add other views in container view instead of directly in HorizontalScrollView