Полоса прокрутки для пользовательского ViewGroup - PullRequest
5 голосов
/ 01 марта 2012

У меня есть CustomComponent класс, который расширяется ViewGroup.

Исходный код 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-макет:

<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нужно добавить вертикальную прокрутку к этому ViewGroup.Пожалуйста, помогите, я понятия не имею, как решить эту проблему.enter image description here

Ответы [ 3 ]

1 голос
/ 02 марта 2012

Попробуй это.Вам нужен LinearLayout внутри ScrollView.Поместите свой MenuComponent внутри LinearLayout.Это должно работать.

<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>
1 голос
/ 01 марта 2012

Вы можете попытаться включить свой MenuComponent в 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>
0 голосов
/ 01 июля 2014

вам нужно позвонить awakenScrollBars(), что заставит полосы прокрутки рисовать в вашей пользовательской группе просмотра ( больше подробностей ), а активированная вертикальная полоса прокрутки должна быть истинной setVerticalScrollBarEnabled()

Также вам нужно переопределить функции computeVerticalScrollExtent() и computeVerticalScrollRange, чтобы установить размер большого пальца и диапазон прокрутки полосы прокрутки.

...