Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll bar to custom ViewGroup

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

like image 303
Natali Avatar asked Mar 01 '12 11:03

Natali


People also ask

How do I make my recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

Is ScrollView a Viewgroup?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.


1 Answers

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>
like image 121
Shubhayu Avatar answered Sep 20 '22 05:09

Shubhayu