6

I have CustomComponent class which extends ViewGroup.

Source code of CustomComponent:

      public class CustomComponent extends ViewGroup {

            private static final String LOGTAG = "CustomComponent";

            private List<MenuItem> items;

            private Context context;

            private int screenWidth;
            private int screenHeight;
            private int cellWidth;
            private int cellHeight;
            private int duration;
        private int space=7;

       public CustomComponent(Context context) {
            super(context);
            this.context=context;
       }


             @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Log.v(LOGTAG, "on Measure called");
             screenWidth = MeasureSpec.getSize(widthMeasureSpec);
              screenHeight = MeasureSpec.getSize(heightMeasureSpec);
              cellHeight=screenHeight/AppConstants.HEIDHTCELLSCOUNT;
              cellWidth=screenWidth/AppConstants.WIDTHCELLSCOUNT;
              duration= cellHeight*2;
             super.onMeasure(widthMeasureSpec, heightMeasureSpec+duration);

          }


           @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            Log.v(LOGTAG, "onLayout called");
            int childCount = this.getChildCount();
                 for (int i = 0; i < childCount; i++) {
                      View child = getChildAt(i);
                          child.layout(items.get(i).getLeft(),items.get(i).getTop(),items.get(i).getRight(), items.get(i).getBottom());
        } 
      }

public List<MenuItem> getItems() {
        return items;
    }

    public void setItems(List<MenuItem> items) {
        this.items = items;
    }
    }

Xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"
    android:scrollbars="vertical">
        <<package name>.CustomComponentandroid:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/menu_component"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"/>
</LinearLayout>

I need to add vertical scroll to this ViewGroup. Please help, I have no idea how to solve this problem. enter image description here

Natali
  • 2,934
  • 4
  • 39
  • 53
  • The code from [here](http://code.google.com/p/android-masonry/source/checkout) worked for me you may need some editing to suit yours – almuneef Mar 07 '12 at 08:27
  • Hi Natali, I know this question is too old but did you get solution to this. If you have got it plz post it. I am stuck in similar situation – silwar Jun 26 '14 at 09:26
  • Hello. I did not find any solution for it, so I used Relative Layout with Custom Views (colored boxes) as child views. – Natali Jun 27 '14 at 12:00

3 Answers3

3

Try this. You need a LinearLayout inside a ScrollView. Put your MenuComponent inside the LinearLayout. It should work.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <<package name>.MenuComponent android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:id="@+id/menu_component"
                android:scrollbars="vertical"
                android:fadingEdge="vertical"/>
        </LinearLayout>

    </ScrollView>
</LinearLayout>
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
1

You could try to include your MenuComponent inside a ScrollView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"
    android:scrollbars="vertical">
      <ScrollView android:id="@+id/ScrollView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" >

        <<package name>.MenuComponent android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/menu_component"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"/>
       </ScrollView>
</LinearLayout>
Natali
  • 2,934
  • 4
  • 39
  • 53
JuanMa Cuevas
  • 1,162
  • 9
  • 22
  • I try this before ask this question, but whith this xml I have the black screen. My viev grout is created but unfortunately it's not shown. – Natali Mar 01 '12 at 12:03
  • Could you add more info about your problem? what do you want to achieve with your custom MenuComponent? What errors are you getting? – JuanMa Cuevas Mar 02 '12 at 09:39
  • I added scheme of my component. I need add scroll down ny `ViewGroup` to the `duration` size. – Natali Mar 02 '12 at 09:49
  • Do you need to use that custom component? I would suggest using a `RelativeLayout` (container) inside a `ScrollView` and inflate each item inside the container – JuanMa Cuevas Mar 02 '12 at 11:04
  • Thank for your council. I will think adout it. – Natali Mar 02 '12 at 11:12
0

you have to call awakenScrollBars() ,which will trigger the scrollbars to draw on your custom viewGroup.(more details) and the vertical scrollbar enabled has to be true setVerticalScrollBarEnabled()

Also you have to override functions computeVerticalScrollExtent() and computeVerticalScrollRange to set thumb's size and scrollbar scroll range .

Sreejith B Naick
  • 1,203
  • 7
  • 13