2012-03-01 95 views
5

我有CustomComponent类延伸ViewGroup滚动条到自定义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> 

我需要垂直滚动到这个ViewGroup。 请帮忙,我不知道如何解决这个问题。 enter image description here

+0

从[这里](http://code.google.com/p/android-masonry/source/checkout)的代码为我工作,你可能需要一些适合你的编辑 – almuneef 2012-03-07 08:27:51

+0

嗨娜塔莉,我知道这个问题是太旧了,但你有没有解决这个问题。如果你有它,请将其张贴。我陷入了类似的情况 – silwar 2014-06-26 09:26:10

+0

你好。我没有找到任何解决方案,所以我使用相对布局与自定义视图(彩色框)作为子视图。 – Natali 2014-06-27 12:00:29

回答

1

您可以尝试包括你MenuComponentScrollView

<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

我试过这个问题之前问这个问题,但whith这个XML我有黑屏。我的viev浆被创建,但不幸的是没有显示。 – Natali 2012-03-01 12:03:57

+0

你可以添加更多关于你的问题的信息吗?你想用自定义的MenuComponent实现什么?你得到什么错误? – 2012-03-02 09:39:20

+0

我添加了我的组件的方案。我需要将ny ViewGroup添加到'duration'大小。 – Natali 2012-03-02 09:49:33

1

试试这个。你需要一个ScrollView内的LinearLayout。把你的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> 
+0

不幸的是我有相同的'balack屏幕'。 :( – Natali 2012-03-02 10:19:26

+0

您是否设法解决问题? – Shubhayu 2012-03-05 06:04:29

+0

不,我不知道该怎么做。 – Natali 2012-03-05 07:28:56

0

你必须调用awakenScrollBars(),这将触发滚动条上绘制自定义的ViewGroup。(more details),并启用了垂直滚动条必须是真实的setVerticalScrollBarEnabled()

你也需要重写功能computeVerticalScrollExtent()computeVerticalScrollRange设置拇指大小和滚动条滚动范围。